From 576d045cd87ad1f6463627af5422487d737d3689 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 28 Oct 2024 22:52:00 +0100 Subject: [PATCH] add searchbar by geographical zone --- covas_mobile/lib/pages/ListItemMenu.dart | 113 ++++++++--------------- 1 file changed, 38 insertions(+), 75 deletions(-) diff --git a/covas_mobile/lib/pages/ListItemMenu.dart b/covas_mobile/lib/pages/ListItemMenu.dart index 92b37da..4c18ef3 100644 --- a/covas_mobile/lib/pages/ListItemMenu.dart +++ b/covas_mobile/lib/pages/ListItemMenu.dart @@ -1,8 +1,7 @@ import 'dart:convert'; import 'dart:io'; -import "ItemMenu.dart"; -import "Camera.dart"; - +import 'ItemMenu.dart'; +import 'Camera.dart'; import 'package:http/http.dart' as http; import 'package:flutter/material.dart'; import '../classes/events.dart'; @@ -10,12 +9,11 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'package:intl/intl.dart'; import 'package:intl/date_symbol_data_local.dart'; import 'package:camera/camera.dart'; - import '../variable/globals.dart' as globals; // app starting point void main() { - initializeDateFormatting("fr_FR", null).then((_) => (const MyApp())); + initializeDateFormatting("fr_FR", null).then((_) => runApp(const MyApp())); } class MyApp extends StatelessWidget { @@ -40,11 +38,8 @@ class ListItemMenu extends StatefulWidget { // homepage state class _MyHomePageState extends State { - // variable to call and store future list of posts Future> postsFuture = getPosts(); List filteredPosts = []; - late SearchBar searchBar; - String geographicalZone = ''; String query = ''; @@ -86,27 +81,21 @@ class _MyHomePageState extends State { MaterialPageRoute(builder: (_) => Camera(camera: value.first)))); } - void _filterPosts() async { - if (query.isNotEmpty || geographicalZone.isNotEmpty) { - List results = await searchPosts(query); - setState(() { - filteredPosts = _applyFilters(results); - }); - } else { - setState(() { - filteredPosts.clear(); - }); - } - } - - List _applyFilters(List posts) { - return posts.where((post) { - final matchesQuery = - post.name!.toLowerCase().contains(query.toLowerCase()); - final matchesZone = geographicalZone.isEmpty || - post.place!.toLowerCase().contains(geographicalZone.toLowerCase()); - return matchesQuery && matchesZone; - }).toList(); + Padding _buildGeographicalZoneSearchField() { + return Padding( + padding: const EdgeInsets.all(8.0), + child: TextField( + decoration: InputDecoration( + labelText: 'Search by geographical zone', + border: OutlineInputBorder(), + ), + onChanged: (value) { + setState(() { + geographicalZone = value; // Update geographical zone + }); + }, + ), + ); } // build function @@ -132,27 +121,13 @@ class _MyHomePageState extends State { body: Column( children: [ // New Search Bar for Geographical Zone - Padding( - padding: const EdgeInsets.all(8.0), - child: TextField( - decoration: InputDecoration( - labelText: 'Search by geographical zone', - border: OutlineInputBorder(), - ), - onChanged: (value) { - setState(() { - geographicalZone = value; - }); - _filterPosts(); // Call the filtering function - }, - ), - ), + _buildGeographicalZoneSearchField(), Expanded( child: FutureBuilder>( future: postsFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { - return const CircularProgressIndicator(); + return const Center(child: CircularProgressIndicator()); } else if (snapshot.hasData) { final posts = snapshot.data!; final displayedPosts = @@ -171,36 +146,25 @@ class _MyHomePageState extends State { // function to display fetched data on screen Widget buildPosts(List posts) { - // ListView Builder to show data in a list - return Scaffold( - body: ListView.separated( - itemCount: posts.length, - itemBuilder: (context, index) { - final post = posts[index]; - final startDate = DateTime.parse(post.startDate!); - final date = DateFormat.yMd().format(startDate); - final time = DateFormat.Hm().format(startDate); + return ListView.separated( + itemCount: posts.length, + itemBuilder: (context, index) { + final post = posts[index]; + final startDate = DateTime.parse(post.startDate!); + final date = DateFormat.yMd().format(startDate); + final time = DateFormat.Hm().format(startDate); - return ListTile( - title: Text('${post.name!}'), - subtitle: Text('${post.place!}\n${date} ${time}'), - onTap: () { - Navigator.push( - context, - MaterialPageRoute( - builder: (_) => ItemMenu(title: post.id!))); - }); - }, - separatorBuilder: (context, index) { - return Divider(); - }, - ), - floatingActionButton: FloatingActionButton( - onPressed: popCamera, - backgroundColor: Colors.blue, - tooltip: 'Recherche', - child: const Icon(Icons.camera_alt, color: Colors.white), - ), + return ListTile( + title: Text('${post.name!}'), + subtitle: Text('${post.place!}\n${date} ${time}'), + onTap: () { + Navigator.push(context, + MaterialPageRoute(builder: (_) => ItemMenu(title: post.id!))); + }); + }, + separatorBuilder: (context, index) { + return Divider(); + }, ); } } @@ -265,7 +229,6 @@ class SearchDelegateExample extends SearchDelegate { @override Widget buildSuggestions(BuildContext context) { - // Show suggestions as the user types return Container(); // Implement suggestions if needed }