0
votes

I have created NavigationDrawer in flutter before navigation drawer screen appear to user I have 3 screen before it

  1. splash screen
  2. intro slider
  3. Login screen
  4. NavigationDrawer

I want to close my app when user press back button in android phone from navigationdrawer screen but it will shows black screen.

I have called below called for navigation from login to drawer

      Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(
                  builder: (BuildContext context) => NavigationDrawerDemo(),
                ),
                ModalRoute.withName('/LoginFieldForm'));

I called loginfieldform with

  Navigator.push(context,
    new MaterialPageRoute(builder: (context) =>new LoginFieldForm()));
4
can you show the code that pushes LoginFieldForm please? I might know what's wrongmagicleon94
@magicleon94 updated questionurvashi
That's what I thought. Check my answer, it should be what you needmagicleon94

4 Answers

0
votes

I think you have pushed the "LoginFieldForm" with the Navigator.push method and trying to remove it using the named routes.

For Ex :

// Puching a route with Navigator.push method

Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => LoginFieldForm()),
      );

// Removing using the named routes.

Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(
                  builder: (BuildContext context) => NavigationDrawerDemo(),
                ),
                ModalRoute.withName('/LoginFieldForm'));

You can only use ModalRoute.withName() if you have added the route using named routes.

0
votes

Your route probably has not a name set. If so, popping until the name is matched will result in popping everything, since the name you're looking for will never be found.

When you push LoginFieldForm ensure to pass a settings to the Route.

When pushing, you can do this:

MaterialPageRoute(
    settings: RouteSettings(name: "routeName"),
    builder: (context) => YourWidget(),
)

If you're within onGenerateRoute you already have settings passed as an argument to the onGenerateRoute function. In that case, just forward them to the MaterialPageRoute:

MaterialPageRoute(
    settings: settings, //these settings are arguments from the function
    builder: (context) => YourWidget(),
)
0
votes

To close the app you may use exit(0) of dart:io on back press.exit(0) help out to Exit the Dart VM process immediately with the given exit code.

This does not wait for any asynchronous operations to terminate. You may find more information about exit(0) here

Or

SystemChannels.platform.invokeMethod('SystemNavigator.pop');

For more detail you may check here

0
votes

Use-case: pushNamedAndRemoveUntil

 Navigator.of(context).pushNamedAndRemoveUntil('/LoginFieldForm', 
(Route<dynamic> route) => false);