Compare commits
2 Commits
951127d7bc
...
b796d0206f
Author | SHA1 | Date | |
---|---|---|---|
b796d0206f | |||
517652df98 |
@ -62,6 +62,27 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
// Initialize data fetch when the page loads
|
||||||
|
_fetchInitialData();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch initial data from API or any other necessary initialization
|
||||||
|
Future<void> _fetchInitialData() async {
|
||||||
|
try {
|
||||||
|
// Optionally, you can fetch posts initially if needed.
|
||||||
|
List<Events> initialPosts = await getPosts();
|
||||||
|
setState(() {
|
||||||
|
// Assign to the postsFuture and update the filtered posts if needed
|
||||||
|
filteredPosts = initialPosts;
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
print('Error fetching initial data: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> searchSuggestions(String input) async {
|
Future<void> searchSuggestions(String input) async {
|
||||||
await dotenv.load(fileName: ".env"); // Load .env file
|
await dotenv.load(fileName: ".env"); // Load .env file
|
||||||
|
|
||||||
@ -109,13 +130,21 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
HttpHeaders.cookieHeader: "access_token=$accessToken"
|
HttpHeaders.cookieHeader: "access_token=$accessToken"
|
||||||
});
|
});
|
||||||
|
print("status code : ${response.statusCode}");
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
final List<dynamic> body = json.decode(utf8.decode(response.bodyBytes));
|
final List<dynamic> body = json.decode(utf8.decode(response.bodyBytes));
|
||||||
|
print("results fetch : ${body}");
|
||||||
|
// Update state after getting the response
|
||||||
setState(() {
|
setState(() {
|
||||||
filteredPosts = body
|
if (body.isNotEmpty) {
|
||||||
.map((e) => Events.fromJson(e as Map<String, dynamic>))
|
// If we have results, map them to Events
|
||||||
.toList();
|
filteredPosts = body
|
||||||
|
.map((e) => Events.fromJson(e as Map<String, dynamic>))
|
||||||
|
.toList();
|
||||||
|
} else {
|
||||||
|
// If no results, clear filteredPosts
|
||||||
|
filteredPosts.clear();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
throw Exception('Failed to load posts');
|
throw Exception('Failed to load posts');
|
||||||
@ -134,12 +163,13 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
labelText: 'Search by geographical zone',
|
labelText: 'Search by geographical zone',
|
||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(),
|
||||||
suffixIcon: IconButton(
|
suffixIcon: IconButton(
|
||||||
icon: Icon(Icons.clear),
|
icon: const Icon(Icons.clear),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
setState(() {
|
setState(() {
|
||||||
inputGeo.clear(); // Clear the text field
|
inputGeo.clear(); // Clear the text field
|
||||||
geographicalZone = ''; // Reset the geographical zone state
|
geographicalZone = ''; // Reset the geographical zone state
|
||||||
suggestions.clear(); // Optionally clear suggestions
|
suggestions.clear(); // Optionally clear suggestions
|
||||||
|
_fetchInitialData(); // Clear the filtered posts
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@ -155,9 +185,8 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
Container(
|
Container(
|
||||||
height: 200,
|
height: 200,
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
border: Border.all(color: Colors.blue), // Add a border color
|
border: Border.all(color: Colors.blue),
|
||||||
borderRadius:
|
borderRadius: BorderRadius.circular(8),
|
||||||
BorderRadius.circular(8), // Optional: rounded corners
|
|
||||||
),
|
),
|
||||||
child: ListView.builder(
|
child: ListView.builder(
|
||||||
shrinkWrap: true,
|
shrinkWrap: true,
|
||||||
@ -234,25 +263,37 @@ 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) {
|
||||||
|
print("filteredposts : ${filteredPosts}");
|
||||||
|
final displayedPosts = filteredPosts;
|
||||||
|
print("results ${displayedPosts}");
|
||||||
|
// If filteredPosts is empty, show a message saying no data is available
|
||||||
|
if (displayedPosts.isEmpty) {
|
||||||
|
return const Center(
|
||||||
|
child: Text('No events available for this location.',
|
||||||
|
style: TextStyle(fontSize: 18, color: Colors.grey)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return ListView.separated(
|
return ListView.separated(
|
||||||
itemCount: posts.length,
|
itemCount: displayedPosts.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final post = posts[index];
|
final post = displayedPosts[index];
|
||||||
final startDate = DateTime.parse(post.startDate!);
|
final startDate = DateTime.parse(post.startDate!);
|
||||||
final date = DateFormat.yMd().format(startDate);
|
final date = DateFormat.yMd().format(startDate);
|
||||||
final time = DateFormat.Hm().format(startDate);
|
final time = DateFormat.Hm().format(startDate);
|
||||||
|
|
||||||
return ListTile(
|
return ListTile(
|
||||||
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(context,
|
Navigator.push(
|
||||||
MaterialPageRoute(builder: (_) => ItemMenu(title: post.id!)));
|
context,
|
||||||
});
|
MaterialPageRoute(builder: (_) => ItemMenu(title: post.id!)),
|
||||||
},
|
);
|
||||||
separatorBuilder: (context, index) {
|
},
|
||||||
return Divider();
|
);
|
||||||
},
|
},
|
||||||
|
separatorBuilder: (context, index) => Divider(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user