0

I'm working on a Flutter web PWA that gets installed on Android devices, and I'm running into a frustrating navigation issue with the browser's back button.

The Problem My app has a typical flow: Home → Page1 → Page2 → Dialog. When users navigate through multiple pages and open a dialog, the back button works once to close the dialog, but then it either stops working entirely or causes the PWA to exit unexpectedly instead of going back through the page history.

What Should Happen

  • Back button closes dialog → back to Page2

  • Back button again → Page2 to Page1

  • Back button again → Page1 to Home

  • Back button again → proper app exit

What Actually Happens

  • Back button closes dialog → back to Page2 ✅

  • Back button again → button becomes disabled or PWA exits ❌

        import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'PWA Navigation Issue',
          home: HomePage(),
        );
      }
    }
    
    class HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('Home')),
          body: Center(
            child: ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => Page1()),
                );
              },
              child: Text('Go to Page 1'),
            ),
          ),
        );
      }
    }
    
    class Page1 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('Page 1')),
          body: Center(
            child: ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => Page2()),
                );
              },
              child: Text('Go to Page 2'),
            ),
          ),
        );
      }
    }
    
    class Page2 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('Page 2')),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: () {
                    showDialog(
                      context: context,
                      builder: (context) => AlertDialog(
                        title: Text('Dialog'),
                        content: Text('This is a dialog'),
                        actions: [
                          TextButton(
                            onPressed: () => Navigator.pop(context),
                            child: Text('Close'),
                          ),
                        ],
                      ),
                    );
                  },
                  child: Text('Show Dialog'),
                ),
                SizedBox(height: 20),
                ElevatedButton(
                  onPressed: () {
                    showModalBottomSheet(
                      context: context,
                      builder: (context) => Container(
                        height: 300,
                        child: Center(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text('Bottom Sheet'),
                              ElevatedButton(
                                onPressed: () => Navigator.pop(context),
                                child: Text('Close'),
                              ),
                            ],
                          ),
                        ),
                      ),
                    );
                  },
                  child: Text('Show Bottom Sheet'),
                ),
              ],
            ),
          ),
        );
      }
    }

 

**

What do I need to change or implement to make the browser back button work properly for multi-level navigation in my Flutter web PWA?**

2
  • what dialog do you mean? I don't see any call to showDialog function Commented Jul 15 at 11:52
  • @pskink i have update that code.. Commented Jul 15 at 18:44

1 Answer 1

0

You're experiencing this issue because the default Navigator in Flutter doesn't properly manage the browser's history stack on web when using Navigator.push.

go_router is built for this and handles browser back button behavior correctly. It keeps the URL and navigation stack in sync across pages, dialogs, and modals.

Define routes with GoRouter:

final _router = GoRouter(
  routes: [
    GoRoute(path: '/', builder: (context, state) => HomePage()),
    GoRoute(path: '/page1', builder: (context, state) => Page1()),
    GoRoute(path: '/page2', builder:  (context, state) => Page2()),
  ],
);

Wrap your app with MaterialApp.router:

MaterialApp.router(
  // other configuration
  routerConfig: _router,
);
  • Navigate using context.push('/page1'), etc

  • Use showDialog or showModalBottomSheet as usual — the back button will close them first, then navigate back correctly through the stack.

for futher details and configuration check out the documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.