Fork me on GitHub

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。

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

posted @   猫叔Vincent  阅读(1115)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示