Search code examples
flutterdartnavigation

How to push and pop until to a specific route in flutter


I am making an application, In my sign out dialog, i got this code.

  Navigator.of(context).pushAndRemoveUntil(
                    MaterialPageRoute(builder: (context) {
                  return AuthScreen();
                }), (route) {
                  // if( route is (MaterialPageRoute('/')))
                  // {

                  // }
                  // print(route);
                  return false;
                });


I just want to push AuthScreen and remove until the route screen. How to do that?

So after this code, stack contains root screen, and AuthScreen.


Solution

  • You can do this:

    Navigator.pushAndRemoveUntil(
        context,   
        MaterialPageRoute(builder: (BuildContext context) => AuthScreen()), 
        ModalRoute.withName('/') // Replace this with your root screen's route name (usually '/')
    );
    

    Another way you can try is:

    Navigator.pushAndRemoveUntil(
      context,
      MaterialPageRoute(builder: (BuildContext context) => AuthScreen()),
      (Route<dynamic> route) => route is RootPage (your root page's type)
    );
    

    Yet another one:

    Navigator.pushAndRemoveUntil(
      context,
      MaterialPageRoute(builder: (BuildContext context) => AuthScreen()),
      (Route<dynamic> route) => route.isFirst (pop until your first page)
    );
    

    Sample app:

    import 'dart:async';
    
    import 'package:flutter/material.dart';
    import 'package:in_app_update/in_app_update.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          initialRoute: '/',
          routes: {
            '/': (context) => MyHome(),
          },
        );
      }
    }
    
    class MyHome extends StatelessWidget {
      const MyHome({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Sample App'),
          ),
          body: Center(
            child: TextButton(
              onPressed: () => Navigator.push(
                  context, MaterialPageRoute(builder: (context) => MyChild())),
              child: Text('To child page'),
            ),
          ),
        );
      }
    }
    
    class MyChild extends StatelessWidget {
      const MyChild({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('My Child'),
          ),
          body: Center(
            child: TextButton(
              onPressed: () => Navigator.pushAndRemoveUntil(
                  context,
                  MaterialPageRoute(
                      builder: (BuildContext context) => MySecondChild()),
                  ModalRoute.withName('/')),
              child: Text('Push to Second Child and remove until homepage'),
            ),
          ),
        );
      }
    }
    
    class MySecondChild extends StatelessWidget {
      const MySecondChild({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('My Second Child'),
          ),
          body: Center(
            child: TextButton(
              onPressed: () => Navigator.pop(context),
              child: Text('Pop'),
            ),
          ),
        );
      }
    }