skantravels/Mobile/lib/base/sidemenu.dart

153 lines
4.4 KiB
Dart
Raw Normal View History

2024-08-21 12:28:48 +01:00
import 'package:flutter/material.dart';
2024-09-13 08:59:21 +01:00
import 'package:mobile/services/api.dart' as api;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:google_fonts/google_fonts.dart';
import 'variables.dart';
2024-08-21 12:28:48 +01:00
class SideMenu extends StatefulWidget {
final Widget body;
2024-08-26 19:53:47 +01:00
final int selectedIndex;
2024-08-21 12:28:48 +01:00
const SideMenu({super.key, required this.body, required this.selectedIndex});
2024-08-21 12:28:48 +01:00
@override
State<SideMenu> createState() => _SideMenuState();
}
class _SideMenuState extends State<SideMenu> {
late int _selectedIndex;
2024-08-27 07:40:54 +01:00
bool _isLoggedIn = false;
@override
void initState() {
super.initState();
_selectedIndex = widget.selectedIndex; // Initialize _selectedIndex with the value from the widget
}
void _logout() async {
2024-09-02 12:14:13 +01:00
api.logout();
2024-08-26 19:53:47 +01:00
setState(() {
user = null;
2024-08-26 19:53:47 +01:00
});
if (mounted) {
2024-08-26 19:53:47 +01:00
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Successfully logged out')));
Navigator.pushReplacementNamed(context, '/login');
}
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
2024-08-26 19:53:47 +01:00
api.isLoggedIn(context).then((value) {
setState(() {
2024-08-27 07:40:54 +01:00
_isLoggedIn = value;
2024-08-26 19:53:47 +01:00
});
});
}
2024-08-21 12:28:48 +01:00
@override
2024-08-27 07:21:15 +01:00
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text('SkanTravels',
style: GoogleFonts.jacquesFrancois(
fontSize: 30,
color: Colors.black,
),
2024-08-27 07:21:15 +01:00
),
2024-08-21 12:28:48 +01:00
),
2024-08-27 07:21:15 +01:00
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
DrawerHeader(
child: Column(
children: [
const Image(image: AssetImage('assets/logo.png'), height: 100),
Text(
'SkanTravels',
style: GoogleFonts.jacquesFrancois(
fontSize: 20,
color: const Color(0xFF1862E7),
),
2024-08-26 19:53:47 +01:00
),
2024-08-27 07:21:15 +01:00
],
),
2024-08-26 19:53:47 +01:00
),
2024-08-27 07:21:15 +01:00
ListTile(
title: const Text('Home'),
leading: const Icon(Icons.home),
selected: _selectedIndex == 0,
onTap: () {
Navigator.pushReplacementNamed(context, '/home');
},
),
2024-08-27 12:06:31 +01:00
...(_isLoggedIn
? [
2024-08-27 07:21:15 +01:00
ListTile(
title: const Text('Favorites'),
leading: const Icon(Icons.star),
selected: _selectedIndex == 1,
onTap: () {
Navigator.pushReplacementNamed(context, '/favorites');
},
),
2024-09-13 08:59:21 +01:00
ListTile(
2024-08-27 07:21:15 +01:00
title: const Text('Profile'),
leading: const Icon(Icons.person),
selected: _selectedIndex == 2,
onTap: () {
Navigator.pushReplacementNamed(context, '/profile');
},
),
2024-09-13 08:59:21 +01:00
ListTile(
title: const Text('GuideBook'),
leading: const Icon(Icons.person),
selected: _selectedIndex == 5,
onTap: () {
Navigator.pushReplacementNamed(context, '/tourist-guide-book');
},
),
2024-08-27 07:21:15 +01:00
const Divider(
color: Colors.grey,
thickness: 2,
indent: 40,
),
2024-08-27 12:06:31 +01:00
2024-08-27 07:21:15 +01:00
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: () {
Navigator.pushReplacementNamed(context, '/register');
},
),
ListTile(
title: const Text('Login'),
leading: const Icon(Icons.login),
selected: _selectedIndex == 4,
onTap: () {
Navigator.pushReplacementNamed(context, '/login');
},
),
]
),
],
),
2024-08-21 12:28:48 +01:00
),
2024-08-27 07:21:15 +01:00
body: widget.body,
);
}
}