2022-12-05 08:06:45 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2023-01-23 03:02:59 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2022-12-05 08:06:45 +00:00
|
|
|
import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart';
|
2023-10-06 04:43:37 +00:00
|
|
|
import 'package:unit2/model/login_data/user_info/module_object.dart';
|
2023-07-28 02:21:42 +00:00
|
|
|
import 'package:unit2/screens/unit2/homepage.dart/components/dashboard/dashboard.dart';
|
2022-12-05 08:06:45 +00:00
|
|
|
import 'package:unit2/theme-data.dart/colors.dart';
|
|
|
|
import 'package:unit2/utils/text_container.dart';
|
2023-02-01 08:03:05 +00:00
|
|
|
import '../../../bloc/user/user_bloc.dart';
|
2023-07-28 02:21:42 +00:00
|
|
|
import '../../../model/login_data/user_info/module.dart';
|
2023-01-23 03:02:59 +00:00
|
|
|
import '../../../model/login_data/user_info/role.dart';
|
2022-12-05 08:06:45 +00:00
|
|
|
import 'components/empty_module.dart';
|
|
|
|
|
|
|
|
class MainScreen extends StatefulWidget {
|
|
|
|
const MainScreen({Key? key}) : super(key: key);
|
2022-11-28 08:03:13 +00:00
|
|
|
|
|
|
|
@override
|
2022-12-05 08:06:45 +00:00
|
|
|
State<MainScreen> createState() => _MainScreenState();
|
2022-11-28 08:03:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 08:06:45 +00:00
|
|
|
class _MainScreenState extends State<MainScreen> {
|
2023-07-28 02:21:42 +00:00
|
|
|
List<Role> roles = [];
|
|
|
|
List<DisplayCard> cards = [];
|
2023-06-21 00:22:43 +00:00
|
|
|
int? userId;
|
2023-09-11 03:34:42 +00:00
|
|
|
DateTime? ctime;
|
2022-11-28 08:03:13 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-09-11 03:34:42 +00:00
|
|
|
|
2023-07-28 02:21:42 +00:00
|
|
|
setState(() {
|
|
|
|
cards.clear();
|
|
|
|
cards=[];
|
|
|
|
roles.clear();
|
|
|
|
roles = [];
|
|
|
|
});
|
2022-12-05 08:06:45 +00:00
|
|
|
return WillPopScope(
|
|
|
|
onWillPop: () async {
|
|
|
|
return Future.value(true);
|
|
|
|
},
|
2023-01-23 03:02:59 +00:00
|
|
|
child: BlocBuilder<UserBloc, UserState>(builder: (context, state) {
|
|
|
|
if (state is UserLoggedIn) {
|
2023-06-21 00:22:43 +00:00
|
|
|
userId = state.userData!.user!.login!.user!.id;
|
2023-03-07 02:31:28 +00:00
|
|
|
for (var role in state.userData!.user!.login!.user!.roles!) {
|
2023-01-23 03:02:59 +00:00
|
|
|
Role? getRole = role;
|
2023-07-28 02:21:42 +00:00
|
|
|
roles.add(getRole!);
|
|
|
|
}
|
|
|
|
for (var role in roles) {
|
|
|
|
for (var module in role.modules!) {
|
|
|
|
for (var object in module!.objects!) {
|
|
|
|
DisplayCard newCard = DisplayCard(
|
2023-08-27 08:38:05 +00:00
|
|
|
roleId: role.id!,
|
2023-07-28 02:21:42 +00:00
|
|
|
moduleName: module.name!.toLowerCase(),
|
|
|
|
object: object!,
|
|
|
|
roleName: role.name!.toLowerCase());
|
|
|
|
cards.add(newCard);
|
2023-01-23 03:02:59 +00:00
|
|
|
}
|
2023-03-07 02:31:28 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-28 02:21:42 +00:00
|
|
|
|
2023-09-11 03:34:42 +00:00
|
|
|
return WillPopScope(
|
|
|
|
onWillPop: () {
|
|
|
|
DateTime now = DateTime.now();
|
|
|
|
if (ctime == null || now.difference(ctime!) > const Duration(seconds: 2)) {
|
|
|
|
//add duration of press gap
|
|
|
|
ctime = now;
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(content: Text('Press Again to Exit',textAlign: TextAlign.center,))
|
|
|
|
);
|
|
|
|
return Future.value(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Future.value(true);
|
|
|
|
},
|
|
|
|
child: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
backgroundColor: primary,
|
|
|
|
leading: IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
ZoomDrawer.of(context)!.toggle();
|
|
|
|
},
|
|
|
|
icon: const Icon(
|
|
|
|
Icons.menu,
|
|
|
|
color: Colors.white,
|
|
|
|
),
|
2023-07-28 02:21:42 +00:00
|
|
|
),
|
2023-09-11 03:34:42 +00:00
|
|
|
centerTitle: true,
|
|
|
|
title: const Text(
|
|
|
|
unit2ModuleScreen,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18.0,
|
|
|
|
color: Colors.white,
|
|
|
|
),
|
2023-07-28 02:21:42 +00:00
|
|
|
),
|
|
|
|
),
|
2023-09-11 03:34:42 +00:00
|
|
|
body: state.userData!.user!.login!.user!.roles!.isNotEmpty
|
|
|
|
? DashBoard(
|
|
|
|
estPersonAssignedArea: state.estPersonAssignedArea,
|
|
|
|
userId: userId!,
|
|
|
|
cards: cards,
|
|
|
|
)
|
|
|
|
: const NoModule(),
|
2022-12-05 08:06:45 +00:00
|
|
|
),
|
2023-02-08 08:06:27 +00:00
|
|
|
);
|
2023-01-23 03:02:59 +00:00
|
|
|
}
|
|
|
|
return Container();
|
|
|
|
}),
|
2022-12-05 08:06:45 +00:00
|
|
|
);
|
2022-11-28 08:03:13 +00:00
|
|
|
}
|
2023-07-28 02:21:42 +00:00
|
|
|
}
|
2023-01-23 03:02:59 +00:00
|
|
|
|
2023-07-28 02:21:42 +00:00
|
|
|
class DisplayCard {
|
|
|
|
final String roleName;
|
|
|
|
final String moduleName;
|
|
|
|
final ModuleObject object;
|
2023-08-27 08:38:05 +00:00
|
|
|
final int roleId;
|
2023-07-28 02:21:42 +00:00
|
|
|
const DisplayCard(
|
2023-08-27 08:38:05 +00:00
|
|
|
{required this.moduleName, required this.object, required this.roleName,required this.roleId});
|
2023-01-23 03:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Module {
|
|
|
|
final String name;
|
|
|
|
final List<Roles> roles;
|
|
|
|
Module({required this.name, required this.roles});
|
|
|
|
}
|
|
|
|
|
|
|
|
class Roles {
|
2023-07-28 02:21:42 +00:00
|
|
|
final IconData? icon;
|
2023-01-23 03:02:59 +00:00
|
|
|
final Role role;
|
|
|
|
Roles({required this.role, required this.icon});
|
2022-12-05 08:06:45 +00:00
|
|
|
}
|