chore : refactor root

This commit is contained in:
2025-08-25 10:03:55 +03:30
parent 3bd3ecbf50
commit 6e8530ec7f
2 changed files with 27 additions and 26 deletions

View File

@@ -43,6 +43,8 @@ class RootLogic extends GetxController {
late StreamSubscription<List<ConnectivityResult>> connectivitySubscription;
RxList<ConnectivityResult> connectivityResults = <ConnectivityResult>[].obs;
DateTime? lastBackPressed;
@override
void onInit() {
super.onInit();

View File

@@ -7,7 +7,29 @@ import 'logic.dart';
class RootPage extends GetView<RootLogic> {
RootPage({super.key});
DateTime? _lastBackPressed;
// Extracted back-press
Future<void> _handleBackPress(BuildContext context) async {
final nestedKey = Get.nestedKey(controller.currentIndex.value);
final currentNavigator = nestedKey?.currentState;
if (currentNavigator?.canPop() ?? false) {
currentNavigator?.pop();
return;
}
final now = DateTime.now();
if (controller.lastBackPressed == null ||
now.difference(controller.lastBackPressed!) > const Duration(seconds: 2)) {
controller.lastBackPressed = now;
Get.snackbar(
'خروج از برنامه',
'برای خروج دوباره بازگشت را بزنید',
snackPosition: SnackPosition.TOP,
duration: const Duration(seconds: 2),
backgroundColor: AppColor.warning,
);
} else {
await SystemNavigator.pop();
}
}
@override
Widget build(BuildContext context) {
@@ -15,36 +37,14 @@ class RootPage extends GetView<RootLogic> {
return PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, result) async {
final nestedKey = Get.nestedKey(controller.currentIndex.value);
final currentNavigator = nestedKey?.currentState;
if (currentNavigator?.canPop() ?? false) {
currentNavigator?.pop();
} else {
final now = DateTime.now();
if (_lastBackPressed == null ||
now.difference(_lastBackPressed!) > Duration(seconds: 2)) {
_lastBackPressed = now;
Get.snackbar(
'خروج از برنامه',
'برای خروج دوباره بازگشت را بزنید',
snackPosition: SnackPosition.TOP,
duration: Duration(seconds: 2),
backgroundColor: AppColor.warning,
);
} else {
await SystemNavigator.pop();
}
}
await _handleBackPress(context);
},
child: Scaffold(
body: IndexedStack(
children: [...controller.pages],
children: controller.pages,
index: currentIndex.value,
sizing: StackFit.expand,
),
bottomNavigationBar: RBottomNavigation(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
items: [
@@ -66,7 +66,6 @@ class RootPage extends GetView<RootLogic> {
controller.changePage(1);
},
),
RBottomNavigationItem(
label: 'پروفایل',
icon: Assets.vec.profileCircleSvg.path,