diff --git a/covas_mobile/lib/ListItemMenu.dart b/covas_mobile/lib/ListItemMenu.dart index 4946182..ee83305 100644 --- a/covas_mobile/lib/ListItemMenu.dart +++ b/covas_mobile/lib/ListItemMenu.dart @@ -1,9 +1,12 @@ import 'dart:convert'; +import 'dart:io'; import 'package:http/http.dart' as http; import 'package:flutter/material.dart'; -import 'classes/post.dart'; +import 'classes/events.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'variable/globals.dart' as globals; + // app starting point void main() { runApp(const MyApp()); @@ -32,20 +35,21 @@ class ListItemMenu extends StatefulWidget { // homepage state class _MyHomePageState extends State { // variable to call and store future list of posts - Future> postsFuture = getPosts(); + Future> postsFuture = getPosts(); // function to fetch data from api and return future list of posts - static Future> getPosts() async { + static Future> getPosts() async { SharedPreferences prefs = await SharedPreferences.getInstance(); var accessToken = prefs.getString("access_token") ?? ""; - final List body = []; + final List body = []; if (accessToken.isNotEmpty) { - 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(); + 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; } @@ -56,7 +60,7 @@ class _MyHomePageState extends State { return Scaffold( body: Center( // FutureBuilder - child: FutureBuilder>( + child: FutureBuilder>( future: postsFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { @@ -77,7 +81,7 @@ class _MyHomePageState extends State { } // function to display fetched data on screen - Widget buildPosts(List posts) { + Widget buildPosts(List posts) { // ListView Builder to show data in a list return ListView.builder( itemCount: posts.length, @@ -85,15 +89,20 @@ class _MyHomePageState extends State { final post = posts[index]; return Container( color: Colors.grey.shade300, - margin: EdgeInsets.symmetric(vertical: 5, horizontal: 10), + margin: EdgeInsets.symmetric(vertical: 5, horizontal: 5), 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!)), + Text( + '${post.name!}\n\n', + style: Theme.of(context).textTheme.titleMedium, + ), + Text( + '${post.place!}', + style: Theme.of(context).textTheme.titleSmall, + ), ], ), ); diff --git a/covas_mobile/lib/classes/events.dart b/covas_mobile/lib/classes/events.dart new file mode 100644 index 0000000..f40e1b9 --- /dev/null +++ b/covas_mobile/lib/classes/events.dart @@ -0,0 +1,13 @@ +class Events { + String? id; + String? name; + String? place; + + Events({this.place, this.id, this.name}); + + Events.fromJson(Map json) { + id = json['id']; + name = json['name']; + place = json['place']; + } +}