MIF_E31221222/sigap-mobile/lib/navigation_menu.dart

87 lines
2.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sigap/src/cores/services/supabase_service.dart';
import 'package:sigap/src/features/panic/presentation/pages/panic_button_page.dart';
import 'package:sigap/src/features/personalization/presentasion/pages/settings/setting_screen.dart';
import 'package:sigap/src/shared/widgets/navigation/custom_bottom_navigation_bar.dart';
class NavigationMenu extends StatelessWidget {
const NavigationMenu({super.key});
@override
Widget build(BuildContext context) {
// Ensure NavigationController is registered in a binding first, then use find
final controller = Get.find<NavigationController>();
final theme = Theme.of(context);
return Scaffold(
backgroundColor: theme.scaffoldBackgroundColor,
body: Obx(
() => IndexedStack(
index: controller.selectedIndex.value,
children: controller.getScreens(),
),
),
bottomNavigationBar: const CustomBottomNavigationBar(),
);
}
}
class NavigationController extends GetxController {
static NavigationController get instance => Get.find();
// Observable variable to track the current selected index
final Rx<int> selectedIndex = 0.obs; // Start with PanicButtonPage (index 0)
final SupabaseService supabaseService;
// Observable to track if user is an officer
final RxBool isOfficer = false.obs;
NavigationController({required this.supabaseService});
@override
void onInit() {
super.onInit();
_checkUserRole();
// Listen to auth state changes to update role when login/logout happens
supabaseService.client.auth.onAuthStateChange.listen((data) {
_checkUserRole();
});
}
// Check if the current user is an officer
void _checkUserRole() {
isOfficer.value = supabaseService.isOfficer;
}
// Get the appropriate screens based on user role
List<Widget> getScreens() {
return [
const PanicButtonPage(),
const SettingsScreen(),
];
}
// Method to change selected index
void changeIndex(int index) {
// Ensure the index is valid for the current user role
if (index < getScreens().length) {
selectedIndex.value = index;
}
}
// Get the maximum index based on available screens
int get maxIndex => getScreens().length - 1;
// Check if a specific screen index should be visible
bool isScreenVisible(int index) {
// The PatrolUnit screen is at index 4
if (index == 4) {
return isOfficer.value;
}
return true;
}
}