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; late StreamSubscription<List<ConnectivityResult>> connectivitySubscription;
RxList<ConnectivityResult> connectivityResults = <ConnectivityResult>[].obs; RxList<ConnectivityResult> connectivityResults = <ConnectivityResult>[].obs;
DateTime? lastBackPressed;
@override @override
void onInit() { void onInit() {
super.onInit(); super.onInit();

View File

@@ -7,7 +7,29 @@ import 'logic.dart';
class RootPage extends GetView<RootLogic> { class RootPage extends GetView<RootLogic> {
RootPage({super.key}); 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -15,36 +37,14 @@ class RootPage extends GetView<RootLogic> {
return PopScope( return PopScope(
canPop: false, canPop: false,
onPopInvokedWithResult: (didPop, result) async { onPopInvokedWithResult: (didPop, result) async {
final nestedKey = Get.nestedKey(controller.currentIndex.value); await _handleBackPress(context);
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();
}
}
}, },
child: Scaffold( child: Scaffold(
body: IndexedStack( body: IndexedStack(
children: [...controller.pages], children: controller.pages,
index: currentIndex.value, index: currentIndex.value,
sizing: StackFit.expand, sizing: StackFit.expand,
), ),
bottomNavigationBar: RBottomNavigation( bottomNavigationBar: RBottomNavigation(
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: MainAxisAlignment.spaceEvenly,
items: [ items: [
@@ -66,7 +66,6 @@ class RootPage extends GetView<RootLogic> {
controller.changePage(1); controller.changePage(1);
}, },
), ),
RBottomNavigationItem( RBottomNavigationItem(
label: 'پروفایل', label: 'پروفایل',
icon: Assets.vec.profileCircleSvg.path, icon: Assets.vec.profileCircleSvg.path,