fixed double sidemenu. Login/logout dosent work now in sidemenu. moved code from main to sidemenu
This commit is contained in:
parent
46bb1ce74c
commit
ec498eb0f7
@ -1,16 +1,20 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:mobile/api.dart' as api;
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
class SideMenu extends StatefulWidget {
|
class SideMenu extends StatefulWidget {
|
||||||
final Widget body;
|
final Widget body;
|
||||||
|
|
||||||
const SideMenu({Key? key, required this.body}) : super(key: key);
|
const SideMenu({super.key, required this.body});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<SideMenu> createState() => _SideMenuState();
|
State<SideMenu> createState() => _SideMenuState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _SideMenuState extends State<SideMenu> {
|
class _SideMenuState extends State<SideMenu> {
|
||||||
|
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
||||||
int _selectedIndex = 0;
|
int _selectedIndex = 0;
|
||||||
|
bool _isLoggedIn = false;
|
||||||
|
|
||||||
void _onItemTapped(int index) {
|
void _onItemTapped(int index) {
|
||||||
setState(() {
|
setState(() {
|
||||||
@ -18,6 +22,38 @@ class _SideMenuState extends State<SideMenu> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@ -54,7 +90,7 @@ class _SideMenuState extends State<SideMenu> {
|
|||||||
// Update the state of the app
|
// Update the state of the app
|
||||||
_onItemTapped(1);
|
_onItemTapped(1);
|
||||||
// Then close the drawer
|
// Then close the drawer
|
||||||
Navigator.pop(context);
|
Navigator.pushReplacementNamed(context, '/favourites');
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
ListTile(
|
ListTile(
|
||||||
@ -73,6 +109,16 @@ class _SideMenuState extends State<SideMenu> {
|
|||||||
thickness: 2,
|
thickness: 2,
|
||||||
indent: 40,
|
indent: 40,
|
||||||
),
|
),
|
||||||
|
...(_isLoggedIn
|
||||||
|
? [
|
||||||
|
ListTile(
|
||||||
|
title: const Text('Log out'),
|
||||||
|
leading: const Icon(Icons.logout),
|
||||||
|
selected: false,
|
||||||
|
onTap: _logout,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
: [
|
||||||
ListTile(
|
ListTile(
|
||||||
title: const Text('Register'),
|
title: const Text('Register'),
|
||||||
leading: const Icon(Icons.add_box_outlined),
|
leading: const Icon(Icons.add_box_outlined),
|
||||||
@ -94,7 +140,8 @@ class _SideMenuState extends State<SideMenu> {
|
|||||||
// Then close the drawer
|
// Then close the drawer
|
||||||
Navigator.pushReplacementNamed(context, '/login');
|
Navigator.pushReplacementNamed(context, '/login');
|
||||||
},
|
},
|
||||||
),
|
)
|
||||||
|
])
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
15
Mobile/lib/favourites.dart
Normal file
15
Mobile/lib/favourites.dart
Normal file
@ -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'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -5,9 +5,7 @@ import 'register.dart';
|
|||||||
import 'api.dart' as api;
|
import 'api.dart' as api;
|
||||||
|
|
||||||
class LoginPage extends StatefulWidget {
|
class LoginPage extends StatefulWidget {
|
||||||
const LoginPage({super.key, required this.title});
|
const LoginPage({super.key});
|
||||||
|
|
||||||
final String title;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<LoginPage> createState() => _LoginPageState();
|
State<LoginPage> createState() => _LoginPageState();
|
||||||
@ -42,8 +40,7 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
body: Scaffold(
|
body: Scaffold(
|
||||||
body: Center(
|
body: Center(
|
||||||
child: Container(
|
child: Container(
|
||||||
constraints:
|
constraints: const BoxConstraints(minWidth: 100, maxWidth: 400),
|
||||||
const BoxConstraints(minWidth: 100, maxWidth: 400),
|
|
||||||
child: Column(children: [
|
child: Column(children: [
|
||||||
const SizedBox(height: 80),
|
const SizedBox(height: 80),
|
||||||
const Text('Email'),
|
const Text('Email'),
|
||||||
@ -60,13 +57,10 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
const SizedBox(height: 10),
|
const SizedBox(height: 10),
|
||||||
TextButton(
|
TextButton(
|
||||||
child: const Text('Register account'),
|
child: const Text('Register account'),
|
||||||
onPressed: () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const RegisterPage(title: 'Register')))),
|
onPressed: () => Navigator.pushReplacement(
|
||||||
ElevatedButton(onPressed: _login, child: const Text('Log ind')),
|
context,
|
||||||
const SizedBox(height: 10),
|
MaterialPageRoute(
|
||||||
TextButton(
|
builder: (context) => const RegisterPage()))),
|
||||||
child: const Text('Registrer konto'),
|
|
||||||
onPressed: () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const RegisterPage(title: 'Registrer'))),
|
|
||||||
),
|
|
||||||
]),
|
]),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_map/flutter_map.dart';
|
import 'package:flutter_map/flutter_map.dart';
|
||||||
import 'package:latlong2/latlong.dart';
|
import 'package:latlong2/latlong.dart';
|
||||||
|
import 'package:mobile/favourites.dart';
|
||||||
import 'package:mobile/register.dart';
|
import 'package:mobile/register.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
import 'login.dart';
|
import 'login.dart';
|
||||||
import 'api.dart' as api;
|
|
||||||
import 'base/sidemenu.dart';
|
import 'base/sidemenu.dart';
|
||||||
import "login.dart";
|
|
||||||
import 'profile.dart';
|
import 'profile.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@ -23,24 +21,21 @@ class MyApp extends StatelessWidget {
|
|||||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
),
|
),
|
||||||
home: const MyHomePage(title: 'SkanTravels'),
|
home: const MyHomePage(),
|
||||||
initialRoute: '/',
|
initialRoute: '/',
|
||||||
routes: {
|
routes: {
|
||||||
'/home': (context) => const MyHomePage(
|
'/home': (context) => const MyHomePage(),
|
||||||
title: 'SkasdanTravels',
|
|
||||||
),
|
|
||||||
'/profile': (context) => const ProfilePage(),
|
'/profile': (context) => const ProfilePage(),
|
||||||
'/login': (context) => const LoginPage(title: 'SkanTravels'),
|
'/favourites': (context) => const FavouritesPage(),
|
||||||
'/register': (context) => const RegisterPage(title: 'SkanTravels'),
|
'/login': (context) => const LoginPage(),
|
||||||
|
'/register': (context) => const RegisterPage(),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyHomePage extends StatefulWidget {
|
class MyHomePage extends StatefulWidget {
|
||||||
const MyHomePage({super.key, required this.title});
|
const MyHomePage({super.key});
|
||||||
|
|
||||||
final String title;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<MyHomePage> createState() => _MyHomePageState();
|
State<MyHomePage> createState() => _MyHomePageState();
|
||||||
@ -48,55 +43,12 @@ class MyHomePage extends StatefulWidget {
|
|||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
||||||
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<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));
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return SideMenu(
|
return SideMenu(
|
||||||
body: Scaffold(
|
body: Scaffold(
|
||||||
key: _scaffoldKey,
|
key: _scaffoldKey,
|
||||||
appBar: AppBar(
|
//drawer: navigationMenu,
|
||||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
||||||
title: Text(widget.title),
|
|
||||||
),
|
|
||||||
drawer: navigationMenu,
|
|
||||||
body: FlutterMap(
|
body: FlutterMap(
|
||||||
options: const MapOptions(
|
options: const MapOptions(
|
||||||
initialCenter: LatLng(55.9397, 9.5156), initialZoom: 7.0),
|
initialCenter: LatLng(55.9397, 9.5156), initialZoom: 7.0),
|
||||||
@ -117,110 +69,10 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
]),
|
]),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
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(
|
TileLayer get openStreetMapTileLayer => TileLayer(
|
||||||
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
|
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
|
||||||
userAgentPackageName: 'dev.fleaflet.flutter_map.example',
|
userAgentPackageName: 'dev.fleaflet.flutter_map.example',
|
||||||
|
@ -4,9 +4,7 @@ import 'login.dart';
|
|||||||
import 'api.dart' as api;
|
import 'api.dart' as api;
|
||||||
|
|
||||||
class RegisterPage extends StatefulWidget {
|
class RegisterPage extends StatefulWidget {
|
||||||
const RegisterPage({super.key, required this.title});
|
const RegisterPage({super.key});
|
||||||
|
|
||||||
final String title;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<RegisterPage> createState() => _RegisterPageState();
|
State<RegisterPage> createState() => _RegisterPageState();
|
||||||
@ -31,9 +29,7 @@ class _RegisterPageState extends State<RegisterPage> {
|
|||||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
|
||||||
content: Text('Successfully registered, please login')));
|
content: Text('Successfully registered, please login')));
|
||||||
Navigator.pushReplacement(
|
Navigator.pushReplacement(
|
||||||
context,
|
context, MaterialPageRoute(builder: (context) => const LoginPage()));
|
||||||
MaterialPageRoute(
|
|
||||||
builder: (context) => const LoginPage(title: 'Log ind')));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,8 +39,7 @@ class _RegisterPageState extends State<RegisterPage> {
|
|||||||
body: Scaffold(
|
body: Scaffold(
|
||||||
body: Center(
|
body: Center(
|
||||||
child: Container(
|
child: Container(
|
||||||
constraints:
|
constraints: const BoxConstraints(minWidth: 100, maxWidth: 400),
|
||||||
const BoxConstraints(minWidth: 100, maxWidth: 400),
|
|
||||||
child: Column(children: [
|
child: Column(children: [
|
||||||
const SizedBox(height: 80),
|
const SizedBox(height: 80),
|
||||||
const Text('Username'),
|
const Text('Username'),
|
||||||
@ -60,12 +55,15 @@ class _RegisterPageState extends State<RegisterPage> {
|
|||||||
enableSuggestions: false,
|
enableSuggestions: false,
|
||||||
autocorrect: false),
|
autocorrect: false),
|
||||||
const SizedBox(height: 30),
|
const SizedBox(height: 30),
|
||||||
ElevatedButton(onPressed: _register, child: const Text('Register')),
|
ElevatedButton(
|
||||||
|
onPressed: _register, child: const Text('Register')),
|
||||||
const SizedBox(height: 10),
|
const SizedBox(height: 10),
|
||||||
TextButton(
|
TextButton(
|
||||||
child: const Text('Login'),
|
child: const Text('Login'),
|
||||||
onPressed: () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const LoginPage(title: 'Login')))
|
onPressed: () => Navigator.pushReplacement(
|
||||||
),
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (context) => const LoginPage()))),
|
||||||
]),
|
]),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
Loading…
Reference in New Issue
Block a user