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

PopScope not working on web browser back button #1944

Open
rsegecin opened this issue Apr 26, 2024 · 7 comments
Open

PopScope not working on web browser back button #1944

rsegecin opened this issue Apr 26, 2024 · 7 comments

Comments

@rsegecin
Copy link

Hi, PopScope seems to be not working when the back button in the web browser is pressed. It does work when you navigate to the page using Navigator.push but not context.pushRoute.

Here is the example code:

auto_router.dart:

import 'package:auto_route/auto_route.dart';
import 'package:phonetest/auto_router.gr.dart';

@AutoRouterConfig()
class AppRouter extends $AppRouter {
  @override
  List<AutoRoute> get routes => [
        AutoRoute(
          path: '/',
          page: FirstRoute.page,
        ),
        AutoRoute(
          path: '/second-route',
          page: SecondRoute.page,
        ),
      ];
}

main.dart:

import 'package:auto_route/auto_route.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:phonetest/auto_router.dart';
import 'package:phonetest/auto_router.gr.dart';

void main() {
  usePathUrlStrategy();

  WidgetsFlutterBinding.ensureInitialized();

  final _appRouter = AppRouter();

  runApp(CupertinoApp.router(
    title: 'Navigation Basics',
    routerConfig: _appRouter.config(),
  ));
}

@RoutePage()
class FirstScreen extends StatelessWidget {
  const FirstScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: const CupertinoNavigationBar(
        middle: Text('First Route'),
      ),
      child: Center(
        child: CupertinoButton(
          child: const Text('Open route'),
          onPressed: () {
            context.pushRoute(const SecondRoute());

            // Navigator.push(
            //   context,
            //   MaterialPageRoute(builder: (context) => const SecondScreen()),
            // );
          },
        ),
      ),
    );
  }
}

@RoutePage()
class SecondScreen extends StatelessWidget {
  const SecondScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: const CupertinoNavigationBar(
        middle: Text('Second Route'),
      ),
      child: PopScope(
        canPop: false,
        onPopInvoked: (didPop) {
          if (kDebugMode) {
            print("didpop $didPop");
          }
        },
        child: Center(
          child: CupertinoButton(
            onPressed: () {
              context.maybePop();
              //Navigator.pop(context);
            },
            child: const Text('Go back!'),
          ),
        ),
      ),
    );
  }
}

flutter doctor -v

flutter doctor -v
[√] Flutter (Channel stable, 3.19.5, on Microsoft Windows [Version
    10.0.19045.4291], locale en-US)
    • Flutter version 3.19.5 on channel stable at C:\Users\Rinaldi\Programs\flutter    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 300451adae (4 weeks ago), 2024-03-27 21:54:07 -0500       
    • Engine revision e76c956498
    • Dart version 3.3.3
    • DevTools version 2.31.1

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

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\Rinaldi\AppData\Local\Android\sdk
    • Platform android-33, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java  
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • 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.8.3)
    • Visual Studio at D:\Program Files (x86)\VisualStudio2022
    • Visual Studio Community 2022 version 17.8.34330.188
    • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2020.3)
    • 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 11.0.10+0-b96-7249189)       

[√] VS Code (version 1.88.1)
    • VS Code at C:\Users\Rinaldi\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.86.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version    
      10.0.19045.4291]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 124.0.6367.78   
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 124.0.2478.51  

[√] Network resources
    • All expected network resources are available.
@iagoandrei
Copy link
Contributor

Same here

@arturoszulc
Copy link

I'm also looking for a way to either disable browser back button or at least recognize it and show a warning dialog

@t-unit
Copy link

t-unit commented Jun 7, 2024

Also experiencing this issue

@Milad-Akarie
Copy link
Owner

Hello @ALL please understand that clicking the browser's back button DOES not trigger a pop event, there for it's not something PopScope can handle, clicking the back button on the browser navigates to whatever entry is previous to current in the browser's entries stack, let me try to visualize it

if you navigate to /path-1
then navigate to /path-2
clicking the back button will just navigate you back to /path-1, it does not trigger a pop event.

when it comes to disabling/intercepting the browser's back button the short answer is "You can't", that being said there are a couple hacks you can do but none of theme are guaranteed.

refer to this link to learn more

@Milad-Akarie Milad-Akarie pinned this issue Jun 13, 2024
@rsegecin
Copy link
Author

rsegecin commented Jun 15, 2024

Hi everyone,

I'm not sure if my issue was evident in the original post and it seems that it got a bit sidetracked. The PopScope is not triggered when I navigate away from a page that was pushed using context.pushRoute. So I'm navigating eg. from pageOne to pageTwo and when pageTwo is popped PopScope is not triggered. While Navigator.push works, but it's not intended for use with AutoRoute, and routes can't be pushed with it.

@geronimol
Copy link

I think this issue also happens with go_router, and seems they are waiting for a new Flutter release to fix
flutter/flutter#138737 (comment)

@rsegecin
Copy link
Author

rsegecin commented Jul 8, 2024

I think this issue also happens with go_router, and seems they are waiting for a new Flutter release to fix flutter/flutter#138737 (comment)

I think its a coincidence of go_router having the same issue and its not flutter's fault as it does work when Navigating with Navigator.push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants