219 lines
6.2 KiB
Dart
219 lines
6.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:responsive_builder/responsive_builder.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
class CustomDrawer extends StatefulWidget{
|
|
|
|
final bool isDesktop;
|
|
const CustomDrawer({super.key, required this.isDesktop});
|
|
|
|
@override
|
|
_CustomDrawerState createState() => _CustomDrawerState();
|
|
|
|
}
|
|
|
|
class _CustomDrawerState extends State<CustomDrawer>{
|
|
|
|
String? token;
|
|
Map<String,dynamic>? userData;
|
|
Map<String, dynamic>? fetchedUserData;
|
|
Map<String, dynamic> userDetails = {};
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
initializeData();
|
|
}
|
|
|
|
Future<void> initializeData() async{
|
|
|
|
print("initializeDatainitializeData");
|
|
token = await getToken();
|
|
fetchedUserData = await getUserData();
|
|
|
|
if(token == null || fetchedUserData == null)
|
|
{
|
|
print("Token or USerId missing");
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
userData = fetchedUserData;
|
|
});
|
|
|
|
}
|
|
|
|
|
|
Future<String?> getToken() async{
|
|
final prefs = await SharedPreferences.getInstance();
|
|
return prefs.getString("auth_token");
|
|
}
|
|
|
|
Future <Map<String,dynamic>?> getUserData() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final String? userDataString = prefs.getString('user_data');
|
|
|
|
if(userDataString != null){
|
|
try{
|
|
userDetails = jsonDecode(userDataString);
|
|
|
|
return{
|
|
"user_id" : userDetails["user_id"].toString(),
|
|
"name" : "${userDetails["first_name"]} ${userDetails["last_name"]}",
|
|
"email" : userDetails["email"] ?? "",
|
|
};
|
|
}catch (e) {
|
|
print("Error decoding user data: $e");
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
Widget build(BuildContext context){
|
|
Widget drawerContent = Container(
|
|
color: Color(0xFFF3F3FA),
|
|
child: Column(
|
|
children: [
|
|
GestureDetector(
|
|
onTap:(){
|
|
print("ONTAP Custom");
|
|
context.go(
|
|
"/CreateUserDetails",
|
|
extra: {
|
|
"selectedUser": userDetails,
|
|
"isEditProfile": true,
|
|
"isViewMode": true
|
|
},
|
|
);
|
|
},
|
|
child :SizedBox(
|
|
height: 80,
|
|
child: Container(
|
|
color: Color(0xFFF3F3FA),
|
|
padding: EdgeInsets.all(16),
|
|
width: double.infinity,
|
|
child: Row(
|
|
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(2.0),
|
|
child: Container(
|
|
height: 50,
|
|
width: 50,
|
|
decoration:BoxDecoration(
|
|
color: Colors.blueAccent,
|
|
shape: BoxShape.circle
|
|
) ,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [ Text(
|
|
userData?["name"]?.isNotEmpty == true
|
|
? userData!["name"]![0].toUpperCase()
|
|
: "N/A",
|
|
style: TextStyle(color: Colors.white, fontSize: 25),
|
|
),],),
|
|
),
|
|
),
|
|
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text(
|
|
userData?["name"] ?? "N/A",
|
|
style: TextStyle(color: Colors.black87, fontSize: 11),
|
|
),
|
|
Text(
|
|
userData?["email"] ?? "N/A",
|
|
style: TextStyle(color: Colors.black45, fontSize: 10),
|
|
),
|
|
|
|
],)
|
|
],)
|
|
),
|
|
),
|
|
),
|
|
_buildDrawerItem(context, Icons.home,'Home', '/home'),
|
|
_buildExpandableItem(context,Icons.assessment,'Plans',[
|
|
_buildSubDrawerItem(context,'My Travel Request','/listPlan'),
|
|
// _buildSubDrawerItem(context,'PlanB','/PlanB')
|
|
]),
|
|
_buildExpandableItem(context,Icons.account_circle_outlined,'User ',[
|
|
_buildSubDrawerItem(context,'User List','/listUser'),
|
|
// _buildSubDrawerItem(context,'PlanB','/PlanB')
|
|
]),
|
|
|
|
_buildExpandableItem(context,Icons.policy,'Policy ',[
|
|
_buildSubDrawerItem(context,'Policy','/Policy'),
|
|
// _buildSubDrawerItem(context,'PlanB','/PlanB')
|
|
]),
|
|
_buildDrawerItem(context,Icons.logout,'Logout','/')
|
|
],
|
|
),
|
|
);
|
|
|
|
if (widget.isDesktop) {
|
|
// Sidebar for Desktop (always visible)**
|
|
return Container(
|
|
width: 250, // Fixed width for sidebar
|
|
color: Colors.grey.shade50,
|
|
child: drawerContent,
|
|
);
|
|
} else {
|
|
// Drawer for Mobile & Tablet**
|
|
return Drawer(child: ListView(padding: EdgeInsets.zero, children: [drawerContent]));
|
|
}
|
|
|
|
}
|
|
|
|
/// **Reusable Drawer Item**
|
|
Widget _buildDrawerItem(BuildContext context, IconData icon, String title, String route)
|
|
{
|
|
return ListTile(
|
|
leading: Icon(icon),
|
|
title: Text(title),
|
|
onTap: () async {
|
|
if (route == '/') {
|
|
// Handle logout separately
|
|
final pref = await SharedPreferences.getInstance();
|
|
await pref.clear(); // Clear stored token or session data
|
|
context.go("/"); // Redirect to login instead of home
|
|
} else {
|
|
context.go(route);
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
Widget _buildExpandableItem(BuildContext context, IconData icon, String title, List<Widget>children){
|
|
return ExpansionTile(
|
|
leading: Icon(icon),
|
|
title: Text(title),
|
|
shape: const Border(), // Removes top and bottom dividers
|
|
childrenPadding: const EdgeInsets.only(left: 40), // Indent sub-items
|
|
children: children,
|
|
);
|
|
}
|
|
|
|
Widget _buildSubDrawerItem(BuildContext context, String title, String route)
|
|
{
|
|
return ListTile(
|
|
title: Text(title),
|
|
onTap: (){
|
|
context.go(route);
|
|
if (!widget.isDesktop) Navigator.pop(context);
|
|
},
|
|
);
|
|
}
|
|
|
|
}
|
|
|