diff --git a/covas_mobile/lib/pages/ItemMenu.dart b/covas_mobile/lib/pages/ItemMenu.dart index 3d923e9..5d93215 100644 --- a/covas_mobile/lib/pages/ItemMenu.dart +++ b/covas_mobile/lib/pages/ItemMenu.dart @@ -4,6 +4,7 @@ import 'dart:io'; import 'dart:convert'; import 'package:covas_mobile/classes/alert.dart'; +import 'package:covas_mobile/pages/ListItemByTags.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; @@ -15,6 +16,7 @@ import '../variable/globals.dart' as globals; import '../classes/events.dart'; import 'ListItemMenu.dart'; +import 'ListItemByTags.dart'; void main() { initializeDateFormatting("fr_FR", null).then((_) => (const MyApp())); @@ -337,6 +339,13 @@ class _ItemMenuState extends State with ShowErrorDialog { mainAxisSize: MainAxisSize.min, children: [ InkWell( + onTap: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (_) => + ListItemTags(tags: '$tag'))); + }, child: Text( '$tag', style: diff --git a/covas_mobile/lib/pages/ListItemByTags.dart b/covas_mobile/lib/pages/ListItemByTags.dart new file mode 100644 index 0000000..35a0d88 --- /dev/null +++ b/covas_mobile/lib/pages/ListItemByTags.dart @@ -0,0 +1,122 @@ +import 'dart:convert'; +import 'dart:io'; +import "ItemMenu.dart"; + +import 'package:http/http.dart' as http; +import 'package:flutter/material.dart'; +import '../classes/events.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:intl/intl.dart'; +import 'package:intl/date_symbol_data_local.dart'; + +import '../variable/globals.dart' as globals; + +// app starting point +void main() { + initializeDateFormatting("fr_FR", null).then((_) => (const MyApp())); +} + +class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + + @override + Widget build(BuildContext context) { + return MaterialApp( + home: const ListItemTags(tags: "default"), + debugShowCheckedModeBanner: false, + ); + } +} + +// homepage class +class ListItemTags extends StatefulWidget { + const ListItemTags({Key? key, required this.tags}) : super(key: key); + + final String tags; + @override + State createState() => _MyHomePageState(); +} + +// homepage state +class _MyHomePageState extends State { + // variable to call and store future list of posts + + // function to fetch data from api and return future list of posts + static Future> getPosts(tags) async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + var accessToken = prefs.getString("access_token") ?? ""; + final List body = []; + if (accessToken.isNotEmpty) { + var url = Uri.parse("${globals.api}/events?tags=${tags}"); + 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; + } + + // build function + @override + Widget build(BuildContext context) { + return Scaffold( + body: Center( + // FutureBuilder + child: FutureBuilder>( + future: getPosts(widget.tags), + 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!; + return buildPosts(posts); + } else { + // if no data, show simple Text + return const Text("No data available"); + } + }, + ), + ), + ); + } + + // function to display fetched data on screen + Widget buildPosts(List posts) { + // ListView Builder to show data in a list + return Scaffold( + appBar: AppBar( + // Here we take the value from the MyHomePage object that was created by + // the App.build method, and use it to set our appbar title. + title: Text("Tags : ${widget.tags}"), + backgroundColor: Colors.blue, + foregroundColor: Colors.white, + ), + 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 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(); + }, + ), + ); + } +}