zoukankan      html  css  js  c++  java
  • Flutter

    前一篇博客讲到了如何创建侧滑菜单,但是再实际使用过程中,会发现,这个策划菜单只能在首页侧滑出来。

    当导航到其他页面后,侧滑就不管用了。这也有点不符合良好的用户体验设计。Google Play就是很好的例子,她就是可以几乎在所有的页面上侧滑出来(一些特定的页面除外)。

    下面看看如何来实现这一功能。

     其实原理很简单,就是在每一个page里面都加上drawer。

    我的这个是比较笨的方法,如果谁有更好用的,请告诉我。

    1.首先将drawer封装成一个widget:drawerEx

    class drawerEx extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Drawer(
          child: new ListView(
            children: <Widget>[
              new UserAccountsDrawerHeader(
                accountName: Text("小薇识花"),
                accountEmail: Text("flutter@gmail.com"),
                currentAccountPicture: new GestureDetector(
                  onTap: () => Fluttertoast.showToast(
                      msg: "flutter@gmail.com", toastLength: Toast.LENGTH_LONG),
                  child: new CircleAvatar(
                    backgroundImage: new ExactAssetImage("images/Head0.png"),
                  ),
                ),
                decoration: new BoxDecoration(
                  image: new DecorationImage(
                    fit: BoxFit.fill,
                    image: new ExactAssetImage("images/mm.jpg"),),),
              ),
              new ListTile(
                title: new Text("识花"),
                trailing: new Icon(Icons.local_florist),
                onTap: (){
                  Navigator.of(context).pop();
                  Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new HomePage()));
                  Navigator.pushNamedAndRemoveUntil(context, '/', (_) => false);
                },
              ),
              new ListTile(
                title: new Text("搜索"),
                trailing: new Icon(Icons.search),
                onTap: (){
                  Navigator.of(context).pop();
                  Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new SearchPage('Search Web')));
                },
              ),
              new Divider(),
              new ListTile(
                title: new Text("设置"),
                trailing: new Icon(Icons.settings),
                onTap: (){
                  Navigator.of(context).pop();
                  Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new SettingsPage('Settings Center')));
                },
              ),
            ],
          ),
        );
      }

    2.在各个页面pages的Scaffold里面引用drawerEx

    class SettingsPage extends StatelessWidget{
      final String pageText;
      
      SettingsPage(this.pageText);
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text('Settings Center'),
          ),
          drawer: new drawerEx(),//Refer your drawerEx widget
          body: new Center(
            child: new Text('Hello, Settings!'),
          ),
        );
      }
    }

    3.One more thing

    细心的可能会发现在drawer的一个ListTile里面,也就是首页,多了一句话

    Navigator.pushNamedAndRemoveUntil(context, '/', (_) => false);

    这句话的作用是当导航到首页的时候,导航历史会清除。

    比如,你导航了

    首页——》搜索——》设置——》首页——》设置

     ①             ②             ③            ①           ③

    此时,你按下返回键,则回到了“首页”,在点击返回键,则退出App。

    如果你不添加那句话,那么按返回键,则一次出现③①③②①,这个顺序显然不是我们想要的,并且用户也不希望这样导航。

  • 相关阅读:
    53分(我的所有)请教:关于ClientDataSet新增记录问题(请富翁们关注!谢谢)
    DBGrid显示行号的几种方法
    现在序号加上,但怎么控制这一列不能拖动,换句话说不能获取焦点?
    Delphi
    巧妙修复delphi文件关联
    cxGrid中有没有办法操作单个cell是否只读?
    delphi cxgrid 使用方法
    Delphi XE2 的控件安装方法。
    舟山牙医 君子慎独 让你的DBGrid竖着站
    DELPHI程序的自动升级功能的实现(AUTOUPDATE使用指南)
  • 原文地址:https://www.cnblogs.com/hupo376787/p/10027284.html
Copyright © 2011-2022 走看看