Line data Source code
1 : part of '../../routemaster.dart'; 2 : 3 : /// The state of a stack of pages. 4 : class StackPageState { 5 : final navigatorKey = GlobalKey<NavigatorState>(); 6 : final Routemaster _delegate; 7 : late List<PageWrapper> _routes; 8 : 9 9 : StackPageState({ 10 : required Routemaster delegate, 11 : List<PageWrapper>? routes, 12 : }) : _delegate = delegate { 13 : if (routes != null) { 14 9 : _routes = routes; 15 : } 16 : } 17 : 18 9 : List<Page> createPages() { 19 18 : assert(_routes.isNotEmpty, "Can't generate pages with no routes"); 20 45 : final pages = _routes.map((pageState) => pageState.createPage()).toList(); 21 9 : assert(pages.isNotEmpty, 'Returned pages list must not be empty'); 22 : return pages; 23 : } 24 : 25 4 : bool maybeSetChildPages(Iterable<PageWrapper> pages) { 26 8 : _routes = pages.toList(); 27 8 : _delegate._markNeedsUpdate(); 28 : return true; 29 : } 30 : 31 : Iterable<PageWrapper> _getCurrentPages() sync* { 32 : yield* _routes.last.getCurrentPages(); 33 : } 34 : 35 : /// Passed to [Navigator] widgets for them to inform this stack of a pop 36 2 : bool onPopPage(Route<dynamic> route, dynamic result) { 37 2 : if (route.didPop(result)) { 38 2 : _didPop(); 39 : return true; 40 : } 41 : 42 : return false; 43 : } 44 : 45 2 : void _didPop() async { 46 8 : if (await _routes.last.maybePop()) { 47 : return; 48 : } 49 : 50 6 : if (_routes.length > 1) { 51 4 : _routes.removeLast(); 52 4 : _delegate._markNeedsUpdate(); 53 : } 54 : } 55 : 56 2 : Future<bool> maybePop() async { 57 : // First try delegating the pop to the last child route. 58 : // Covered by several tests in feed_test.dart 59 8 : if (await _routes.last.maybePop()) { 60 2 : return SynchronousFuture(true); 61 : } 62 : 63 : // Child wasn't interested, ask the navigator if we have a key 64 10 : if (await navigatorKey.currentState?.maybePop() == true) { 65 2 : return SynchronousFuture(true); 66 : } 67 : 68 : // No navigator attached, but we can pop the stack anyway 69 3 : if (_routes.length > 1) { 70 2 : _routes.removeLast(); 71 2 : _delegate._markNeedsUpdate(); 72 1 : return SynchronousFuture(true); 73 : } 74 : 75 : // Couldn't find anything to pop 76 1 : return SynchronousFuture(false); 77 : } 78 : }