|
| 1 | +import 'dart:math'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:go_router/go_router.dart'; |
| 5 | +import 'package:rettulf/rettulf.dart'; |
| 6 | + |
| 7 | +typedef AdaptiveNavigationItem = ({String route, IconData icon, IconData activeIcon, String label}); |
| 8 | + |
| 9 | +extension _AdaptiveNavigationItemEX on AdaptiveNavigationItem { |
| 10 | + NavigationDestination toBarItem() { |
| 11 | + return NavigationDestination( |
| 12 | + icon: Icon(icon), |
| 13 | + selectedIcon: Icon(activeIcon), |
| 14 | + label: label, |
| 15 | + ); |
| 16 | + } |
| 17 | + |
| 18 | + NavigationRailDestination toRailDest() { |
| 19 | + return NavigationRailDestination( |
| 20 | + icon: Icon(icon), |
| 21 | + selectedIcon: Icon(activeIcon), |
| 22 | + label: Text(label), |
| 23 | + ); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +class AdaptiveNavigationScaffold extends StatelessWidget { |
| 28 | + final StatefulNavigationShell navigationShell; |
| 29 | + final Color? navigationBarColor; |
| 30 | + final Color? scaffoldBackgroundColor; |
| 31 | + final List<AdaptiveNavigationItem> items; |
| 32 | + final Widget Function(BuildContext context, Widget child)? bottomBarBuilder; |
| 33 | + |
| 34 | + const AdaptiveNavigationScaffold({ |
| 35 | + super.key, |
| 36 | + required this.navigationShell, |
| 37 | + required this.items, |
| 38 | + this.scaffoldBackgroundColor, |
| 39 | + this.navigationBarColor, |
| 40 | + this.bottomBarBuilder, |
| 41 | + }); |
| 42 | + |
| 43 | + @override |
| 44 | + Widget build(BuildContext context) { |
| 45 | + if (context.isPortrait) { |
| 46 | + final bottomBarBuilder = this.bottomBarBuilder; |
| 47 | + final bottomBar = buildNavigationBar(context, items); |
| 48 | + return Scaffold( |
| 49 | + backgroundColor: scaffoldBackgroundColor, |
| 50 | + body: navigationShell, |
| 51 | + bottomNavigationBar: bottomBarBuilder != null ? bottomBarBuilder(context, bottomBar) : bottomBar, |
| 52 | + ); |
| 53 | + } else { |
| 54 | + return Scaffold( |
| 55 | + backgroundColor: scaffoldBackgroundColor, |
| 56 | + body: [ |
| 57 | + buildNavigationRail(context, items), |
| 58 | + navigationShell.expanded(), |
| 59 | + ].row(), |
| 60 | + ); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + Widget buildNavigationBar(BuildContext context, List<AdaptiveNavigationItem> items) { |
| 65 | + return NavigationBar( |
| 66 | + backgroundColor: navigationBarColor, |
| 67 | + selectedIndex: getSelectedIndex(context, items), |
| 68 | + onDestinationSelected: (index) => onItemTapped(index, items), |
| 69 | + destinations: items.map((e) => e.toBarItem()).toList(), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + Widget buildNavigationRail(BuildContext context, List<AdaptiveNavigationItem> items) { |
| 74 | + return NavigationRail( |
| 75 | + backgroundColor: navigationBarColor, |
| 76 | + groupAlignment: 0, |
| 77 | + labelType: NavigationRailLabelType.all, |
| 78 | + selectedIndex: getSelectedIndex(context, items), |
| 79 | + onDestinationSelected: (index) => onItemTapped(index, items), |
| 80 | + destinations: items.map((e) => e.toRailDest()).toList(), |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + int getSelectedIndex(BuildContext context, List<AdaptiveNavigationItem> items) { |
| 85 | + final location = GoRouterState.of(context).uri.toString(); |
| 86 | + return max(0, items.indexWhere((item) => location.startsWith(item.route))); |
| 87 | + } |
| 88 | + |
| 89 | + void onItemTapped(int index, List<AdaptiveNavigationItem> items) { |
| 90 | + final item = items[index]; |
| 91 | + final branchIndex = navigationShell.route.routes.indexWhere((r) { |
| 92 | + if (r is GoRoute) { |
| 93 | + return r.path.startsWith(item.route); |
| 94 | + } |
| 95 | + return false; |
| 96 | + }); |
| 97 | + navigationShell.goBranch( |
| 98 | + branchIndex >= 0 ? branchIndex : index, |
| 99 | + initialLocation: index == navigationShell.currentIndex, |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments