Merge pull request 'feature/itemlist' (#2) from feature/itemlist into main

Reviewed-on: #2
This commit is contained in:
v4l3n71n 2024-06-29 00:07:13 +02:00
commit 43aeed4010
5 changed files with 150 additions and 7 deletions

View File

@ -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<ListItemMenu> createState() => _MyHomePageState();
}
// homepage state
class _MyHomePageState extends State<ListItemMenu> {
// variable to call and store future list of posts
Future<List<Events>> postsFuture = getPosts();
// function to fetch data from api and return future list of posts
static Future<List<Events>> getPosts() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
final List<Events> 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<List<Events>>(
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<Events> 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),
),
);
}
}

View File

@ -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<String, dynamic> json) {
id = json['id'];
name = json['name'];
place = json['place'];
startDate = json["start_date"];
}
}

View File

@ -5,7 +5,8 @@ import 'package:http/http.dart' as http;
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'MyHomePage.dart'; //import 'MyHomePage.dart';
import 'ListItemMenu.dart';
import 'classes/alert.dart'; import 'classes/alert.dart';
@ -74,9 +75,7 @@ class _LoginDemoState extends State<LoginDemo> with ShowErrorDialog {
} }
} }
Navigator.push( Navigator.push(
context, context, MaterialPageRoute(builder: (_) => ListItemMenu()));
MaterialPageRoute(
builder: (_) => MyHomePage(title: 'Flutter Demo')));
} else { } else {
var text = ""; var text = "";
switch (response.statusCode) { switch (response.statusCode) {
@ -136,9 +135,7 @@ class _LoginDemoState extends State<LoginDemo> with ShowErrorDialog {
headers: {HttpHeaders.cookieHeader: 'access_token: ${access_token}'}); headers: {HttpHeaders.cookieHeader: 'access_token: ${access_token}'});
if (responseToken.statusCode == 200) { if (responseToken.statusCode == 200) {
Navigator.push( Navigator.push(
context, context, MaterialPageRoute(builder: (_) => ListItemMenu()));
MaterialPageRoute(
builder: (_) => MyHomePage(title: 'Flutter Demo')));
} else { } else {
prefs.remove("access_token"); prefs.remove("access_token");
} }
@ -157,6 +154,8 @@ class _LoginDemoState extends State<LoginDemo> with ShowErrorDialog {
backgroundColor: Colors.white, backgroundColor: Colors.white,
appBar: AppBar( appBar: AppBar(
title: Text("Login Page"), title: Text("Login Page"),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
), ),
body: SingleChildScrollView( body: SingleChildScrollView(
child: Column( child: Column(

View File

@ -112,6 +112,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "4.0.2" 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: leak_tracker:
dependency: transitive dependency: transitive
description: description:

View File

@ -36,6 +36,7 @@ dependencies:
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
http: ^1.2.1 http: ^1.2.1
shared_preferences: ^2.2.3 shared_preferences: ^2.2.3
intl: ^0.19.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: