简体   繁体   English

Flutter:按后退按钮关闭两个屏幕

[英]Flutter : Pressing back button closes two screens

I have navigate from Dashboard to Some Screen called 'A'.我已从仪表板导航到名为“A”的某个屏幕。 In that screen, I have shown a listing using ListView Builder.在该屏幕中,我使用 ListView Builder 显示了一个列表。 In that, clicking on item in ListView navigate to another Screen called "B".在那,单击 ListView 中的项目导航到另一个名为“B”的屏幕。 When I press back button in that Screen "B" its navigate to Dashboard.当我在该屏幕“B”中按下后退按钮时,它会导航到仪表板。 But it has to navigate to Screen "A".但它必须导航到屏幕“A”。 Please help me to resolve this issue.请帮我解决这个问题。

In Dashboard, I have use below code to Navigate to Screen "A",在仪表板中,我使用以下代码导航到屏幕“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",在屏幕“A”中,我使用以下代码导航到屏幕“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.当我在屏幕“B”中按下后退按钮时,它会导航到仪表板。 But I need to navigate to "A" as per the backstacks.但是我需要根据 backstacks 导航到“A”。

Please help me!请帮我!

So as discussed in the comments.所以正如评论中所讨论的那样。 using Navigator.of(context,rootNavigator: true).push(...) fixed the issue.使用Navigator.of(context,rootNavigator: true).push(...)解决了这个问题。
But why did you faced this issue at first ?但你为什么一开始会遇到这个问题? because you have multiple MaterialApp in your app.因为您的应用中有多个MaterialApp you have to keep only the one in main.dart as root widget.你只需要在main.dart保留一个作为根小部件。 so you have two options:所以你有两个选择:

  1. Use only one MaterialApp as root widget and call Navigator.of(context).push(...)仅使用一个MaterialApp作为根小部件并调用Navigator.of(context).push(...)
  2. Have multiple MaterialApp and use Navigator.of(context,rootNavigator: true).push(...)有多个MaterialApp并使用Navigator.of(context,rootNavigator: true).push(...)

If you need my advice, use 1.如果您需要我的建议,请使用 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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