skantravels/Mobile/lib/main.dart

229 lines
6.5 KiB
Dart
Raw Permalink Normal View History

2024-08-05 08:00:16 +01:00
import 'package:flutter/material.dart';
2024-08-20 10:00:10 +01:00
import 'package:flutter_map/flutter_map.dart';
2024-08-21 11:49:38 +01:00
import 'package:latlong2/latlong.dart';
2024-08-20 15:46:13 +01:00
import 'package:mobile/register.dart';
2024-08-21 11:49:38 +01:00
import 'package:shared_preferences/shared_preferences.dart';
import 'login.dart';
import 'api.dart' as api;
2024-08-21 12:28:48 +01:00
import 'base/sidemenu.dart';
2024-08-20 13:04:40 +01:00
import "login.dart";
2024-08-21 12:28:48 +01:00
import 'profile.dart';
2024-08-05 08:00:16 +01:00
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'H4 Flutter',
theme: ThemeData(
2024-08-20 13:04:40 +01:00
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
2024-08-05 08:00:16 +01:00
useMaterial3: true,
),
2024-08-20 13:04:40 +01:00
home: const MyHomePage(title: 'SkanTravels'),
2024-08-21 12:28:48 +01:00
initialRoute: '/',
routes: {
'/home': (context) => const MyHomePage(
title: 'SkasdanTravels',
),
'/profile': (context) => const ProfilePage(),
'/login': (context) => const LoginPage(title: 'SkanTravels'),
'/register': (context) => const RegisterPage(title: 'SkanTravels'),
},
2024-08-05 08:00:16 +01:00
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
2024-08-21 11:49:38 +01:00
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
2024-08-20 15:46:13 +01:00
int _selectedIndex = 0;
2024-08-21 11:49:38 +01:00
bool _isLoggedIn = false;
2024-08-20 15:46:13 +01:00
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
2024-08-21 11:49:38 +01:00
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<void> _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));
}
2024-08-05 08:00:16 +01:00
@override
Widget build(BuildContext context) {
2024-08-21 12:28:48 +01:00
return SideMenu(
body: Scaffold(
2024-08-21 13:24:53 +01:00
key: _scaffoldKey,
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
drawer: navigationMenu,
2024-08-20 10:00:10 +01:00
body: FlutterMap(
2024-08-20 15:46:13 +01:00
options: const MapOptions(
initialCenter: LatLng(55.9397, 9.5156), initialZoom: 7.0),
2024-08-20 10:00:10 +01:00
children: [
2024-08-21 12:28:48 +01:00
openStreetMapTileLayer,
const MarkerLayer(markers: [
Marker(
point: LatLng(56.465511, 9.411366),
width: 60,
height: 100,
alignment: Alignment.center,
child: Icon(
Icons.location_pin,
size: 60,
color: Colors.purple,
),
2024-08-20 15:46:13 +01:00
),
2024-08-21 12:28:48 +01:00
]),
2024-08-20 10:00:10 +01:00
],
2024-08-05 08:00:16 +01:00
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
2024-08-21 11:49:38 +01:00
children: _isLoggedIn ? [] : [
2024-08-05 08:00:16 +01:00
FloatingActionButton(
2024-08-20 13:04:40 +01:00
onPressed: () {
2024-08-21 11:49:38 +01:00
Navigator.push(context, MaterialPageRoute(builder: (context) => const LoginPage(title: "Login")))
.then(_postNavigationCallback);
2024-08-20 13:04:40 +01:00
},
tooltip: 'Login',
child: const Icon(Icons.login),
2024-08-05 08:00:16 +01:00
),
],
),
2024-08-21 12:28:48 +01:00
),
2024-08-05 08:00:16 +01:00
);
}
2024-08-20 15:46:13 +01:00
Drawer get navigationMenu => Drawer(
2024-08-21 13:24:53 +01:00
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 ? [
2024-08-20 15:46:13 +01:00
ListTile(
2024-08-21 13:24:53 +01:00
title: const Text('Log out'),
leading: const Icon(Icons.logout),
selected: false,
onTap: _logout,
)
] : [
2024-08-20 15:46:13 +01:00
ListTile(
2024-08-21 13:24:53 +01:00
title: const Text('Register'),
leading: const Icon(Icons.add_box_outlined),
selected: _selectedIndex == 3,
2024-08-20 15:46:13 +01:00
onTap: () {
// Update the state of the app
2024-08-21 13:24:53 +01:00
_onItemTapped(3);
2024-08-20 15:46:13 +01:00
// Then close the drawer
2024-08-21 13:24:53 +01:00
Navigator.push(context, MaterialPageRoute(builder: (context) => const RegisterPage(title: 'Register')))
.then(_postNavigationCallback);
2024-08-20 15:46:13 +01:00
},
),
ListTile(
2024-08-21 13:24:53 +01:00
title: const Text('Login'),
leading: const Icon(Icons.login),
selected: _selectedIndex == 4,
2024-08-20 15:46:13 +01:00
onTap: () {
// Update the state of the app
2024-08-21 13:24:53 +01:00
_onItemTapped(4);
2024-08-20 15:46:13 +01:00
// Then close the drawer
2024-08-21 13:24:53 +01:00
Navigator.push(context, MaterialPageRoute(builder: (context) => const LoginPage(title: 'Login')))
.then(_postNavigationCallback);
2024-08-20 15:46:13 +01:00
},
2024-08-21 11:49:38 +01:00
)
]
2024-08-21 13:24:53 +01:00
)
],
),
);
2024-08-21 12:28:48 +01:00
TileLayer get openStreetMapTileLayer => TileLayer(
2024-08-21 13:24:53 +01:00
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName: 'dev.fleaflet.flutter_map.example',
);
2024-08-05 08:00:16 +01:00
}