import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; import 'dart:io'; import '../pages/EditProfile.dart'; import '../pages/EditSettings.dart'; import '../pages/ListItemMenu.dart'; import 'alert.dart'; import '../variable/globals.dart' as globals; import '../main.dart'; class MyDrawer extends StatelessWidget with ShowAlertDialog { Future 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."); } } @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: () { Navigator.pushReplacement( context, MaterialPageRoute(builder: (_) => ListItemMenu())); /// Close the drawer }, ), ListTile( leading: Icon(Icons.settings), title: Text('Settings'), onTap: () { Navigator.pushReplacement( context, MaterialPageRoute( builder: (_) => EditSettings())); // Close the drawer }, ), ListTile( leading: Icon(Icons.account_circle), title: Text('Update profile'), onTap: () { Navigator.pushReplacement( context, MaterialPageRoute( builder: (_) => EditProfile())); // Close the drawer }, ), ListTile( leading: Icon(Icons.info), title: Text('About'), onTap: () { showAlertDialog( context, 'About', "Version 0.0.1"); // Close the drawer }, ), ListTile( leading: Icon(Icons.logout), title: Text('Log out'), onTap: () async { logout(context); // Close the drawer }, ), ], ), ); } }