146 lines
4.6 KiB
Dart
Raw Normal View History

2024-12-30 22:14:46 +01:00
import 'package:flutter/material.dart';
2024-12-30 23:33:51 +01:00
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import 'dart:io';
2025-01-04 14:25:34 +01:00
import '../pages/EditProfile.dart';
2025-01-09 23:27:22 +01:00
import '../pages/EditSettings.dart';
2025-01-09 21:35:31 +01:00
import '../pages/ListItemMenu.dart';
2024-12-30 22:51:47 +01:00
import 'alert.dart';
2024-12-30 22:14:46 +01:00
2024-12-30 23:33:51 +01:00
import '../variable/globals.dart' as globals;
import '../main.dart';
2024-12-30 22:51:47 +01:00
class MyDrawer extends StatelessWidget with ShowAlertDialog {
2024-12-30 23:33:51 +01:00
Future<void> logout(BuildContext context) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
if (accessToken.isNotEmpty) {
var url = Uri.parse("${globals.api}/token");
try {
var response = await http.delete(url, headers: {
"Content-Type": "application/json",
HttpHeaders.cookieHeader: "access_token=${accessToken}"
});
print("Status code logout ${response.statusCode}");
if (response.statusCode == 200) {
await prefs.setString("access_token", ""); // Clear the token
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (_) => LoginDemo()));
} else {
switch (response.statusCode) {
case 400:
print("Bad Request: Please check your input.");
showAlertDialog(
context, "Bad Request", "Please check your input.");
break;
case 401:
print("Unauthorized: Invalid credentials.");
showAlertDialog(context, "Unauthorized", "Invalid credentials.");
break;
case 403:
print("Forbidden: You don't have permission.");
showAlertDialog(context, "Forbidden",
"You don't have permission to access this.");
break;
case 404:
print("Not Found: The resource was not found.");
showAlertDialog(
context, "Not Found", "The resource was not found.");
break;
case 500:
print("Server Error: Something went wrong on the server.");
showAlertDialog(context, "Server Error",
"Something went wrong on the server.");
break;
default:
print("Unexpected Error: ${response.statusCode}");
showAlertDialog(context, "Error", "Unexpected Error occurred.");
break;
}
}
} catch (e) {
print("Error: $e");
showAlertDialog(
context, "Error", "An error occurred. Please try again.");
}
} else {
showAlertDialog(context, "Error", "Token invalide.");
}
}
2024-12-30 22:14:46 +01:00
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
// Drawer Header
DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text(
'Menu',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
// Drawer Items
ListTile(
leading: Icon(Icons.home),
title: Text('Home'),
onTap: () {
2025-01-09 21:35:31 +01:00
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (_) => ListItemMenu()));
/// Close the drawer
2024-12-30 22:14:46 +01:00
},
),
ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),
onTap: () {
2025-01-09 23:27:22 +01:00
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (_) => EditSettings())); // Close the drawer
2024-12-30 22:14:46 +01:00
},
),
2024-12-30 23:48:03 +01:00
ListTile(
leading: Icon(Icons.account_circle),
title: Text('Update profile'),
onTap: () {
2025-01-04 14:25:34 +01:00
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (_) => EditProfile())); // Close the drawer
2024-12-30 23:48:03 +01:00
},
),
2024-12-30 22:14:46 +01:00
ListTile(
leading: Icon(Icons.info),
title: Text('About'),
onTap: () {
2024-12-30 22:51:47 +01:00
showAlertDialog(
context, 'About', "Version 0.0.1"); // Close the drawer
2024-12-30 22:14:46 +01:00
},
),
2024-12-30 23:33:51 +01:00
ListTile(
leading: Icon(Icons.logout),
title: Text('Log out'),
onTap: () async {
logout(context);
// Close the drawer
},
),
2024-12-30 22:14:46 +01:00
],
),
);
}
}