Add favorite menu functionality

Co-authored-by: Reimar <mail@reim.ar>
This commit is contained in:
Sandertp 2024-08-22 12:14:06 +02:00
parent 67f3cd118f
commit 158bdd91a9
5 changed files with 50 additions and 20 deletions

View File

@ -83,6 +83,7 @@ Future<bool> isLoggedIn(BuildContext context) async {
} catch (e) {
messenger.showSnackBar(const SnackBar(content: Text('Invalid token, please sign in again')));
prefs.remove('token');
debugPrint(e.toString());
return false;
}

View File

@ -83,14 +83,14 @@ class _SideMenuState extends State<SideMenu> {
},
),
ListTile(
title: const Text('Favourites'),
title: const Text('Favorites'),
leading: const Icon(Icons.star),
selected: _selectedIndex == 1,
onTap: () {
// Update the state of the app
_onItemTapped(1);
// Then close the drawer
Navigator.pushReplacementNamed(context, '/favourites');
Navigator.pushReplacementNamed(context, '/favorites');
},
),
ListTile(

45
Mobile/lib/favorites.dart Normal file
View File

@ -0,0 +1,45 @@
import 'dart:convert';
import 'api.dart' as api;
import 'package:flutter/material.dart';
import 'base/sidemenu.dart'; // Import the base layout widget
import 'models.dart';
class FavoritesPage extends StatefulWidget {
const FavoritesPage({super.key});
@override
State<FavoritesPage> createState() => _FavoritesPage();
}
class _FavoritesPage extends State<FavoritesPage> {
List<Favorite> _favorites = [];
@override
void didChangeDependencies() {
super.didChangeDependencies();
api.isLoggedIn(context).then((isLoggedIn) async {
if (!isLoggedIn || !mounted) return;
final response = await api.request(context, api.ApiService.app, 'GET', '/favorites', null);
if (response == null) return;
final List<dynamic> favorites = jsonDecode(response);
setState(() {
_favorites = favorites.map((favorite) => Favorite(favorite['id'], favorite['user_id'], favorite['lat'], favorite['lng'])).toList();
});
});
}
@override
Widget build(BuildContext context) {
return SideMenu(
body: Container(
padding: const EdgeInsets.all(20.0),
child: Column(children:
_favorites.map((favorite) => Text("${favorite.lat} ${favorite.lng}")).toList(),
),
),
);
}
}

View File

@ -1,15 +0,0 @@
import 'package:flutter/material.dart';
import 'base/sidemenu.dart'; // Import the base layout widget
class FavouritesPage extends StatelessWidget {
const FavouritesPage({super.key});
@override
Widget build(BuildContext context) {
return const SideMenu(
body: Center(
child: Text('This is Page 1'),
),
);
}
}

View File

@ -4,7 +4,7 @@ import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import 'package:mobile/favourites.dart';
import 'package:mobile/favorites.dart';
import 'package:mobile/register.dart';
import 'login.dart';
import 'base/sidemenu.dart';
@ -31,7 +31,7 @@ class MyApp extends StatelessWidget {
routes: {
'/home': (context) => const MyHomePage(),
'/profile': (context) => const ProfilePage(),
'/favourites': (context) => const FavouritesPage(),
'/favorites': (context) => const FavoritesPage(),
'/login': (context) => const LoginPage(),
'/register': (context) => const RegisterPage(),
},
@ -59,7 +59,6 @@ class _MyHomePageState extends State<MyHomePage> {
final response = await api.request(context, api.ApiService.app, 'GET', '/favorites', null);
if (response == null) return;
log(response);
final List<dynamic> favorites = jsonDecode(response);
setState(() {