Skip to content

Nested Navigator breaks Predictive Back (PopScope) #139903

@maRci002

Description

@maRci002

Is there an existing issue for this?

Steps to reproduce

  1. enable predictive back
  2. Click on Rebuild Outer Navigator button
  3. Try Android gesture back

It seems that when the Outer Navigator is rebuilt and the Inner one is not (since I'm using the constant widget const MaterialPage(child: OuterScreen())), the Outer Navigator's NavigationNotification.canHandlePop will be reported to the engine.

/// The default value for [onNavigationNotification].
///
/// Does nothing and stops bubbling if the app is detached. Otherwise, updates
/// the platform with [NavigationNotification.canHandlePop] and stops
/// bubbling.
bool _defaultOnNavigationNotification(NavigationNotification notification) {
switch (_appLifecycleState) {
case null:
case AppLifecycleState.detached:
case AppLifecycleState.inactive:
// Avoid updating the engine when the app isn't ready.
return true;
case AppLifecycleState.resumed:
case AppLifecycleState.hidden:
case AppLifecycleState.paused:
SystemNavigator.setFrameworkHandlesBack(notification.canHandlePop);
return true;
}
}

Expected results

When the widget tree contains PopScope.canPop: false, Predictive Back is disabled.

Actual results

When the widget tree contains PopScope.canPop: false, Predictive Back is enabled.

Code sample

Code sample
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class RouteParser extends RouteInformationParser<RouteInformation> {
  RouteParser();

  @override
  Future<RouteInformation> parseRouteInformation(RouteInformation routeInformation) => SynchronousFuture(routeInformation);
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static final outerRouterDelegate = OuterRouterDelegate();

  @override
  void dispose() {
    outerRouterDelegate.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'PopScope Test',
      routerDelegate: outerRouterDelegate,
      routeInformationParser: RouteParser(),
    );
  }
}

class OuterRouterDelegate extends RouterDelegate<RouteInformation> with ChangeNotifier {
  List<Page<dynamic>> get pages => [const MaterialPage(child: OuterScreen())];

  @override
  Widget build(BuildContext context) {
    return Navigator(
      pages: pages,
      onPopPage: (_, __) => false,
    );
  }

  @override
  Future<bool> popRoute() async => true;

  @override
  Future<void> setNewRoutePath(RouteInformation configuration) async {}
}

class OuterScreen extends StatelessWidget {
  const OuterScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return const Column(
      children: [
        Expanded(child: Text('Outer')),
        SizedBox(width: 8),
        Flexible(child: InnerScreen()),
      ],
    );
  }
}

class InnerScreen extends StatefulWidget {
  const InnerScreen({super.key});

  @override
  State<InnerScreen> createState() => _InnerScreenState();
}

class _InnerScreenState extends State<InnerScreen> {
  static final innerRouterDelegate = InnerRouterDelegate();

  @override
  void dispose() {
    innerRouterDelegate.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Router(
      routerDelegate: innerRouterDelegate,
      routeInformationParser: RouteParser(),
    );
  }
}

class InnerRouterDelegate extends RouterDelegate<RouteInformation> with ChangeNotifier {
  @override
  Widget build(BuildContext context) {
    return Navigator(
      pages: [
        MaterialPage(
          child: PopScope(
            canPop: false,
            onPopInvoked: print,
            child: Column(
              children: [
                const Text('Inner'),
                const SizedBox(height: 8),
                ElevatedButton(onPressed: _MyAppState.outerRouterDelegate.notifyListeners, child: const Text('Rebuild Outer Navigator')),
                const SizedBox(height: 8),
                ElevatedButton(onPressed: _InnerScreenState.innerRouterDelegate.notifyListeners, child: const Text('Rebuild Inner Navigator')),
              ],
            ),
          ),
        ),
      ],
      onPopPage: (_, __) => false,
    );
  }

  @override
  Future<bool> popRoute() async => true;

  @override
  Future<void> setNewRoutePath(RouteInformation configuration) async {}
}

Screenshots or Video

Screenshots / Video demonstration
nested_navigator_popscope.mp4

Logs

No response

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.16.3, on Microsoft Windows [Version 10.0.19045.3693], locale hu-HU)
    • Flutter version 3.16.3 on channel stable at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b0366e0a3f (5 days ago), 2023-12-05 19:46:39 -0800
    • Engine revision 54a7145303
    • Dart version 3.2.3
    • DevTools version 2.28.4

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at C:\Users\maRci002\AppData\Local\Android\Sdk
    • Platform android-33, build-tools 33.0.2
    • ANDROID_HOME = C:\Users\maRci002\AppData\Local\Android\Sdk
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.7.0)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.7.34003.232
    • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2023.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314)

[√] VS Code (version 1.85.0)
    • VS Code at C:\Users\maRci002\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension can be installed from:
       https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[√] Connected device (4 available)
    • sdk gphone x86 64 (mobile) • emulator-5554 • android-x64    • Android 13 (API 33) (emulator)
    • Windows (desktop)          • windows       • windows-x64    • Microsoft Windows [Version 10.0.19045.3693]
    • Chrome (web)               • chrome        • web-javascript • Google Chrome 120.0.6099.71
    • Edge (web)                 • edge          • web-javascript • Microsoft Edge 120.0.2210.61

[√] Network resources
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listf: routesNavigator, Router, and related APIs.found in release: 3.16Found to occur in 3.16found in release: 3.18Found to occur in 3.18frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions