diff --git a/covas_mobile/lib/ListItemMenu.dart b/covas_mobile/lib/ListItemMenu.dart new file mode 100644 index 0000000..eea0e64 --- /dev/null +++ b/covas_mobile/lib/ListItemMenu.dart @@ -0,0 +1,120 @@ +import 'dart:convert'; +import 'dart:io'; +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({super.key}); + + @override + Widget build(BuildContext context) { + return MaterialApp( + home: const ListItemMenu(), + debugShowCheckedModeBanner: false, + ); + } +} + +// homepage class +class ListItemMenu extends StatefulWidget { + const ListItemMenu({super.key}); + + @override + State createState() => _MyHomePageState(); +} + +// homepage state +class _MyHomePageState extends State { + // variable to call and store future list of posts + Future> postsFuture = getPosts(); + + // function to fetch data from api and return future list of posts + static Future> getPosts() async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + var accessToken = prefs.getString("access_token") ?? ""; + final List body = []; + if (accessToken.isNotEmpty) { + var url = Uri.parse("http://${globals.api}/events"); + 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: postsFuture, + 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("Item list menu"), + 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}')); + }, + separatorBuilder: (context, index) { + return Divider(); + }, + ), + floatingActionButton: FloatingActionButton( + onPressed: () {}, + backgroundColor: Colors.blue, + tooltip: 'Recherche', + child: const Icon(Icons.search, color: Colors.white), + ), + ); + } +} diff --git a/covas_mobile/lib/classes/events.dart b/covas_mobile/lib/classes/events.dart new file mode 100644 index 0000000..c1cb070 --- /dev/null +++ b/covas_mobile/lib/classes/events.dart @@ -0,0 +1,15 @@ +class Events { + String? id; + String? name; + String? place; + String? startDate; + + Events({this.place, this.id, this.name, this.startDate}); + + Events.fromJson(Map json) { + id = json['id']; + name = json['name']; + place = json['place']; + startDate = json["start_date"]; + } +} diff --git a/covas_mobile/lib/main.dart b/covas_mobile/lib/main.dart index 63d637f..817a956 100644 --- a/covas_mobile/lib/main.dart +++ b/covas_mobile/lib/main.dart @@ -5,7 +5,8 @@ import 'package:http/http.dart' as http; import 'dart:convert'; import 'dart:io'; -import 'MyHomePage.dart'; +//import 'MyHomePage.dart'; +import 'ListItemMenu.dart'; import 'classes/alert.dart'; @@ -74,9 +75,7 @@ class _LoginDemoState extends State with ShowErrorDialog { } } Navigator.push( - context, - MaterialPageRoute( - builder: (_) => MyHomePage(title: 'Flutter Demo'))); + context, MaterialPageRoute(builder: (_) => ListItemMenu())); } else { var text = ""; switch (response.statusCode) { @@ -136,9 +135,7 @@ class _LoginDemoState extends State with ShowErrorDialog { headers: {HttpHeaders.cookieHeader: 'access_token: ${access_token}'}); if (responseToken.statusCode == 200) { Navigator.push( - context, - MaterialPageRoute( - builder: (_) => MyHomePage(title: 'Flutter Demo'))); + context, MaterialPageRoute(builder: (_) => ListItemMenu())); } else { prefs.remove("access_token"); } @@ -157,6 +154,8 @@ class _LoginDemoState extends State with ShowErrorDialog { backgroundColor: Colors.white, appBar: AppBar( title: Text("Login Page"), + backgroundColor: Colors.blue, + foregroundColor: Colors.white, ), body: SingleChildScrollView( child: Column( diff --git a/covas_mobile/pubspec.lock b/covas_mobile/pubspec.lock index 2cbf4e6..b873bf1 100644 --- a/covas_mobile/pubspec.lock +++ b/covas_mobile/pubspec.lock @@ -112,6 +112,14 @@ packages: url: "https://pub.dev" source: hosted version: "4.0.2" + intl: + dependency: "direct main" + description: + name: intl + sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf + url: "https://pub.dev" + source: hosted + version: "0.19.0" leak_tracker: dependency: transitive description: diff --git a/covas_mobile/pubspec.yaml b/covas_mobile/pubspec.yaml index c1cb06c..1356f4a 100644 --- a/covas_mobile/pubspec.yaml +++ b/covas_mobile/pubspec.yaml @@ -36,6 +36,7 @@ dependencies: cupertino_icons: ^1.0.2 http: ^1.2.1 shared_preferences: ^2.2.3 + intl: ^0.19.0 dev_dependencies: flutter_test: