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: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<ListItemMenu> {
// variable to call and store future list of posts
Future<List<Events>> postsFuture = getPosts();
List<Events> filteredPosts = [];
late SearchBar searchBar;
String geographicalZone = '';
String query = '';
@ -86,27 +81,21 @@ class _MyHomePageState extends State<ListItemMenu> {
MaterialPageRoute(builder: (_) => Camera(camera: value.first))));
}
void _filterPosts() async {
if (query.isNotEmpty || geographicalZone.isNotEmpty) {
List<Events> results = await searchPosts(query);
setState(() {
filteredPosts = _applyFilters(results);
});
} 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();
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<ListItemMenu> {
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<List<Events>>(
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<ListItemMenu> {
// function to display fetched data on screen
Widget buildPosts(List<Events> 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
}