diff --git a/Mobile/lib/base/sidemenu.dart b/Mobile/lib/base/sidemenu.dart index 3683d8f..7f74d20 100644 --- a/Mobile/lib/base/sidemenu.dart +++ b/Mobile/lib/base/sidemenu.dart @@ -1,16 +1,20 @@ import 'package:flutter/material.dart'; +import 'package:mobile/api.dart' as api; +import 'package:shared_preferences/shared_preferences.dart'; class SideMenu extends StatefulWidget { final Widget body; - const SideMenu({Key? key, required this.body}) : super(key: key); + const SideMenu({super.key, required this.body}); @override State createState() => _SideMenuState(); } class _SideMenuState extends State { + final GlobalKey _scaffoldKey = GlobalKey(); int _selectedIndex = 0; + bool _isLoggedIn = false; void _onItemTapped(int index) { setState(() { @@ -18,6 +22,38 @@ class _SideMenuState extends State { }); } + Future _postNavigationCallback(dynamic _) async { + final isLoggedIn = await api.isLoggedIn(context); + setState(() => _isLoggedIn = isLoggedIn); + + // Close sidebar + if (mounted && _scaffoldKey.currentState?.isDrawerOpen == true) { + Navigator.pop(context); + } + } + + void _logout() async { + final prefs = await SharedPreferences.getInstance(); + + prefs.remove('token'); + setState(() => _isLoggedIn = false); + + if (mounted) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Successfully logged out'))); + Navigator.pop(context); + } + } + + @override + void didChangeDependencies() { + super.didChangeDependencies(); + + api + .isLoggedIn(context) + .then((value) => setState(() => _isLoggedIn = value)); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -54,7 +90,7 @@ class _SideMenuState extends State { // Update the state of the app _onItemTapped(1); // Then close the drawer - Navigator.pop(context); + Navigator.pushReplacementNamed(context, '/favourites'); }, ), ListTile( @@ -73,28 +109,39 @@ class _SideMenuState extends State { thickness: 2, indent: 40, ), - ListTile( - title: const Text('Register'), - leading: const Icon(Icons.add_box_outlined), - selected: _selectedIndex == 3, - onTap: () { - // Update the state of the app - _onItemTapped(3); - // Then close the drawer - Navigator.pushReplacementNamed(context, '/register'); - }, - ), - ListTile( - title: const Text('Login'), - leading: const Icon(Icons.login), - selected: _selectedIndex == 4, - onTap: () { - // Update the state of the app - _onItemTapped(4); - // Then close the drawer - Navigator.pushReplacementNamed(context, '/login'); - }, - ), + ...(_isLoggedIn + ? [ + ListTile( + title: const Text('Log out'), + leading: const Icon(Icons.logout), + selected: false, + onTap: _logout, + ) + ] + : [ + ListTile( + title: const Text('Register'), + leading: const Icon(Icons.add_box_outlined), + selected: _selectedIndex == 3, + onTap: () { + // Update the state of the app + _onItemTapped(3); + // Then close the drawer + Navigator.pushReplacementNamed(context, '/register'); + }, + ), + ListTile( + title: const Text('Login'), + leading: const Icon(Icons.login), + selected: _selectedIndex == 4, + onTap: () { + // Update the state of the app + _onItemTapped(4); + // Then close the drawer + Navigator.pushReplacementNamed(context, '/login'); + }, + ) + ]) ], ), ), diff --git a/Mobile/lib/favourites.dart b/Mobile/lib/favourites.dart new file mode 100644 index 0000000..fd01f2a --- /dev/null +++ b/Mobile/lib/favourites.dart @@ -0,0 +1,15 @@ +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'), + ), + ); + } +} diff --git a/Mobile/lib/login.dart b/Mobile/lib/login.dart index fa40de9..13b6f46 100644 --- a/Mobile/lib/login.dart +++ b/Mobile/lib/login.dart @@ -5,9 +5,7 @@ import 'register.dart'; import 'api.dart' as api; class LoginPage extends StatefulWidget { - const LoginPage({super.key, required this.title}); - - final String title; + const LoginPage({super.key}); @override State createState() => _LoginPageState(); @@ -42,31 +40,27 @@ class _LoginPageState extends State { body: Scaffold( body: Center( child: Container( - constraints: - const BoxConstraints(minWidth: 100, maxWidth: 400), + constraints: const BoxConstraints(minWidth: 100, maxWidth: 400), child: Column(children: [ const SizedBox(height: 80), const Text('Email'), TextField(controller: emailInput), const SizedBox(height: 30), const Text('Password'), - TextField( - controller: passwordInput, - obscureText: true, - enableSuggestions: false, - autocorrect: false), + TextField( + controller: passwordInput, + obscureText: true, + enableSuggestions: false, + autocorrect: false), const SizedBox(height: 30), ElevatedButton(onPressed: _login, child: const Text('Login')), const SizedBox(height: 10), TextButton( - child: const Text('Register account'), - onPressed: () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const RegisterPage(title: 'Register')))), - ElevatedButton(onPressed: _login, child: const Text('Log ind')), - const SizedBox(height: 10), - TextButton( - child: const Text('Registrer konto'), - onPressed: () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const RegisterPage(title: 'Registrer'))), - ), + child: const Text('Register account'), + onPressed: () => Navigator.pushReplacement( + context, + MaterialPageRoute( + builder: (context) => const RegisterPage()))), ]), ), ), diff --git a/Mobile/lib/main.dart b/Mobile/lib/main.dart index bc8ef5c..65f5548 100644 --- a/Mobile/lib/main.dart +++ b/Mobile/lib/main.dart @@ -1,12 +1,10 @@ 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/register.dart'; -import 'package:shared_preferences/shared_preferences.dart'; import 'login.dart'; -import 'api.dart' as api; import 'base/sidemenu.dart'; -import "login.dart"; import 'profile.dart'; void main() { @@ -23,24 +21,21 @@ class MyApp extends StatelessWidget { colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue), useMaterial3: true, ), - home: const MyHomePage(title: 'SkanTravels'), + home: const MyHomePage(), initialRoute: '/', routes: { - '/home': (context) => const MyHomePage( - title: 'SkasdanTravels', - ), + '/home': (context) => const MyHomePage(), '/profile': (context) => const ProfilePage(), - '/login': (context) => const LoginPage(title: 'SkanTravels'), - '/register': (context) => const RegisterPage(title: 'SkanTravels'), + '/favourites': (context) => const FavouritesPage(), + '/login': (context) => const LoginPage(), + '/register': (context) => const RegisterPage(), }, ); } } class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); - - final String title; + const MyHomePage({super.key}); @override State createState() => _MyHomePageState(); @@ -48,59 +43,16 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { final GlobalKey _scaffoldKey = GlobalKey(); - int _selectedIndex = 0; - bool _isLoggedIn = false; - - void _onItemTapped(int index) { - setState(() { - _selectedIndex = index; - }); - } - - void _logout() async { - final prefs = await SharedPreferences.getInstance(); - - prefs.remove('token'); - setState(() => _isLoggedIn = false); - - if (mounted) { - ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Successfully logged out'))); - Navigator.pop(context); - } - } - - Future _postNavigationCallback(dynamic _) async { - final isLoggedIn = await api.isLoggedIn(context); - setState(() => _isLoggedIn = isLoggedIn); - - // Close sidebar - if (mounted && _scaffoldKey.currentState?.isDrawerOpen == true) { - Navigator.pop(context); - } - } - - @override - void didChangeDependencies() { - super.didChangeDependencies(); - - api.isLoggedIn(context) - .then((value) => setState(() => _isLoggedIn = value)); - } - @override Widget build(BuildContext context) { return SideMenu( body: Scaffold( key: _scaffoldKey, - appBar: AppBar( - backgroundColor: Theme.of(context).colorScheme.inversePrimary, - title: Text(widget.title), - ), - drawer: navigationMenu, - body: FlutterMap( - options: const MapOptions( - initialCenter: LatLng(55.9397, 9.5156), initialZoom: 7.0), - children: [ + //drawer: navigationMenu, + body: FlutterMap( + options: const MapOptions( + initialCenter: LatLng(55.9397, 9.5156), initialZoom: 7.0), + children: [ openStreetMapTileLayer, const MarkerLayer(markers: [ Marker( @@ -115,114 +67,14 @@ class _MyHomePageState extends State { ), ), ]), - ], - ), - floatingActionButton: Row( - mainAxisAlignment: MainAxisAlignment.end, - children: _isLoggedIn ? [] : [ - FloatingActionButton( - onPressed: () { - Navigator.push(context, MaterialPageRoute(builder: (context) => const LoginPage(title: "Login"))) - .then(_postNavigationCallback); - }, - tooltip: 'Login', - child: const Icon(Icons.login), - ), - ], - ), + ], + ), ), ); } - Drawer get navigationMenu => Drawer( - child: ListView( - padding: EdgeInsets.zero, - children: [ - const DrawerHeader( - decoration: BoxDecoration( - color: Colors.blue, - ), - child: Text('Drawer Header'), - ), - ListTile( - title: const Text('Home'), - leading: const Icon(Icons.home), - selected: _selectedIndex == 0, - onTap: () { - // Update the state of the app - _onItemTapped(0); - // Then close the drawer - Navigator.pop(context); - }, - ), - ListTile( - title: const Text('Favourites'), - leading: const Icon(Icons.star), - selected: _selectedIndex == 1, - onTap: () { - // Update the state of the app - _onItemTapped(1); - // Then close the drawer - Navigator.pop(context); - }, - ), - ListTile( - title: const Text('Profile'), - leading: const Icon(Icons.person), - selected: _selectedIndex == 2, - onTap: () { - // Update the state of the app - _onItemTapped(2); - // Then close the drawer - Navigator.pop(context); - }, - ), - const Divider( - color: Colors.grey, - thickness: 2, - indent: 40, - ), - ...( - _isLoggedIn ? [ - ListTile( - title: const Text('Log out'), - leading: const Icon(Icons.logout), - selected: false, - onTap: _logout, - ) - ] : [ - ListTile( - title: const Text('Register'), - leading: const Icon(Icons.add_box_outlined), - selected: _selectedIndex == 3, - onTap: () { - // Update the state of the app - _onItemTapped(3); - // Then close the drawer - Navigator.push(context, MaterialPageRoute(builder: (context) => const RegisterPage(title: 'Register'))) - .then(_postNavigationCallback); - }, - ), - ListTile( - title: const Text('Login'), - leading: const Icon(Icons.login), - selected: _selectedIndex == 4, - onTap: () { - // Update the state of the app - _onItemTapped(4); - // Then close the drawer - Navigator.push(context, MaterialPageRoute(builder: (context) => const LoginPage(title: 'Login'))) - .then(_postNavigationCallback); - }, - ) - ] - ) - ], - ), - ); - TileLayer get openStreetMapTileLayer => TileLayer( - urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png', - userAgentPackageName: 'dev.fleaflet.flutter_map.example', - ); + urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png', + userAgentPackageName: 'dev.fleaflet.flutter_map.example', + ); } diff --git a/Mobile/lib/register.dart b/Mobile/lib/register.dart index 9b04366..2a92c34 100644 --- a/Mobile/lib/register.dart +++ b/Mobile/lib/register.dart @@ -4,9 +4,7 @@ import 'login.dart'; import 'api.dart' as api; class RegisterPage extends StatefulWidget { - const RegisterPage({super.key, required this.title}); - - final String title; + const RegisterPage({super.key}); @override State createState() => _RegisterPageState(); @@ -31,9 +29,7 @@ class _RegisterPageState extends State { ScaffoldMessenger.of(context).showSnackBar(const SnackBar( content: Text('Successfully registered, please login'))); Navigator.pushReplacement( - context, - MaterialPageRoute( - builder: (context) => const LoginPage(title: 'Log ind'))); + context, MaterialPageRoute(builder: (context) => const LoginPage())); } } @@ -43,8 +39,7 @@ class _RegisterPageState extends State { body: Scaffold( body: Center( child: Container( - constraints: - const BoxConstraints(minWidth: 100, maxWidth: 400), + constraints: const BoxConstraints(minWidth: 100, maxWidth: 400), child: Column(children: [ const SizedBox(height: 80), const Text('Username'), @@ -55,17 +50,20 @@ class _RegisterPageState extends State { const SizedBox(height: 30), const Text('Password'), TextField( - controller: passwordInput, - obscureText: true, - enableSuggestions: false, - autocorrect: false), + controller: passwordInput, + obscureText: true, + enableSuggestions: false, + autocorrect: false), const SizedBox(height: 30), - ElevatedButton(onPressed: _register, child: const Text('Register')), + ElevatedButton( + onPressed: _register, child: const Text('Register')), const SizedBox(height: 10), TextButton( - child: const Text('Login'), - onPressed: () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const LoginPage(title: 'Login'))) - ), + child: const Text('Login'), + onPressed: () => Navigator.pushReplacement( + context, + MaterialPageRoute( + builder: (context) => const LoginPage()))), ]), ), ),