From e3d4f293bb771eafe389665b26dbb08b04e2c3d4 Mon Sep 17 00:00:00 2001 From: Lucas Aschenbach Date: Thu, 1 Apr 2021 12:02:18 +0200 Subject: [PATCH] Add NestedBackButtonDispatcher --- lib/src/advanced_navigator.dart | 1 + lib/src/back_button_dispatcher.dart | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 lib/src/back_button_dispatcher.dart diff --git a/lib/src/advanced_navigator.dart b/lib/src/advanced_navigator.dart index 6cf20d8..9a4065e 100644 --- a/lib/src/advanced_navigator.dart +++ b/lib/src/advanced_navigator.dart @@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; part 'route_information_provider.dart'; +part 'back_button_dispatcher.dart'; /// A function for building a page stack from route information typedef PathFactory = RouterConfiguration Function(RouteInformation); diff --git a/lib/src/back_button_dispatcher.dart b/lib/src/back_button_dispatcher.dart new file mode 100644 index 0000000..780ae9a --- /dev/null +++ b/lib/src/back_button_dispatcher.dart @@ -0,0 +1,24 @@ +part of 'advanced_navigator.dart'; + +class NestedBackButtonDispatcher extends ChildBackButtonDispatcher { + NestedBackButtonDispatcher( + BackButtonDispatcher parent, { + @required Route route, + }) : _route = route, + super(parent); + + final Route _route; + + @override + Future invokeCallback(Future defaultValue) { + // only pop if navigator is contained in top-most route + if (_route.isCurrent) { + return super.invokeCallback(defaultValue); + } else { + parent.forget(this); + var value = parent.invokeCallback(defaultValue); + parent.deferTo(this); + return value; + } + } +} \ No newline at end of file