From a7c7442b49720c5017fbd13f0d2d4bf658112de6 Mon Sep 17 00:00:00 2001 From: "mr.mojtaba" Date: Sat, 27 Sep 2025 08:05:26 +0330 Subject: [PATCH] refactor : base page --- .../service/app_navigation_observer.dart | 17 +- .../poultry_science/poultry_action/view.dart | 1 + .../pages/poultry_science/root/logic.dart | 53 +++++- .../pages/poultry_science/root/view.dart | 154 ++++++++---------- .../presentation/widget/base_page/view.dart | 117 ++++--------- pubspec.lock | 6 +- 6 files changed, 149 insertions(+), 199 deletions(-) diff --git a/lib/infrastructure/service/app_navigation_observer.dart b/lib/infrastructure/service/app_navigation_observer.dart index b57db42..475fa44 100644 --- a/lib/infrastructure/service/app_navigation_observer.dart +++ b/lib/infrastructure/service/app_navigation_observer.dart @@ -6,20 +6,9 @@ class CustomNavigationObserver extends NavigatorObserver { @override void didPush(Route route, Route? previousRoute) async { final routeName = route.settings.name; - /* if (!_isWorkDone && (routeName == ChickenRoutes.init || routeName == ChickenRoutes.auth)) { - _isWorkDone = true; - await setupChickenDI(); - } else if (!_isWorkDone && - (routeName == InspectionRoutes.init || routeName == InspectionRoutes.auth)) { - _isWorkDone = true; - await setupInspectionDI(); - } else if (!_isWorkDone && - (routeName == LiveStockRoutes.init || routeName == LiveStockRoutes.auth)) { - - }*/ super.didPush(route, previousRoute); - // tLog('CustomNavigationObserver: didPush - $routeName'); + tLog('CustomNavigationObserver: didPush - $routeName'); } @override @@ -31,12 +20,12 @@ class CustomNavigationObserver extends NavigatorObserver { @override void didPop(Route route, Route? previousRoute) { super.didPop(route, previousRoute); - // tLog('CustomNavigationObserver: didPop - ${route.settings.name}'); + tLog('CustomNavigationObserver: didPop - ${route.settings.name}'); } @override void didRemove(Route route, Route? previousRoute) { super.didRemove(route, previousRoute); - // tLog('CustomNavigationObserver: didRemove - ${route.settings.name}'); + tLog('CustomNavigationObserver: didRemove - ${route.settings.name}'); } } diff --git a/packages/chicken/lib/presentation/pages/poultry_science/poultry_action/view.dart b/packages/chicken/lib/presentation/pages/poultry_science/poultry_action/view.dart index 066a9c9..597b5f0 100644 --- a/packages/chicken/lib/presentation/pages/poultry_science/poultry_action/view.dart +++ b/packages/chicken/lib/presentation/pages/poultry_science/poultry_action/view.dart @@ -31,6 +31,7 @@ class PoultryActionPage extends GetView { title: item.title, vecIcon: item.icon, onTap: () async { + Get.toNamed(item.route, id: poultryFirstKey); }, ); diff --git a/packages/chicken/lib/presentation/pages/poultry_science/root/logic.dart b/packages/chicken/lib/presentation/pages/poultry_science/root/logic.dart index d28567f..70044d7 100644 --- a/packages/chicken/lib/presentation/pages/poultry_science/root/logic.dart +++ b/packages/chicken/lib/presentation/pages/poultry_science/root/logic.dart @@ -1,9 +1,12 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:rasadyar_chicken/data/di/chicken_di.dart'; import 'package:rasadyar_chicken/data/repositories/poultry_science/poultry_science_repository.dart'; import 'package:rasadyar_chicken/presentation/pages/common/profile/view.dart'; import 'package:rasadyar_chicken/presentation/pages/poultry_science/home/view.dart'; import 'package:rasadyar_chicken/presentation/pages/poultry_science/poultry_action/view.dart'; +import 'package:rasadyar_chicken/presentation/routes/pages.dart'; +import 'package:rasadyar_chicken/presentation/routes/routes.dart'; import 'package:rasadyar_chicken/presentation/utils/nested_keys_utils.dart'; import 'package:rasadyar_chicken/presentation/utils/utils.dart'; import 'package:rasadyar_core/core.dart'; @@ -11,15 +14,35 @@ import 'package:rasadyar_core/core.dart'; enum ErrorLocationType { serviceDisabled, permissionDenied, none } class PoultryScienceRootLogic extends GetxController { - RxInt currentPage = 1.obs; - List pages = [PoultryActionPage(), PoultryScienceHomePage(), ProfilePage()]; - var tokenService = Get.find(); late PoultryScienceRepository poultryRepository; RxList errorLocationType = RxList(); RxMap homeExpandedList = RxMap(); + DateTime? _lastBackPressed; + + RxInt currentPage = 1.obs; + + final pages = [ + Navigator( + key: Get.nestedKey(poultryFirstKey), + onGenerateRoute: (settings) { + final page = ChickenPages.pages.firstWhere( + (e) => e.name == settings.name, + orElse: () => ChickenPages.pages.firstWhere( + (e) => e.name == ChickenRoutes.actionPoultryScience, + ), + ); + + return buildRouteFromGetPage(page); + }, + ), + PoultryScienceHomePage(), + ProfilePage(), + + + ]; @override void onInit() { @@ -62,4 +85,28 @@ class PoultryScienceRootLogic extends GetxController { return poultryFirstKey; } } + + void popBackTaped(bool didPop, result) async { + final nestedKeyId = getNestedKey(); + GlobalKey? currentNestedKey = Get.nestedKey(nestedKeyId); + + if (currentNestedKey?.currentState?.canPop() == true) { + Get.back(id: nestedKeyId); + } 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(); + } + } + } } diff --git a/packages/chicken/lib/presentation/pages/poultry_science/root/view.dart b/packages/chicken/lib/presentation/pages/poultry_science/root/view.dart index 9e7cb66..c2d5254 100644 --- a/packages/chicken/lib/presentation/pages/poultry_science/root/view.dart +++ b/packages/chicken/lib/presentation/pages/poultry_science/root/view.dart @@ -1,7 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; -import 'package:rasadyar_chicken/presentation/routes/pages.dart'; -import 'package:rasadyar_chicken/presentation/routes/routes.dart'; import 'package:rasadyar_chicken/presentation/utils/nested_keys_utils.dart'; import 'package:rasadyar_chicken/presentation/widget/base_page/view.dart'; import 'package:rasadyar_core/core.dart'; @@ -9,98 +6,75 @@ import 'package:rasadyar_core/core.dart'; import 'logic.dart'; class PoultryScienceRootPage extends GetView { - PoultryScienceRootPage({super.key}); - - DateTime? _lastBackPressed; + const PoultryScienceRootPage({super.key}); @override Widget build(BuildContext context) { - return ChickenBasePage( - isFullScreen: true, - child: ObxValue((data) { - return Stack( - children: [ - IndexedStack( - children: [ - Navigator( - key: Get.nestedKey(poultryFirstKey), - onGenerateRoute: (settings) { - final page = ChickenPages.pages.firstWhere( - (e) => e.name == settings.name, - orElse: () => ChickenPages.pages.firstWhere( - (e) => e.name == ChickenRoutes.actionPoultryScience, - ), - ); - - return buildRouteFromGetPage(page); - }, + return PopScope( + canPop: false, + onPopInvokedWithResult: controller.popBackTaped, + child: ChickenBasePage( + isFullScreen: true, + child: ObxValue((data) { + return Stack( + children: [ + IndexedStack(children: controller.pages, index: data.value), + Positioned( + right: 0, + left: 0, + bottom: 0, + child: RBottomNavigation( + mainAxisAlignment: MainAxisAlignment.spaceAround, + items: [ + RBottomNavigationItem( + label: 'عملیات', + icon: Assets.vec.settingSvg.path, + isSelected: controller.currentPage.value == 0, + onTap: () { + Get.nestedKey( + poultrySecondKey, + )?.currentState?.popUntil((route) => route.isFirst); + Get.nestedKey( + poultryFirstKey, + )?.currentState?.popUntil((route) => route.isFirst); + controller.changePage(0); + }, + ), + RBottomNavigationItem( + label: 'خانه', + icon: Assets.vec.homeSvg.path, + isSelected: controller.currentPage.value == 1, + onTap: () { + Get.nestedKey( + poultryFirstKey, + )?.currentState?.popUntil((route) => route.isFirst); + Get.nestedKey( + poultryThirdKey, + )?.currentState?.popUntil((route) => route.isFirst); + controller.changePage(1); + }, + ), + RBottomNavigationItem( + label: 'پروفایل', + icon: Assets.vec.profileCircleSvg.path, + isSelected: controller.currentPage.value == 2, + onTap: () { + Get.nestedKey( + poultryFirstKey, + )?.currentState?.popUntil((route) => route.isFirst); + Get.nestedKey( + poultrySecondKey, + )?.currentState?.popUntil((route) => route.isFirst); + controller.changePage(2); + }, + ), + ], ), - Navigator( - key: Get.nestedKey(poultrySecondKey), - onGenerateRoute: (settings) => GetPageRoute(page: () => controller.pages[1]), - ), - Navigator( - key: Get.nestedKey(poultryThirdKey), - onGenerateRoute: (settings) => GetPageRoute(page: () => controller.pages[2]), - ), - ], - index: data.value, - ), - Positioned( - right: 0, - left: 0, - bottom: 0, - child: RBottomNavigation( - mainAxisAlignment: MainAxisAlignment.spaceAround, - items: [ - RBottomNavigationItem( - label: 'عملیات', - icon: Assets.vec.settingSvg.path, - isSelected: controller.currentPage.value == 0, - onTap: () { - Get.nestedKey( - poultrySecondKey, - )?.currentState?.popUntil((route) => route.isFirst); - Get.nestedKey( - poultryFirstKey, - )?.currentState?.popUntil((route) => route.isFirst); - controller.changePage(0); - }, - ), - RBottomNavigationItem( - label: 'خانه', - icon: Assets.vec.homeSvg.path, - isSelected: controller.currentPage.value == 1, - onTap: () { - Get.nestedKey( - poultryFirstKey, - )?.currentState?.popUntil((route) => route.isFirst); - Get.nestedKey( - poultryThirdKey, - )?.currentState?.popUntil((route) => route.isFirst); - controller.changePage(1); - }, - ), - RBottomNavigationItem( - label: 'پروفایل', - icon: Assets.vec.profileCircleSvg.path, - isSelected: controller.currentPage.value == 2, - onTap: () { - Get.nestedKey( - poultryFirstKey, - )?.currentState?.popUntil((route) => route.isFirst); - Get.nestedKey( - poultrySecondKey, - )?.currentState?.popUntil((route) => route.isFirst); - controller.changePage(2); - }, - ), - ], ), - ), - ], - ); - }, controller.currentPage), + ], + ); + }, controller.currentPage), + ), ); } } diff --git a/packages/chicken/lib/presentation/widget/base_page/view.dart b/packages/chicken/lib/presentation/widget/base_page/view.dart index 244deb7..c5df49c 100644 --- a/packages/chicken/lib/presentation/widget/base_page/view.dart +++ b/packages/chicken/lib/presentation/widget/base_page/view.dart @@ -3,7 +3,7 @@ import 'package:rasadyar_chicken/presentation/widget/app_bar.dart'; import 'package:rasadyar_chicken/presentation/widget/base_page/back_ground.dart'; import 'package:rasadyar_core/core.dart'; -class ChickenBasePage extends StatefulWidget { +class ChickenBasePage extends GetView { const ChickenBasePage({ super.key, this.hasBack = true, @@ -62,44 +62,13 @@ class ChickenBasePage extends StatefulWidget { final BackGroundWidget? backGroundWidget; - @override - State createState() => _ChickenBasePageState(); -} - -class _ChickenBasePageState extends State { - int? _lastBackId; - - @override - void initState() { - super.initState(); - _lastBackId = widget.backId; - tLog(_lastBackId); - tLog(widget.backId); - } - - @override - void didChangeDependencies() { - fLog('didChangeDependencies'); - fLog(_lastBackId); - fLog(widget.backId); - super.didChangeDependencies(); - } - - @override - void didUpdateWidget(covariant ChickenBasePage oldWidget) { - dLog('didUpdateWidget'); - dLog(_lastBackId); - dLog(widget.backId); - super.didUpdateWidget(oldWidget); - } - void _onFilterTap() { - if (widget.hasFilter && widget.filteringWidget != null) { + if (hasFilter && filteringWidget != null) { final currentRoute = ModalRoute.of(Get.context!); if (currentRoute?.isCurrent != true) return; Get.bottomSheet( - widget.filteringWidget!, + filteringWidget!, isScrollControlled: true, isDismissible: true, enableDrag: true, @@ -109,61 +78,31 @@ class _ChickenBasePageState extends State { @override Widget build(BuildContext context) { - return PopScope( - canPop: false, - onPopInvokedWithResult: (didPop, result) { - iLog('PopScope onPopInvokedWithResult:==> ${widget.backId}'); - iLog('PopScope onPopInvokedWithResult:==> $_lastBackId}'); - iLog('PopScope onPopInvokedWithResult:==> ${widget.onPopScopTaped}'); - iLog('PopScope onPopInvokedWithResult:==> ${widget.onBackTap}'); - // چک کردن و مدیریت برگشت با backId - if (widget.backId != null) { - final navigatorState = Get.nestedKey(widget.backId!)?.currentState; - if (navigatorState != null && navigatorState.canPop()) { - Get.back(id: widget.backId!); // پاپ با استفاده از backId - } else { - // اگر هیچ صفحه‌ای برای پاپ موجود نیست - if (widget.onPopScopTaped != null) { - widget.onPopScopTaped!(); - } else { - Get.back(); - } - } - } else { - // اگر backId نداشته باشد - if (widget.onPopScopTaped != null) { - widget.onPopScopTaped!(); - } else { - Get.back(); - } - } - }, - child: BasePage( - routes: widget.routes, - routesWidget: widget.routesWidget, - widgets: widget.widgets, - child: widget.child, - scrollable: widget.scrollable, - floatingActionButtonLocation: widget.floatingActionButtonLocation, - floatingActionButton: widget.floatingActionButton, - backGroundWidget: widget.backGroundWidget ?? chickenBackground(), - appBar: widget.isFullScreen - ? null - : chickenAppBar( - isBase: widget.isBase, - hasBack: widget.isBase ? false : widget.hasBack, - onNewsTap: widget.onNewsTap, - hasFilter: widget.hasFilter, - hasSearch: widget.hasSearch, - hasNews: widget.hasNews, - backId: widget.backId, - onBackTap: widget.onBackTap, - hasNotification: widget.hasNotification, - onNotificationTap: widget.onNotificationTap, - onFilterTap: widget.hasFilter ? _onFilterTap : null, - //onSearchTap: widget.hasSearch ? controller.toggleSearch : null, - ), - ), + return BasePage( + routes: routes, + routesWidget: routesWidget, + widgets: widgets, + child: child, + scrollable: scrollable, + floatingActionButtonLocation: floatingActionButtonLocation, + floatingActionButton: floatingActionButton, + backGroundWidget: backGroundWidget ?? chickenBackground(), + appBar: isFullScreen + ? null + : chickenAppBar( + isBase: isBase, + hasBack: isBase ? false : hasBack, + onNewsTap: onNewsTap, + hasFilter: hasFilter, + hasSearch: hasSearch, + hasNews: hasNews, + backId: backId, + onBackTap: onBackTap, + hasNotification: hasNotification, + onNotificationTap: onNotificationTap, + onFilterTap: hasFilter ? _onFilterTap : null, + //onSearchTap: widget.hasSearch ? controller.toggleSearch : null, + ), ); } } diff --git a/pubspec.lock b/pubspec.lock index 8bea71a..c48647c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -317,10 +317,10 @@ packages: dependency: transitive description: name: device_preview_plus - sha256: "2ebe9cf25ed46875c9c1b745d0438df9e4c49f7bcf9ea466c88efbcffcc73ab7" + sha256: "45f9154c3213e470df1aa54326757ccf3cc6fb1f04396bced6ad1a7bfc9d6400" url: "https://pub.dev" source: hosted - version: "2.5.1" + version: "2.5.0" dio: dependency: transitive description: @@ -1839,4 +1839,4 @@ packages: version: "2.1.0" sdks: dart: ">=3.9.2 <4.0.0" - flutter: ">=3.35.4" + flutter: ">=3.35.3"