当我从导航抽屉中向后轻按选中的项目时,我的应用程序关闭了

时间:2019-10-02 07:01:07

标签: flutter

我有导航抽屉,我从中选择了一个项目,选择第二项应用程序后,当我按回去时选择第二项后,我需要在回去时继续进行第一项。

@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)),
        ],
      ),
    );
  }

在抽屉的选定项目上

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

在这里获取选择的抽屉屏幕方法

_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]);
    }
  }

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

1 个答案:

答案 0 :(得分:0)

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

onPressed: () {
  Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => SecondRoute()),
  );
}

详细参考https://medium.com/flutter-community/flutter-push-pop-push-1bb718b13c31

如果要处理后退按钮
您可以使用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;
}

详细信息参考https://codingwithjoe.com/flutter-navigation-how-to-prevent-navigation/