按下后退时导航抽屉不会转到上一页

时间:2020-04-06 15:26:13

标签: flutter

我的仪表板页面上有一个导航抽屉。当我单击每个项目时,抽屉上的列表标题很少,它将打开一个新页面。当我打开几页然后单击后退按钮时,该应用程序将关闭完全。我想查看打开的先前页面,然后在堆栈中找到最后一页时关闭该应用程序。

final drawerItems = [
new DrawerItem("Home", Icons.home),
new DrawerItem("Profile", Icons.person),
new DrawerItem("Change Password", Icons.lock),
new DrawerItem("Link CCID", Icons.calendar_view_day),
new DrawerItem("Settings", Icons.settings),
new DrawerItem("Allergies", Icons.opacity),
new DrawerItem("Logout", Icons.power_settings_new),
];

 _getDrawerItemScreen(int  pos) {
debugPrint("Position $pos");
if(pos==0)
{
  setState(() {
    title="Home";
  });
  return new Home(drawerItem: drawerItems[_selectedIndex]);
}
else if(pos==1)
{
  setState(() {
    title="My Profile";
  });
  return new ProfilePage(drawerItem: drawerItems[_selectedIndex],title: "My Profile",);
}
else if(pos==2)
{
  setState(() {
    title="Change Password";
  });
  return new ChangePassword(drawerItem: drawerItems[_selectedIndex],title: "Change Password",);
}
else if(pos==3)
{
  setState(() {
    title="LinkCCid";
  });
  return new LinkCcid(drawerItem: drawerItems[_selectedIndex],title: "LinkCCid",);
}
else if(pos==4)
{
  setState(() {
    title="Settings";
  });
//  Navigator.push(context, MaterialPageRoute(builder: (context) =>Settings(drawerItem: drawerItems[_selectedIndex],title: "Settings",)));
  return new Settings(drawerItem: drawerItems[_selectedIndex],title: "Settings",);
}
else if(pos==5)
{
  setState(() {
    title="Allergies";
  });
  return new Allergies(drawerItem: drawerItems[_selectedIndex],title: "Allergies",);
}
else if(pos==6)
{
  _logout();
}
}


 _onSelectItem(int index) {
setState(() {
  _selectedIndex = index;
  _getDrawerItemScreen(_selectedIndex);
});
Navigator.of(context).pop(); // close the drawer

}

1 个答案:

答案 0 :(得分:0)

您需要使用Navigator并使用其push()方法在Navigator堆栈中添加新页面。

您可以查看官方文档以实现基本导航:https://flutter.dev/docs/cookbook/navigation/navigation-basics

但是,我不建议您从导航器堆栈中的抽屉菜单项添加页面。用户可能将大量页面添加到堆栈中,并且必须按下很多后退按钮才能退出应用程序。