From 4df3b2a2865bd02b8494bd67a72e380b4202f515 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 23 Jun 2024 17:23:26 +0200 Subject: [PATCH] list functionnal test --- covas_mobile/lib/ListItemMenu.dart | 95 ++++++++++++++++++++++++++++++ covas_mobile/lib/classes/post.dart | 17 ++++++ covas_mobile/lib/main.dart | 11 ++-- 3 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 covas_mobile/lib/ListItemMenu.dart create mode 100644 covas_mobile/lib/classes/post.dart diff --git a/covas_mobile/lib/ListItemMenu.dart b/covas_mobile/lib/ListItemMenu.dart new file mode 100644 index 0000000..ac1e420 --- /dev/null +++ b/covas_mobile/lib/ListItemMenu.dart @@ -0,0 +1,95 @@ +import 'dart:convert'; +import 'package:http/http.dart' as http; +import 'package:flutter/material.dart'; +import 'classes/post.dart'; + +// app starting point +void main() { + runApp(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 { + var url = Uri.parse("https://jsonplaceholder.typicode.com/albums/1/photos"); + final response = + await http.get(url, headers: {"Content-Type": "application/json"}); + final List body = json.decode(response.body); + return body.map((e) => Post.fromJson(e)).toList(); + } + + // 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 ListView.builder( + itemCount: posts.length, + itemBuilder: (context, index) { + final post = posts[index]; + return Container( + color: Colors.grey.shade300, + margin: EdgeInsets.symmetric(vertical: 5, horizontal: 10), + padding: EdgeInsets.symmetric(vertical: 5, horizontal: 5), + height: 100, + width: double.maxFinite, + child: Row( + children: [ + Expanded(flex: 1, child: Image.network(post.url!)), + SizedBox(width: 10), + Expanded(flex: 3, child: Text(post.title!)), + ], + ), + ); + }, + ); + } +} diff --git a/covas_mobile/lib/classes/post.dart b/covas_mobile/lib/classes/post.dart new file mode 100644 index 0000000..69c1727 --- /dev/null +++ b/covas_mobile/lib/classes/post.dart @@ -0,0 +1,17 @@ +class Post { + int? albumId; + int? id; + String? title; + String? url; + String? thumbnailUrl; + + Post({this.albumId, this.id, this.title, this.url, this.thumbnailUrl}); + + Post.fromJson(Map json) { + albumId = json['albumId']; + id = json['id']; + title = json['title']; + url = json['url']; + thumbnailUrl = json['thumbnailUrl']; + } +} diff --git a/covas_mobile/lib/main.dart b/covas_mobile/lib/main.dart index 63d637f..0f7e5f8 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"); }