feat : PopScope and there logic

This commit is contained in:
2025-07-13 11:52:19 +03:30
parent 9c8ab53188
commit 6de5c21919
2 changed files with 169 additions and 112 deletions

View File

@@ -1,89 +1,119 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rasadyar_chicken/chicken.dart';
import 'package:rasadyar_chicken/data/models/response/kill_house_distribution_info/kill_house_distribution_info.dart';
import 'package:rasadyar_core/core.dart';
class RootPage extends GetView<RootLogic> {
const RootPage({super.key});
RootPage({super.key});
DateTime? _lastBackPressed;
@override
Widget build(BuildContext context) {
return ObxValue((data) {
return Scaffold(
backgroundColor: AppColor.bgLight,
body: IndexedStack(
children: [
Navigator(
key: Get.nestedKey(0),
onGenerateRoute: (settings) {
final page = ChickenPages.pages.firstWhere(
(e) => e.name == settings.name,
orElse: () => ChickenPages.pages.firstWhere((e) => e.name == ChickenRoutes.buy),
);
return PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, result) async {
final nestedKey = Get.nestedKey(controller.currentPage.value);
final currentNavigator = nestedKey?.currentState;
return buildRouteFromGetPage(page);
},
),
Navigator(
key: Get.nestedKey(1),
onGenerateRoute: (settings) {
final page = ChickenPages.pages.firstWhere(
(e) => e.name == settings.name,
orElse: () => ChickenPages.pages.firstWhere((e) => e.name == ChickenRoutes.sale),
);
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(
backgroundColor: AppColor.bgLight,
body: IndexedStack(
children: [
Navigator(
key: Get.nestedKey(0),
onGenerateRoute: (settings) {
final page = ChickenPages.pages.firstWhere(
(e) => e.name == settings.name,
orElse: () => ChickenPages.pages.firstWhere((e) => e.name == ChickenRoutes.buy),
);
return buildRouteFromGetPage(page);
},
),
Navigator(
key: Get.nestedKey(2),
onGenerateRoute: (settings) => GetPageRoute(page: () => controller.pages[2]),
),
Navigator(
key: Get.nestedKey(3),
onGenerateRoute: (settings) => GetPageRoute(page: () => controller.pages[3]),
),
Navigator(
key: Get.nestedKey(4),
onGenerateRoute: (settings) => GetPageRoute(page: () => controller.pages[4]),
),
],
index: data.value,
),
return buildRouteFromGetPage(page);
},
),
Navigator(
key: Get.nestedKey(1),
onGenerateRoute: (settings) {
final page = ChickenPages.pages.firstWhere(
(e) => e.name == settings.name,
orElse: () =>
ChickenPages.pages.firstWhere((e) => e.name == ChickenRoutes.sale),
);
bottomNavigationBar: RBottomNavigation(
items: [
RBottomNavigationItem(
label: 'خرید',
icon: Assets.vec.buySvg.path,
isSelected: controller.currentPage.value == 0,
onTap: () => controller.changePage(0),
),
RBottomNavigationItem(
label: 'فروش',
icon: Assets.vec.saleSvg.path,
isSelected: controller.currentPage.value == 1,
onTap: () => controller.changePage(1),
),
RBottomNavigationItem(
label: 'خانه',
icon: Assets.vec.homeSvg.path,
isSelected: controller.currentPage.value == 2,
onTap: () => controller.changePage(2),
),
RBottomNavigationItem(
label: 'قطعه بندی',
icon: Assets.vec.convertCubeSvg.path,
isSelected: controller.currentPage.value == 3,
onTap: () => controller.changePage(3),
),
RBottomNavigationItem(
label: 'پروفایل',
icon: Assets.vec.profileCircleSvg.path,
isSelected: controller.currentPage.value == 4,
onTap: () => controller.changePage(4),
),
],
return buildRouteFromGetPage(page);
},
),
Navigator(
key: Get.nestedKey(2),
onGenerateRoute: (settings) => GetPageRoute(page: () => controller.pages[2]),
),
Navigator(
key: Get.nestedKey(3),
onGenerateRoute: (settings) => GetPageRoute(page: () => controller.pages[3]),
),
Navigator(
key: Get.nestedKey(4),
onGenerateRoute: (settings) => GetPageRoute(page: () => controller.pages[4]),
),
],
index: data.value,
),
bottomNavigationBar: RBottomNavigation(
items: [
RBottomNavigationItem(
label: 'خرید',
icon: Assets.vec.buySvg.path,
isSelected: controller.currentPage.value == 0,
onTap: () => controller.changePage(0),
),
RBottomNavigationItem(
label: 'فروش',
icon: Assets.vec.saleSvg.path,
isSelected: controller.currentPage.value == 1,
onTap: () => controller.changePage(1),
),
RBottomNavigationItem(
label: 'خانه',
icon: Assets.vec.homeSvg.path,
isSelected: controller.currentPage.value == 2,
onTap: () => controller.changePage(2),
),
RBottomNavigationItem(
label: 'قطعه بندی',
icon: Assets.vec.convertCubeSvg.path,
isSelected: controller.currentPage.value == 3,
onTap: () => controller.changePage(3),
),
RBottomNavigationItem(
label: 'پروفایل',
icon: Assets.vec.profileCircleSvg.path,
isSelected: controller.currentPage.value == 4,
onTap: () => controller.changePage(4),
),
],
),
),
);
}, controller.currentPage);
@@ -164,15 +194,20 @@ class RootPage extends GetView<RootLogic> {
height: 82,
decoration: BoxDecoration(
color: bgLabelColor,
borderRadius: BorderRadius.only(topRight: Radius.circular(8), bottomRight: Radius.circular(8)),
borderRadius: BorderRadius.only(
topRight: Radius.circular(8),
bottomRight: Radius.circular(8),
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
spacing: 4,
children: [
SvgGenImage.vec(
iconPath,
).svg(width: 24, height: 24, colorFilter: ColorFilter.mode(iconColor, BlendMode.srcIn)),
SvgGenImage.vec(iconPath).svg(
width: 24,
height: 24,
colorFilter: ColorFilter.mode(iconColor, BlendMode.srcIn),
),
Text(
title,
textAlign: TextAlign.right,
@@ -187,7 +222,10 @@ class RootPage extends GetView<RootLogic> {
child: Container(
decoration: BoxDecoration(
color: bgDescriptionColor,
borderRadius: BorderRadius.only(topLeft: Radius.circular(8), bottomLeft: Radius.circular(8)),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8),
bottomLeft: Radius.circular(8),
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
@@ -275,9 +313,11 @@ class RootPage extends GetView<RootLogic> {
),
),
child: Center(
child: SvgGenImage.vec(
iconPath,
).svg(width: 24, height: 24, colorFilter: ColorFilter.mode(iconColor, BlendMode.srcIn)),
child: SvgGenImage.vec(iconPath).svg(
width: 24,
height: 24,
colorFilter: ColorFilter.mode(iconColor, BlendMode.srcIn),
),
),
),
),
@@ -286,7 +326,11 @@ class RootPage extends GetView<RootLogic> {
);
}
Widget widelyUsed({required String title, required String iconPath, required VoidCallback onTap}) {
Widget widelyUsed({
required String title,
required String iconPath,
required VoidCallback onTap,
}) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
@@ -525,15 +569,25 @@ class RootPage extends GetView<RootLogic> {
style: AppFonts.yekan16Bold.copyWith(color: AppColor.blueNormal),
),
const SizedBox(height: 12),
buildRow('فروش و توزیع داخل استان (کیلوگرم)', model.stewardAllocationsWeight!.toInt().toString()),
buildRow('فروش و توزیع خارج استان (کیلوگرم)', model.freeSalesWeight!.toInt().toString()),
buildRow(
'فروش و توزیع داخل استان (کیلوگرم)',
model.stewardAllocationsWeight!.toInt().toString(),
),
buildRow(
'فروش و توزیع خارج استان (کیلوگرم)',
model.freeSalesWeight!.toInt().toString(),
),
],
)
: const Center(child: CircularProgressIndicator()),
);
}
Widget cardWidget({required String title, required String iconPath, required VoidCallback onTap}) {
Widget cardWidget({
required String title,
required String iconPath,
required VoidCallback onTap,
}) {
return Container(
width: Get.width / 4,
height: 130,

View File

@@ -23,11 +23,10 @@ class BasePage extends StatefulWidget {
this.onFilterTap,
this.onSearchTap,
this.filteringWidget,
}):assert(
(routes != null ) || routesWidget != null,
'Either routes or routesWidget must be provided.',
);
}) : assert(
(routes != null) || routesWidget != null,
'Either routes or routesWidget must be provided.',
);
final List<String>? routes;
final Widget? routesWidget;
@@ -77,29 +76,33 @@ class _BasePageState extends State<BasePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppColor.bgLight,
appBar: chickenAppBar(
hasBack: widget.isBase ? false : widget.hasBack,
onBackPressed: widget.onBackPressed,
hasFilter: widget.hasFilter,
hasSearch: widget.hasSearch,
isBase: widget.isBase,
onFilterTap: widget.hasFilter ? () => controller.toggleFilter() : null,
onSearchTap: widget.hasSearch ? () => Get.find<SearchLogic>().toggleSearch() : null,
return PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, result) => widget.onBackPressed,
child: Scaffold(
backgroundColor: AppColor.bgLight,
appBar: chickenAppBar(
hasBack: widget.isBase ? false : widget.hasBack,
onBackPressed: widget.onBackPressed,
hasFilter: widget.hasFilter,
hasSearch: widget.hasSearch,
isBase: widget.isBase,
onFilterTap: widget.hasFilter ? () => controller.toggleFilter() : null,
onSearchTap: widget.hasSearch ? () => Get.find<SearchLogic>().toggleSearch() : null,
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
widget.routesWidget != null ? widget.routesWidget! : buildPageRoute(widget.routes!),
if (!widget.isBase && widget.hasSearch) ...{
SearchWidget(onSearchChanged: widget.onSearchChanged),
},
...widget.widgets,
],
),
floatingActionButtonLocation: widget.floatingActionButtonLocation,
floatingActionButton: widget.floatingActionButton,
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
widget.routesWidget != null ? widget.routesWidget! : buildPageRoute(widget.routes!),
if (!widget.isBase && widget.hasSearch) ...{
SearchWidget(onSearchChanged: widget.onSearchChanged),
},
...widget.widgets,
],
),
floatingActionButtonLocation: widget.floatingActionButtonLocation,
floatingActionButton: widget.floatingActionButton,
);
}
}