From 7e65e9837648c253bdf7708b3c771a0295abe0da Mon Sep 17 00:00:00 2001 From: KeidsID Date: Wed, 15 Jan 2025 10:19:37 +0800 Subject: [PATCH] fix(lib-interfaces): fix presist FloatingActionButton on post story page ds-12 --- lib/interfaces/modules/routes.dart | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/interfaces/modules/routes.dart b/lib/interfaces/modules/routes.dart index 2c86d61..498048b 100644 --- a/lib/interfaces/modules/routes.dart +++ b/lib/interfaces/modules/routes.dart @@ -124,12 +124,15 @@ class _RootShellRouteScreenNavDelegate { final String label; final Icon? activeIcon; final String routePath; + + /// Only be rendered when the current route matches the [routePath]. final Widget? floatingActionButton; } mixin _RootShellRouteScreenNavHelperMixin on Widget { double get minWidthForNavigationRail => 800.0; + /// Match [GoRouter] current route starts with the [navs] route path. int _getCurrentNavigationIndex( BuildContext context, List<_RootShellRouteScreenNavDelegate> navs, @@ -138,6 +141,19 @@ mixin _RootShellRouteScreenNavHelperMixin on Widget { return navs.indexWhere((e) => currentRoute.startsWith(e.routePath)); } + + /// Instead of matching the route starts like [_getCurrentNavigationIndex] + /// does, this method match the exact route path. + /// + /// If there is no matched route, it returns -1. + int _getActualRouteMatchIndex( + BuildContext context, + List<_RootShellRouteScreenNavDelegate> navs, + ) { + final currentRoute = GoRouterState.of(context).uri.path; + + return navs.indexWhere((e) => currentRoute == e.routePath); + } } class _RootShellRouteScreenBody extends StatelessWidget @@ -239,6 +255,7 @@ class _RootShellRouteScreenBottomNavBar extends StatelessWidget return BottomNavigationBarItem( icon: e.icon, label: e.label, + activeIcon: e.activeIcon, ); }).toList(), ); @@ -263,11 +280,13 @@ class _RootShellRouteScreenFAB extends StatelessWidget @override Widget build(BuildContext context) { - final currentNavIndex = _getCurrentNavigationIndex( + final currentNavIndex = _getActualRouteMatchIndex( context, navigationDelegates, ); + if (currentNavIndex == -1) return const SizedBox.shrink(); + return navigationDelegates[currentNavIndex].floatingActionButton ?? const SizedBox.shrink(); }