add searchbar by geographical zone

This commit is contained in:
Valentin CZERYBA 2024-10-28 22:52:00 +01:00
parent 4e0222d4bb
commit 576d045cd8

View File

@ -1,8 +1,7 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import "ItemMenu.dart"; import 'ItemMenu.dart';
import "Camera.dart"; import 'Camera.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../classes/events.dart'; import '../classes/events.dart';
@ -10,12 +9,11 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart'; import 'package:intl/date_symbol_data_local.dart';
import 'package:camera/camera.dart'; import 'package:camera/camera.dart';
import '../variable/globals.dart' as globals; import '../variable/globals.dart' as globals;
// app starting point // app starting point
void main() { void main() {
initializeDateFormatting("fr_FR", null).then((_) => (const MyApp())); initializeDateFormatting("fr_FR", null).then((_) => runApp(const MyApp()));
} }
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
@ -40,11 +38,8 @@ class ListItemMenu extends StatefulWidget {
// homepage state // homepage state
class _MyHomePageState extends State<ListItemMenu> { class _MyHomePageState extends State<ListItemMenu> {
// variable to call and store future list of posts
Future<List<Events>> postsFuture = getPosts(); Future<List<Events>> postsFuture = getPosts();
List<Events> filteredPosts = []; List<Events> filteredPosts = [];
late SearchBar searchBar;
String geographicalZone = ''; String geographicalZone = '';
String query = ''; String query = '';
@ -86,27 +81,21 @@ class _MyHomePageState extends State<ListItemMenu> {
MaterialPageRoute(builder: (_) => Camera(camera: value.first)))); MaterialPageRoute(builder: (_) => Camera(camera: value.first))));
} }
void _filterPosts() async { Padding _buildGeographicalZoneSearchField() {
if (query.isNotEmpty || geographicalZone.isNotEmpty) { return Padding(
List<Events> results = await searchPosts(query); padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: InputDecoration(
labelText: 'Search by geographical zone',
border: OutlineInputBorder(),
),
onChanged: (value) {
setState(() { setState(() {
filteredPosts = _applyFilters(results); geographicalZone = value; // Update geographical zone
}); });
} else { },
setState(() { ),
filteredPosts.clear(); );
});
}
}
List<Events> _applyFilters(List<Events> 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();
} }
// build function // build function
@ -132,27 +121,13 @@ class _MyHomePageState extends State<ListItemMenu> {
body: Column( body: Column(
children: [ children: [
// New Search Bar for Geographical Zone // New Search Bar for Geographical Zone
Padding( _buildGeographicalZoneSearchField(),
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
},
),
),
Expanded( Expanded(
child: FutureBuilder<List<Events>>( child: FutureBuilder<List<Events>>(
future: postsFuture, future: postsFuture,
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) { if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator(); return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasData) { } else if (snapshot.hasData) {
final posts = snapshot.data!; final posts = snapshot.data!;
final displayedPosts = final displayedPosts =
@ -171,9 +146,7 @@ class _MyHomePageState extends State<ListItemMenu> {
// function to display fetched data on screen // function to display fetched data on screen
Widget buildPosts(List<Events> posts) { Widget buildPosts(List<Events> posts) {
// ListView Builder to show data in a list return ListView.separated(
return Scaffold(
body: ListView.separated(
itemCount: posts.length, itemCount: posts.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
final post = posts[index]; final post = posts[index];
@ -185,22 +158,13 @@ class _MyHomePageState extends State<ListItemMenu> {
title: Text('${post.name!}'), title: Text('${post.name!}'),
subtitle: Text('${post.place!}\n${date} ${time}'), subtitle: Text('${post.place!}\n${date} ${time}'),
onTap: () { onTap: () {
Navigator.push( Navigator.push(context,
context, MaterialPageRoute(builder: (_) => ItemMenu(title: post.id!)));
MaterialPageRoute(
builder: (_) => ItemMenu(title: post.id!)));
}); });
}, },
separatorBuilder: (context, index) { separatorBuilder: (context, index) {
return Divider(); return Divider();
}, },
),
floatingActionButton: FloatingActionButton(
onPressed: popCamera,
backgroundColor: Colors.blue,
tooltip: 'Recherche',
child: const Icon(Icons.camera_alt, color: Colors.white),
),
); );
} }
} }
@ -265,7 +229,6 @@ class SearchDelegateExample extends SearchDelegate {
@override @override
Widget buildSuggestions(BuildContext context) { Widget buildSuggestions(BuildContext context) {
// Show suggestions as the user types
return Container(); // Implement suggestions if needed return Container(); // Implement suggestions if needed
} }