mobile-flutter/covas_mobile/lib/pages/ListItemMenu.dart

260 lines
7.8 KiB
Dart
Raw Normal View History

2024-06-23 17:23:26 +02:00
import 'dart:convert';
2024-06-24 23:57:43 +02:00
import 'dart:io';
2024-06-30 13:32:36 +02:00
import "ItemMenu.dart";
2024-07-06 17:29:41 +02:00
import "Camera.dart";
2024-06-23 17:23:26 +02:00
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
2024-06-30 15:17:10 +02:00
import '../classes/events.dart';
2024-06-23 20:57:54 +02:00
import 'package:shared_preferences/shared_preferences.dart';
2024-06-27 00:01:07 +02:00
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';
2024-07-06 17:29:41 +02:00
import 'package:camera/camera.dart';
2024-06-23 17:23:26 +02:00
2024-06-30 15:17:10 +02:00
import '../variable/globals.dart' as globals;
2024-06-24 23:57:43 +02:00
2024-06-23 17:23:26 +02:00
// app starting point
void main() {
2024-06-27 00:01:07 +02:00
initializeDateFormatting("fr_FR", null).then((_) => (const MyApp()));
2024-06-23 17:23:26 +02:00
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: const ListItemMenu(),
debugShowCheckedModeBanner: false,
);
}
}
// homepage class
class ListItemMenu extends StatefulWidget {
const ListItemMenu({super.key});
@override
State<ListItemMenu> createState() => _MyHomePageState();
}
// homepage state
class _MyHomePageState extends State<ListItemMenu> {
// variable to call and store future list of posts
2024-06-24 23:57:43 +02:00
Future<List<Events>> postsFuture = getPosts();
2024-10-23 22:43:03 +02:00
List<Events> filteredPosts = [];
late SearchBar searchBar;
2024-06-23 17:23:26 +02:00
// function to fetch data from api and return future list of posts
2024-06-24 23:57:43 +02:00
static Future<List<Events>> getPosts() async {
2024-06-23 20:57:54 +02:00
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
2024-06-24 23:57:43 +02:00
final List<Events> body = [];
2024-06-23 20:57:54 +02:00
if (accessToken.isNotEmpty) {
2024-06-30 14:48:48 +02:00
var url = Uri.parse("${globals.api}/events");
2024-06-24 23:57:43 +02:00
final response = await http.get(url, headers: {
"Content-Type": "application/json",
HttpHeaders.cookieHeader: "access_token=${accessToken}"
});
final List body = json.decode(utf8.decode(response.bodyBytes));
return body.map((e) => Events.fromJson(e)).toList();
2024-06-23 20:57:54 +02:00
}
return body;
2024-06-23 17:23:26 +02:00
}
2024-10-23 22:43:03 +02:00
Future<List<Events>> searchPosts(String query) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
final List<Events> body = [];
if (accessToken.isNotEmpty) {
var url = Uri.parse("${globals.api}/events/search?item=$query");
final response = await http.get(url, headers: {
"Content-Type": "application/json",
HttpHeaders.cookieHeader: "access_token=${accessToken}"
});
final List body = json.decode(utf8.decode(response.bodyBytes));
return body.map((e) => Events.fromJson(e)).toList();
}
return body;
}
2024-07-06 17:29:41 +02:00
Future<void> popCamera() async {
await availableCameras().then((value) => Navigator.push(context,
MaterialPageRoute(builder: (_) => Camera(camera: value.first))));
}
2024-10-23 22:43:03 +02:00
void _filterPosts(String query) async {
if (query.isNotEmpty) {
List<Events> results = await searchPosts(query);
setState(() {
filteredPosts = results;
});
} else {
// Reset to full list or clear results
setState(() {
filteredPosts.clear();
});
}
}
2024-06-23 17:23:26 +02:00
// build function
@override
Widget build(BuildContext context) {
return Scaffold(
2024-10-23 22:43:03 +02:00
appBar: AppBar(
title: const Text("Item list menu"),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
actions: [
IconButton(
icon: const Icon(Icons.search),
onPressed: () {
showSearch(
context: context,
delegate: SearchDelegateExample(),
);
},
),
],
),
2024-06-23 17:23:26 +02:00
body: Center(
// FutureBuilder
2024-06-24 23:57:43 +02:00
child: FutureBuilder<List<Events>>(
2024-06-23 17:23:26 +02:00
future: postsFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
// until data is fetched, show loader
return const CircularProgressIndicator();
} else if (snapshot.hasData) {
// once data is fetched, display it on screen (call buildPosts())
final posts = snapshot.data!;
2024-10-23 22:43:03 +02:00
final displayedPosts =
filteredPosts.isEmpty ? posts : filteredPosts;
return buildPosts(displayedPosts);
2024-06-23 17:23:26 +02:00
} else {
// if no data, show simple Text
return const Text("No data available");
}
},
),
),
);
}
// function to display fetched data on screen
2024-06-24 23:57:43 +02:00
Widget buildPosts(List<Events> posts) {
2024-06-23 17:23:26 +02:00
// ListView Builder to show data in a list
2024-06-26 22:32:27 +02:00
return Scaffold(
2024-06-26 23:10:16 +02:00
body: ListView.separated(
itemCount: posts.length,
itemBuilder: (context, index) {
final post = posts[index];
2024-06-27 00:01:07 +02:00
final startDate = DateTime.parse(post.startDate!);
final date = DateFormat.yMd().format(startDate);
final time = DateFormat.Hm().format(startDate);
2024-06-26 23:10:16 +02:00
return ListTile(
2024-06-27 00:01:07 +02:00
title: Text('${post.name!}'),
2024-06-29 00:17:22 +02:00
subtitle: Text('${post.place!}\n${date} ${time}'),
onTap: () {
2024-06-30 13:32:36 +02:00
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ItemMenu(title: post.id!)));
2024-06-29 00:17:22 +02:00
});
2024-06-26 23:10:16 +02:00
},
separatorBuilder: (context, index) {
return Divider();
},
),
floatingActionButton: FloatingActionButton(
2024-07-06 17:29:41 +02:00
onPressed: popCamera,
2024-06-26 23:10:16 +02:00
backgroundColor: Colors.blue,
tooltip: 'Recherche',
2024-10-23 23:56:04 +02:00
child: const Icon(Icons.camera_alt, color: Colors.white),
2024-06-26 23:10:16 +02:00
),
);
2024-06-23 17:23:26 +02:00
}
}
2024-10-23 22:43:03 +02:00
class SearchDelegateExample extends SearchDelegate {
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
query = '';
},
),
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
close(context, null);
},
);
}
@override
Widget buildResults(BuildContext context) {
// Perform the search and return the results
return FutureBuilder<List<Events>>(
future: searchPosts(query),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasData) {
final posts = snapshot.data!;
return ListView.builder(
itemCount: posts.length,
itemBuilder: (context, index) {
final post = posts[index];
return ListTile(
title: Text(post.name!),
subtitle: Text(post.place!),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ItemMenu(title: post.id!),
),
);
},
);
},
);
} else {
return const Center(child: Text("No results found"));
}
},
);
}
@override
Widget buildSuggestions(BuildContext context) {
// Show suggestions as the user types
return Container(); // Implement suggestions if needed
}
Future<List<Events>> searchPosts(String query) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
final List<Events> body = [];
if (accessToken.isNotEmpty) {
var url = Uri.parse("${globals.api}/events/search?item=$query");
final response = await http.get(url, headers: {
"Content-Type": "application/json",
HttpHeaders.cookieHeader: "access_token=${accessToken}"
});
final List body = json.decode(utf8.decode(response.bodyBytes));
return body.map((e) => Events.fromJson(e)).toList();
}
return body;
}
}