Flutter Drawer

Flutter中的Drawer常用于实现侧滑菜单,通常与Scaffold的drawer属性结合使用。DrawerHeader和UserAccountsDrawerHeader用于创建头部,展示用户信息。DrawerHeader的child、padding和decoration属性用于定制头部效果,而UserAccountsDrawerHeader则提供了currentAccountPicture、accountName和accountEmail等字段展示用户详情。通过onDetailsPressed回调,可以处理更多用户信息的显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flutter Drawer 侧滑菜单、抽屉

Drawer 与Scaffold一起使用。是Scaffold中drawer属性。drawer通常是一个ListView,它的第一个子视图是一个DrawerHeader,但我们通常使用UserAccountsDrawerHeader显示当前用户的状态信息。其余的drawer子元素通常由ListTile构造,通常以AboutListTile结束。

可以使用Navigator.pop(context)主动关闭Drawer。

参数讲解

属性说明
Drawer
elevation背景高度
child子组件
semanticLabel标签
UserAccountsDrawerHeader
decoration头部装饰
margin外边距 默认8.0
currentAccountPicture主图像
otherAccountsPictures附图像
accountName标题
accountEmail副标题
onDetailsPressed点击监听

3、Drawer可以添加头部效果

  • DrawerHeader:展示头部基本信息
  • UserAccountsDrawerHeader:展示用户头像、用户名、email等信息

4、DrawerHeader常用属性

  • child:Widget类型,Header里面所显示的内容控件
  • padding、margin
  • decoration:Decoration类型,header区域的decoration,通常用来设置背景颜色或者背景图片。

5、UserAccountsDrawerHeader常用属性

  • currentAccountPicture:Widget类型,用来设置当前用户的头像
  • accountName:Widget类型,当前用户的名字
  • accountEmail:Widget类型,当前用户的 Email
  • onDetailsPressed: VoidCallback类型,当 accountName 或者 accountEmail 被点击的时候所触发的回调函数,可以用来显示其他额外的信息
  • otherAccountsPictures:List类型,用来设置当前用户的其他账号的头像
  • decoration:Decoration类型,header区域的decoration,通常用来设置背景颜色或者背景图片。
  • margin

代码示例

简单创建侧滑菜单

return Scaffold(
  appBar: AppBar(title: Text('Flutter Drawer'),),
  drawer: Drawer(
      child:Container(
        alignment: Alignment.center,
        child: Text('我是Drawer',style: TextStyle(fontSize: 30),),
      ),
    ),
  body: Container(
    alignment: Alignment.center,
    child: Text('data')
  ),
);

进阶

drawer: Drawer(
  child: ListView(
    children: <Widget>[
      UserAccountsDrawerHeader(
        accountEmail: Text('wo shi Email'), 
        accountName: Text('我是Drawer'),
        onDetailsPressed: () {},
        currentAccountPicture: CircleAvatar(
          backgroundImage: AssetImage('images/ab.jpg'),
        ),
      ),
      ListTile(
        title: Text('ListTile1'),
        subtitle: Text('ListSubtitle1',maxLines: 2,overflow: TextOverflow.ellipsis,),
        leading: CircleAvatar(child: Text("1")),
        onTap: (){Navigator.pop(context);},
      ),
      Divider(),//分割线
      ListTile(
        title: Text('ListTile2'),
        subtitle: Text('ListSubtitle2',maxLines: 2,overflow: TextOverflow.ellipsis,),
        leading: CircleAvatar(child: Text("2")),
        onTap: (){Navigator.pop(context);},
      ),
      Divider(),//分割线
      ListTile(
        title: Text('ListTile3'),
        subtitle: Text('ListSubtitle3',maxLines: 2,overflow: TextOverflow.ellipsis,),
        leading: CircleAvatar(child: Text("3")),
        onTap: (){Navigator.pop(context);},
      ),
      Divider(),//分割线
      new AboutListTile(
        icon: new CircleAvatar(child: new Text("4")),
        child: new Text("AboutListTile"),
        applicationName: "AppName",
        applicationVersion: "1.0.1",
        applicationIcon: new Image.asset(
          'images/bb.jpg',
          width: 55.0,
          height: 55.0,
        ),
        applicationLegalese: "applicationLegalese",
        aboutBoxChildren: <Widget>[
          new Text("第一条..."),
          new Text("第二条...")
        ],
      ),
      Divider(),//分割线
    ],
  ),
),

image-20200829222454871

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值