Skip to content

Commit

Permalink
Automatically configure backButtonDispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasAschenbach committed Apr 1, 2021
1 parent e3d4f29 commit a628acf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 0.1.2
* Add `NestedBackButtonDispatcher` class
* Automatically configure `backButtonDispatcher` based on widget tree (no manual configuration required anymore!)

# 0.1.1+3
* add static `attach()` and `attachNamed()` functions to `AdvancedNavigator` class
* Add static `attach()` and `attachNamed()` functions to `AdvancedNavigator` class

# 0.1.1+2
* Fix: named path '/' not forwarding requests to nested navigators
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.1.1+3"
version: "0.1.2"
async:
dependency: transitive
description:
Expand Down
22 changes: 20 additions & 2 deletions lib/src/advanced_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ class AdvancedNavigatorState extends State<AdvancedNavigator>

DefaultRouterDelegate _routerDelegate;
RouteInformationProvider _informationProvider;
BackButtonDispatcher _backButtonDispatcher;

Set<AdvancedNavigatorState> _children;

Expand Down Expand Up @@ -499,8 +500,8 @@ class AdvancedNavigatorState extends State<AdvancedNavigator>
);

// persist state on rebuild: == null
var ancestor = context.findAncestorStateOfType<AdvancedNavigatorState>();
if (_informationProvider == null) {
var ancestor = context.findAncestorStateOfType<AdvancedNavigatorState>();
var initialLocation = widget.initialLocation
?? AdvancedNavigator.defaultPathName;
var initialRouteInformation = RouteInformation(
Expand Down Expand Up @@ -530,13 +531,30 @@ class AdvancedNavigatorState extends State<AdvancedNavigator>
);
}
}
if (_backButtonDispatcher == null) {
var backButtonDispatcher = widget.backButtonDispatcher;
if (backButtonDispatcher == null) {
if (ancestor != null) {
var route = ModalRoute.of(context);
if (route != null) {
backButtonDispatcher = NestedBackButtonDispatcher(
ancestor._backButtonDispatcher,
route: route,
);
}
} else {
backButtonDispatcher = RootBackButtonDispatcher();
}
}
_backButtonDispatcher = backButtonDispatcher;
}

return Router(
key: _routerKey,
routerDelegate: _routerDelegate,
routeInformationProvider: _informationProvider,
routeInformationParser: DefaultRouteInformationParser(),
backButtonDispatcher: widget.backButtonDispatcher ?? RootBackButtonDispatcher(),
backButtonDispatcher: _backButtonDispatcher,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: advanced_navigator
description: Flutter's Navigator 2.0 as one easy-to-use widget with full page history manipulation operations, simplified pop event delegation and powerful nesting.
version: 0.1.1+3
version: 0.1.2
repository: https://github.com/LucasAschenbach/advanced_navigator

environment:
Expand Down

0 comments on commit a628acf

Please sign in to comment.