简体   繁体   中英

Flutter : Pressing back button closes two screens

I have navigate from Dashboard to Some Screen called 'A'. In that screen, I have shown a listing using ListView Builder. In that, clicking on item in ListView navigate to another Screen called "B". When I press back button in that Screen "B" its navigate to Dashboard. But it has to navigate to Screen "A". Please help me to resolve this issue.

In Dashboard, I have use below code to Navigate to Screen "A",

  _showSnackBar(BuildContext context, Item item) {
    switch(item.name)
    {
      
      case "Disputes":
        Navigator.push(context, MaterialPageRoute(builder: (context)=> Disputes()));
        break;
        
    }
  }
}

In Screen "A", I have use below code to Navigate to Screen "B",

 Card(
                                                child: ListTile(
                                                  onTap: () {
                                                    Navigator.push(
                                                        context,
                                                        MaterialPageRoute(
                                                            builder: (context) =>
                                                                SubmitDisputes(disputesId: disputeResList[index].id.toString())));
                                                  },
                                                  trailing: Icon(
                                                      Icons.remove_red_eye),
                                               
                                              ));
                                        }),
                                  ),
                                ),

When I press back button in Screen "B", its navigate to Dashboard. But I need to navigate to "A" as per the backstacks.

Please help me!

So as discussed in the comments. using Navigator.of(context,rootNavigator: true).push(...) fixed the issue.
But why did you faced this issue at first ? because you have multiple MaterialApp in your app. you have to keep only the one in main.dart as root widget. so you have two options:

  1. Use only one MaterialApp as root widget and call Navigator.of(context).push(...)
  2. Have multiple MaterialApp and use Navigator.of(context,rootNavigator: true).push(...)

If you need my advice, use 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM