Skip to content
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
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ linter:
analyzer:
exclude:
- example/
- build/**
84 changes: 42 additions & 42 deletions lib/src/router/stacked_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ abstract class StackedPage<T> extends Page<T> {
this.maintainState = true,
this.opaque = true,
LocalKey? key,
}) : _child = child is RouteWrapper
? WrappedRoute(
child: child as RouteWrapper,
)
: child,
super(
restorationId: routeData.name,
name: routeData.name,
arguments: routeData.route.args,
);
}) : _child = child is RouteWrapper
? WrappedRoute(child: child as RouteWrapper)
: child,
super(
restorationId: routeData.name,
name: routeData.name,
arguments: routeData.route.args,
);

@override
bool canUpdate(Page<dynamic> other) {
Expand All @@ -50,20 +48,20 @@ abstract class StackedPage<T> extends Page<T> {
LocalKey get routeKey => routeData.key;

Widget buildPage(BuildContext context) {
return RouteDataScope(
routeData: routeData,
child: _child,
);
return RouteDataScope(routeData: routeData, child: _child);
}

Route<T> onCreateRoute(BuildContext context);

@override
Route<T> createRoute(BuildContext context) {
// Navigator 2.0 can call createRoute() multiple times on the same page
// (e.g. when the Navigator is rebuilt), so only the first popped route
// completes the page's completer.
return onCreateRoute(context)
..popped.then(
_popCompleter.complete,
);
..popped.then((value) {
if (!_popCompleter.isCompleted) _popCompleter.complete(value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guard hides the symptom rather than fixing it. When createRoute runs a second time on the same StackedPage (for example the page resurrected after an imperative removal, as described on route_navigator.dart), the page completer has already completed with the first route's result, usually null for a removal. Whatever the live second route pops with is then dropped silently, so a caller awaiting router.push<T>() got null early and never sees the real result. Before this change the case showed up as a StateError. Now it loses data without any signal. Consider at least an assert or debug log when the completer is already completed.

});
}
}

Expand All @@ -84,9 +82,8 @@ class MaterialPageX<T> extends StackedPage<T> {

class PageBasedMaterialPageRoute<T> extends PageRoute<T>
with MaterialRouteTransitionMixin<T> {
PageBasedMaterialPageRoute({
required StackedPage page,
}) : super(settings: page);
PageBasedMaterialPageRoute({required StackedPage page})
: super(settings: page);

StackedPage get _page => settings as StackedPage;

Expand All @@ -107,9 +104,8 @@ class PageBasedMaterialPageRoute<T> extends PageRoute<T>

class _CustomPageBasedPageRouteBuilder<T> extends PageRoute<T>
with _CustomPageRouteTransitionMixin<T> {
_CustomPageBasedPageRouteBuilder({
required StackedPage page,
}) : super(settings: page);
_CustomPageBasedPageRouteBuilder({required StackedPage page})
: super(settings: page);

@override
Widget buildContent(BuildContext context) => _page.buildPage(context);
Expand All @@ -126,9 +122,8 @@ class _CustomPageBasedPageRouteBuilder<T> extends PageRoute<T>

class _NoAnimationPageRouteBuilder<T> extends PageRoute<T>
with _NoAnimationPageRouteTransitionMixin<T> {
_NoAnimationPageRouteBuilder({
required StackedPage page,
}) : super(settings: page);
_NoAnimationPageRouteBuilder({required StackedPage page})
: super(settings: page);

@override
Widget buildContent(BuildContext context) => _page.buildPage(context);
Expand Down Expand Up @@ -200,14 +195,12 @@ mixin _CustomPageRouteTransitionMixin<T> on PageRoute<T> {
Widget buildContent(BuildContext context);

@override
Duration get transitionDuration => Duration(
milliseconds: _page.durationInMilliseconds,
);
Duration get transitionDuration =>
Duration(milliseconds: _page.durationInMilliseconds);

@override
Duration get reverseTransitionDuration => Duration(
milliseconds: _page.reverseDurationInMilliseconds,
);
Duration get reverseTransitionDuration =>
Duration(milliseconds: _page.reverseDurationInMilliseconds);

@override
bool get barrierDismissible => _page.barrierDismissible;
Expand Down Expand Up @@ -247,16 +240,21 @@ mixin _CustomPageRouteTransitionMixin<T> on PageRoute<T> {
}

Widget _defaultTransitionsBuilder(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child) {
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) {
return child;
}

@override
Widget buildTransitions(BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation, Widget child) {
Widget buildTransitions(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) {
final transitionsBuilder =
_page.transitionsBuilder ?? _defaultTransitionsBuilder;
return transitionsBuilder(context, animation, secondaryAnimation, child);
Expand Down Expand Up @@ -293,9 +291,8 @@ class CupertinoPageX<T> extends _TitledAutoRoutePage<T> {

class _PageBasedCupertinoPageRoute<T> extends PageRoute<T>
with CustomCupertinoRouteTransitionMixin<T> {
_PageBasedCupertinoPageRoute({
required _TitledAutoRoutePage page,
}) : super(settings: page);
_PageBasedCupertinoPageRoute({required _TitledAutoRoutePage page})
: super(settings: page);

_TitledAutoRoutePage get _page => settings as _TitledAutoRoutePage;

Expand Down Expand Up @@ -336,7 +333,10 @@ class AdaptivePage<T> extends _TitledAutoRoutePage<T> {
}

typedef CustomRouteBuilder = Route<T> Function<T>(
BuildContext context, Widget child, CustomPage<T> page);
BuildContext context,
Widget child,
CustomPage<T> page,
);

class CustomPage<T> extends StackedPage<T> {
final int durationInMilliseconds;
Expand Down
24 changes: 19 additions & 5 deletions lib/src/router/widgets/route_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,26 @@ class RouteNavigatorState extends State<RouteNavigator> {
restorationScopeId:
widget.navRestorationScopeId ?? widget.router.routeData.name,
pages: widget.router.stack,
onDidRemovePage: (page) {
if (page is StackedPage) {
var routeData = page.routeData;
widget.router.removeRoute(routeData);
widget.didPop?.call(routeData.route, null);
// ignore: deprecated_member_use
onPopPage: (route, result) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regression: imperative page removals no longer sync the router stack. With onPopPage set instead of onDidRemovePage, Flutter only calls back for Navigator.pop / maybePop / popUntil. Every other imperative removal of a page-backed route (Navigator.removeRoute, removeRouteBelow, pushReplacement, pushAndRemoveUntil, replace/replaceRouteBelow) goes through _RouteEntry.complete(..., imperativeRemoval: true), which only calls widget.onDidRemovePage (see navigator.dart handlePop/handlePush). The old code handled those through onDidRemovePage.

Scenario: app code calls Navigator.of(context).pushAndRemoveUntil(...) or removeRoute(route) on a StackedPage route. The route gets disposed, but its StackedPage stays in router._pages. removeRoute/notifyAll never run, so the URL, guards and didPop go stale. On the next notifyAll rebuild the Navigator finds a page with no entry and calls createRoute on the same StackedPage again, so the removed screen comes back. That is also the "createRoute called twice on the same page" case the completer guard in stacked_page.dart now hides silently.

// CRITICAL: Using deprecated onPopPage instead of onDidRemovePage
// because we MUST update router state BEFORE the pop completes.
// This is essential for Android predictive back gestures to work
// without animation glitches (double animations or forward replays).
// onDidRemovePage is called too late (after page removal) and causes
// page list mismatch during the animation, triggering visual glitches.
// See: https://github.com/flutter/flutter/issues/new (migration path unclear)
if (!route.didPop(result)) {
return false;
}

if (route.settings is StackedPage) {
var page = route.settings as StackedPage;
widget.router.removeRoute(page.routeData);
widget.didPop?.call(page.routeData.route, result);
}

return true;
},
)
: widget.placeholder?.call(context) ??
Expand Down