mobile-flutter/covas_mobile/lib/ListItemMenu.dart

105 lines
3.1 KiB
Dart
Raw Normal View History

2024-06-23 17:23:26 +02:00
import 'dart:convert';
2024-06-24 23:57:43 +02:00
import 'dart:io';
2024-06-23 17:23:26 +02:00
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
2024-06-24 23:57:43 +02:00
import 'classes/events.dart';
2024-06-23 20:57:54 +02:00
import 'package:shared_preferences/shared_preferences.dart';
2024-06-23 17:23:26 +02:00
2024-06-24 23:57:43 +02:00
import 'variable/globals.dart' as globals;
2024-06-23 17:23:26 +02:00
// 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<ListItemMenu> createState() => _MyHomePageState();
}
// homepage state
class _MyHomePageState extends State<ListItemMenu> {
// variable to call and store future list of posts
2024-06-24 23:57:43 +02:00
Future<List<Events>> postsFuture = getPosts();
2024-06-23 17:23:26 +02:00
// function to fetch data from api and return future list of posts
2024-06-24 23:57:43 +02:00
static Future<List<Events>> getPosts() async {
2024-06-23 20:57:54 +02:00
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
2024-06-24 23:57:43 +02:00
final List<Events> body = [];
2024-06-23 20:57:54 +02:00
if (accessToken.isNotEmpty) {
2024-06-24 23:57:43 +02:00
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();
2024-06-23 20:57:54 +02:00
}
return body;
2024-06-23 17:23:26 +02:00
}
// build function
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
// FutureBuilder
2024-06-24 23:57:43 +02:00
child: FutureBuilder<List<Events>>(
2024-06-23 17:23:26 +02:00
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
2024-06-24 23:57:43 +02:00
Widget buildPosts(List<Events> posts) {
2024-06-23 17:23:26 +02:00
// ListView Builder to show data in a list
2024-06-26 22:32:27 +02:00
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"),
),
body: ListView.separated(
itemCount: posts.length,
itemBuilder: (context, index) {
final post = posts[index];
return ListTile(
title: Text('${post.name!}'), subtitle: Text('${post.place!}'));
},
separatorBuilder: (context, index) {
return Divider();
},
));
2024-06-23 17:23:26 +02:00
}
}