refactor: Convert AttendanceFilterBottomSheet to StatefulWidget and enhance state management
This commit is contained in:
parent
9ad8bdc893
commit
706726c787
@ -5,7 +5,7 @@ import 'package:intl/intl.dart';
|
|||||||
import 'package:marco/helpers/widgets/my_text.dart';
|
import 'package:marco/helpers/widgets/my_text.dart';
|
||||||
import 'package:marco/helpers/utils/permission_constants.dart';
|
import 'package:marco/helpers/utils/permission_constants.dart';
|
||||||
|
|
||||||
class AttendanceFilterBottomSheet extends StatelessWidget {
|
class AttendanceFilterBottomSheet extends StatefulWidget {
|
||||||
final AttendanceController controller;
|
final AttendanceController controller;
|
||||||
final PermissionController permissionController;
|
final PermissionController permissionController;
|
||||||
final String selectedTab;
|
final String selectedTab;
|
||||||
@ -17,56 +17,53 @@ class AttendanceFilterBottomSheet extends StatelessWidget {
|
|||||||
required this.selectedTab,
|
required this.selectedTab,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
_AttendanceFilterBottomSheetState createState() =>
|
||||||
|
_AttendanceFilterBottomSheetState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _AttendanceFilterBottomSheetState
|
||||||
|
extends State<AttendanceFilterBottomSheet> {
|
||||||
|
late String? tempSelectedProjectId;
|
||||||
|
late String tempSelectedTab;
|
||||||
|
bool showProjectList = false;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
tempSelectedProjectId = widget.controller.selectedProjectId;
|
||||||
|
tempSelectedTab = widget.selectedTab;
|
||||||
|
}
|
||||||
|
|
||||||
String getLabelText() {
|
String getLabelText() {
|
||||||
final startDate = controller.startDateAttendance;
|
final startDate = widget.controller.startDateAttendance;
|
||||||
final endDate = controller.endDateAttendance;
|
final endDate = widget.controller.endDateAttendance;
|
||||||
if (startDate != null && endDate != null) {
|
if (startDate != null && endDate != null) {
|
||||||
final start = DateFormat('dd MM yyyy').format(startDate);
|
final start = DateFormat('dd/MM/yyyy').format(startDate);
|
||||||
final end = DateFormat('dd MM yyyy').format(endDate);
|
final end = DateFormat('dd/MM/yyyy').format(endDate);
|
||||||
return "$start - $end";
|
return "$start - $end";
|
||||||
}
|
}
|
||||||
return "Date Range";
|
return "Date Range";
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
List<Widget> buildProjectList() {
|
||||||
Widget build(BuildContext context) {
|
final accessibleProjects = widget.controller.projects
|
||||||
String? tempSelectedProjectId = controller.selectedProjectId;
|
|
||||||
String tempSelectedTab = selectedTab;
|
|
||||||
bool showProjectList = false;
|
|
||||||
|
|
||||||
final accessibleProjects = controller.projects
|
|
||||||
.where((project) =>
|
.where((project) =>
|
||||||
permissionController.isUserAssignedToProject(project.id.toString()))
|
widget.permissionController.isUserAssignedToProject(
|
||||||
|
project.id.toString()))
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
bool hasRegularizationPermission =
|
if (accessibleProjects.isEmpty) {
|
||||||
permissionController.hasPermission(Permissions.regularizeAttendance);
|
return [
|
||||||
final viewOptions = [
|
|
||||||
{'label': 'Today\'s Attendance', 'value': 'todaysAttendance'},
|
|
||||||
{'label': 'Attendance Logs', 'value': 'attendanceLogs'},
|
|
||||||
{'label': 'Regularization Requests', 'value': 'regularizationRequests'},
|
|
||||||
];
|
|
||||||
final filteredViewOptions = viewOptions.where((item) {
|
|
||||||
if (item['value'] == 'regularizationRequests') {
|
|
||||||
return hasRegularizationPermission;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}).toList();
|
|
||||||
|
|
||||||
return StatefulBuilder(builder: (context, setState) {
|
|
||||||
List<Widget> filterWidgets;
|
|
||||||
|
|
||||||
if (showProjectList) {
|
|
||||||
filterWidgets = accessibleProjects.isEmpty
|
|
||||||
? [
|
|
||||||
const Padding(
|
const Padding(
|
||||||
padding: EdgeInsets.all(12.0),
|
padding: EdgeInsets.all(12.0),
|
||||||
child: Center(child: Text('No Projects Assigned')),
|
child: Center(child: Text('No Projects Assigned')),
|
||||||
),
|
),
|
||||||
]
|
];
|
||||||
: accessibleProjects.map((project) {
|
}
|
||||||
final isSelected =
|
|
||||||
tempSelectedProjectId == project.id.toString();
|
return accessibleProjects.map((project) {
|
||||||
|
final isSelected = tempSelectedProjectId == project.id.toString();
|
||||||
return ListTile(
|
return ListTile(
|
||||||
dense: true,
|
dense: true,
|
||||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16),
|
contentPadding: const EdgeInsets.symmetric(horizontal: 16),
|
||||||
@ -80,7 +77,15 @@ class AttendanceFilterBottomSheet extends StatelessWidget {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
}).toList();
|
}).toList();
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
List<Widget> buildMainFilters() {
|
||||||
|
final accessibleProjects = widget.controller.projects
|
||||||
|
.where((project) =>
|
||||||
|
widget.permissionController.isUserAssignedToProject(
|
||||||
|
project.id.toString()))
|
||||||
|
.toList();
|
||||||
|
|
||||||
final selectedProject = accessibleProjects.isNotEmpty
|
final selectedProject = accessibleProjects.isNotEmpty
|
||||||
? accessibleProjects.firstWhere(
|
? accessibleProjects.firstWhere(
|
||||||
(p) => p.id.toString() == tempSelectedProjectId,
|
(p) => p.id.toString() == tempSelectedProjectId,
|
||||||
@ -90,7 +95,23 @@ class AttendanceFilterBottomSheet extends StatelessWidget {
|
|||||||
|
|
||||||
final selectedProjectName = selectedProject?.name ?? "Select Project";
|
final selectedProjectName = selectedProject?.name ?? "Select Project";
|
||||||
|
|
||||||
filterWidgets = [
|
final hasRegularizationPermission = widget.permissionController
|
||||||
|
.hasPermission(Permissions.regularizeAttendance);
|
||||||
|
|
||||||
|
final viewOptions = [
|
||||||
|
{'label': 'Today\'s Attendance', 'value': 'todaysAttendance'},
|
||||||
|
{'label': 'Attendance Logs', 'value': 'attendanceLogs'},
|
||||||
|
{'label': 'Regularization Requests', 'value': 'regularizationRequests'},
|
||||||
|
];
|
||||||
|
|
||||||
|
final filteredViewOptions = viewOptions.where((item) {
|
||||||
|
if (item['value'] == 'regularizationRequests') {
|
||||||
|
return hasRegularizationPermission;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}).toList();
|
||||||
|
|
||||||
|
List<Widget> widgets = [
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
|
padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
|
||||||
child: Align(
|
child: Align(
|
||||||
@ -132,7 +153,7 @@ class AttendanceFilterBottomSheet extends StatelessWidget {
|
|||||||
];
|
];
|
||||||
|
|
||||||
if (tempSelectedTab == 'attendanceLogs') {
|
if (tempSelectedTab == 'attendanceLogs') {
|
||||||
filterWidgets.addAll([
|
widgets.addAll([
|
||||||
const Divider(),
|
const Divider(),
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
|
padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
|
||||||
@ -148,13 +169,13 @@ class AttendanceFilterBottomSheet extends StatelessWidget {
|
|||||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
borderRadius: BorderRadius.circular(10),
|
borderRadius: BorderRadius.circular(10),
|
||||||
onTap: () => controller.selectDateRangeForAttendance(
|
onTap: () => widget.controller.selectDateRangeForAttendance(
|
||||||
context,
|
context,
|
||||||
controller,
|
widget.controller,
|
||||||
),
|
),
|
||||||
child: Ink(
|
child: Ink(
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
color: const Color.fromARGB(255, 255, 255, 255),
|
color: Colors.white,
|
||||||
border: Border.all(color: Colors.grey.shade400),
|
border: Border.all(color: Colors.grey.shade400),
|
||||||
borderRadius: BorderRadius.circular(10),
|
borderRadius: BorderRadius.circular(10),
|
||||||
),
|
),
|
||||||
@ -162,8 +183,7 @@ class AttendanceFilterBottomSheet extends StatelessWidget {
|
|||||||
const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||||
child: Row(
|
child: Row(
|
||||||
children: [
|
children: [
|
||||||
Icon(Icons.date_range,
|
Icon(Icons.date_range, color: Colors.black87),
|
||||||
color: const Color.fromARGB(255, 9, 9, 9)),
|
|
||||||
const SizedBox(width: 12),
|
const SizedBox(width: 12),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Text(
|
child: Text(
|
||||||
@ -176,8 +196,7 @@ class AttendanceFilterBottomSheet extends StatelessWidget {
|
|||||||
overflow: TextOverflow.ellipsis,
|
overflow: TextOverflow.ellipsis,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const Icon(Icons.arrow_drop_down,
|
const Icon(Icons.arrow_drop_down, color: Colors.black87),
|
||||||
color: Color.fromARGB(255, 0, 0, 0)),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -185,16 +204,19 @@ class AttendanceFilterBottomSheet extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return widgets;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
return Padding(
|
return Padding(
|
||||||
padding: EdgeInsets.only(
|
padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
|
||||||
bottom: MediaQuery.of(context).viewInsets.bottom,
|
|
||||||
),
|
|
||||||
child: SingleChildScrollView(
|
child: SingleChildScrollView(
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
|
// Drag handle
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.only(top: 12, bottom: 8),
|
padding: const EdgeInsets.only(top: 12, bottom: 8),
|
||||||
child: Center(
|
child: Center(
|
||||||
@ -208,11 +230,10 @@ class AttendanceFilterBottomSheet extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
...filterWidgets,
|
if (showProjectList) ...buildProjectList() else ...buildMainFilters(),
|
||||||
const Divider(),
|
const Divider(),
|
||||||
Padding(
|
Padding(
|
||||||
padding:
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||||
const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
width: double.infinity,
|
width: double.infinity,
|
||||||
child: ElevatedButton(
|
child: ElevatedButton(
|
||||||
@ -237,6 +258,5 @@ class AttendanceFilterBottomSheet extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user