Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BuildContext parameter for onGeneratePages and onPopPage #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/src/url_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class UrlRouter extends RouterDelegate<String> with ChangeNotifier, PopNavigator
/// Should build a stack of pages, based on the current location.
/// This is technically optional, as you could decide to implement your
/// own custom navigator inside the `builder`
final List<Page<dynamic>> Function(UrlRouter router)? onGeneratePages;
final List<Page<dynamic>> Function(BuildContext context, UrlRouter router)? onGeneratePages;

/// Wrap widgets around the [MaterialApp]s [Navigator] widget.
/// Primarily used for providing scaffolding like a `SideBar`, `TitleBar` around the page stack.
/// Also useful for when you would like to discard the provided [Navigator], and implement your own.
final Widget Function(UrlRouter router, Widget navigator)? builder;

/// Optionally provide a way for the parent to implement custom `onPopPage` logic.
final PopPageCallback? onPopPage;
final bool Function(BuildContext context, Route<dynamic> route, dynamic result)? onPopPage;

/// Optionally invoked just prior to the location being changed.
/// Allows a parent class to protect or redirect certain routes. The callback can return the original url to allow the location change,
Expand Down Expand Up @@ -85,7 +85,7 @@ class UrlRouter extends RouterDelegate<String> with ChangeNotifier, PopNavigator

@override
Widget build(BuildContext context) {
final pages = onGeneratePages?.call(this) ?? [];
final pages = onGeneratePages?.call(context, this) ?? [];
//TODO: Add more use cases for this, figure out if this is really what we want to do, or should we just always return false.
bool handlePopPage(Route<dynamic> route, dynamic settings) {
if (pages.length > 1 && route.didPop(settings)) {
Expand All @@ -97,7 +97,9 @@ class UrlRouter extends RouterDelegate<String> with ChangeNotifier, PopNavigator
this.context = context;
Widget content = Navigator(
key: _navKey,
onPopPage: onPopPage ?? handlePopPage,
onPopPage: onPopPage != null
? (route, settings) => onPopPage!(context, route, settings)
: handlePopPage,
pages: pages,
);
if (builder != null) {
Expand Down
2 changes: 1 addition & 1 deletion test/core_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ UrlRouter getSinglePageRouter([String? initial, OnChangingCallback? onChanging])
url: initial ?? '/',
builder: (_, navigator) => Container(child: navigator),
onChanging: onChanging,
onGeneratePages: (router) {
onGeneratePages: (context, router) {
return [
/// Create a single page that renders the current url
MaterialPage(
Expand Down
Loading