Flutter 我的应用程序关闭时,我按下从导航抽屉中选定的项目在颤振

Flutter 我的应用程序关闭时,我按下从导航抽屉中选定的项目在颤振,flutter,Flutter,我有导航抽屉,我从中选择了一个项目,当我选择了第二个项目后,当我按下后退按钮,当选择第二个项目应用程序关闭后,当我按下后退按钮时,我需要继续第一个项目 @override Widget build(BuildContext context) { List<Widget> drawerOptions = []; for (var i = 0; i < drawerItems.length; i++) { var d = drawerItems[i]

我有导航抽屉,我从中选择了一个项目,当我选择了第二个项目后,当我按下后退按钮,当选择第二个项目应用程序关闭后,当我按下后退按钮时,我需要继续第一个项目

@override
  Widget build(BuildContext context) {
    List<Widget> drawerOptions = [];
    for (var i = 0; i < drawerItems.length; i++) {
      var d = drawerItems[i];
      drawerOptions.add(new ListTile(
        leading: new Icon(d.icon),
        title: new Text(
          d.title,
          style: new TextStyle(fontSize: 14.0, fontWeight: FontWeight.w400),
        ),
        selected: i == _selectedIndex,
        onTap: () => _onSelectItem(i),
      ));
    }

    return new Scaffold(
      appBar: SearchBar(
        loader: QuerySetLoader<ProductModel>(
          querySetCall: _getItemListForQuery,
          itemBuilder: _buildItemWidget,
          loadOnEachChange: true,
          animateChanges: true,
        ),
        defaultBar: AppBar(
          title: Text('Home'),
        ),
      ),
      drawer: new Drawer(
        child: SingleChildScrollView(
          child: new Column(
            children: <Widget>[
              DecoratedBox(
                position: DecorationPosition.background,
                decoration: BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage('assets/bac_image.png'),
                      fit: BoxFit.cover),
                ),
                child: Column(
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.all(20.0),
                      child: new Image(
                        image: new AssetImage("assets/blik_mobile.png"),
                        height: 100,
                        width: 100,
                      ),
                    ),
                    Column(children: drawerOptions)
                  ],
                ),

              )
            ],
          ),
        ),
      ),
      body: _getDrawerItemScreen(_selectedIndex),
      bottomNavigationBar: BottomNavigationBar(
        onTap: (int index) {
          setState(() {
            this.index = index;
            // Navigator.of(context).pop();
          });
          _navigateToScreens(index);
        },
        type: BottomNavigationBarType.fixed,
        currentIndex: index,
        items: [
          BottomNavigationBarItem(
              title: Text('Home'),
              icon: Icon(
                Icons.home,
                color: Colors.black,
              )),
          BottomNavigationBarItem(
              title: Text('Categories'),
              icon: Icon(Icons.dashboard, color: Colors.black)),
          BottomNavigationBarItem(
              title: Text('Cart'),
              icon: Icon(Icons.shopping_cart, color: Colors.black)),
          BottomNavigationBarItem(
              title: Text('WishList'),
              icon: Icon(Icons.favorite, color: Colors.black)),
          BottomNavigationBarItem(
              title: Text('Profile'),
              icon: Icon(Icons.person, color: Colors.black)),
        ],
      ),
    );
  }
获取所选抽屉屏幕的方法在此处

_getDrawerItemScreen(int pos) {
    switch (pos) {
      case 0:
        return new FirstScreen(drawerItem: drawerItems[_selectedIndex]);
      case 1:
        return new OrderHistory(drawerItem: drawerItems[_selectedIndex]);
      case 2:
        return new WalletScreen(
          drawerItem: drawerItems[_selectedIndex],
        );
      case 3:
        return new AddressList(drawerItem: drawerItems[_selectedIndex]);
      case 5:
        return new AboutUs(drawerItem: drawerItems[_selectedIndex]);
      // return new AddAddress(drawerItem: drawerItems[_selectedIndex],);
      default:
        return new FirstScreen(drawerItem: drawerItems[_selectedIndex]);
    }
  }

在从导航抽屉中选择任何项目后,我想返回处理

如果需要控制屏幕堆栈,可以使用

onPressed: () {
  Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => SecondRoute()),
  );
}
详细信息参考

如果要处理“后退”按钮
你可以用WillPopScope把脚手架包起来

return  WillPopScope(
        onWillPop: () => _exitApp(context),
        child:  Scaffold(
            appBar:  AppBar(
              title:  Text("Navigation Demo"),
并询问用户是否要退出此应用程序或当前屏幕

Future<bool> _exitApp(BuildContext context) {
  return showDialog(
        context: context,
        child:  AlertDialog(
          title:  Text('Do you want to exit this application?'),
          content:  Text('We hate to see you leave...'),
          actions: <Widget>[
             FlatButton(
              onPressed: () => Navigator.of(context).pop(false),
              child:  Text('No'),
            ),
             FlatButton(
              onPressed: () => Navigator.of(context).pop(true),
              child:  Text('Yes'),
            ),
          ],
        ),
      ) ??
      false;
}
Future\u exitApp(构建上下文){
返回显示对话框(
上下文:上下文,
子:警报对话框(
标题:Text('是否要退出此应用程序?'),
内容:文本('我们讨厌看到你离开…'),
行动:[
扁平按钮(
onPressed:()=>Navigator.of(context.pop)(false),
child:Text('No'),
),
扁平按钮(
onPressed:()=>Navigator.of(context.pop)(true),
子项:文本('Yes'),
),
],
),
) ??
虚假的;
}
详细参考

Future<bool> _exitApp(BuildContext context) {
  return showDialog(
        context: context,
        child:  AlertDialog(
          title:  Text('Do you want to exit this application?'),
          content:  Text('We hate to see you leave...'),
          actions: <Widget>[
             FlatButton(
              onPressed: () => Navigator.of(context).pop(false),
              child:  Text('No'),
            ),
             FlatButton(
              onPressed: () => Navigator.of(context).pop(true),
              child:  Text('Yes'),
            ),
          ],
        ),
      ) ??
      false;
}