【Flutter 专题】22 图解 PopupMenu 那些事儿

举报
阿策小和尚 发表于 2022/02/02 11:25:00 2022/02/02
【摘要】 0 基础学习 Flutter,第二十二步:简单了解 PopUpMenu 菜单栏!

      小菜需要处理标题栏弹出对话框 PopupMenu 样式,Flutter 当然提供了一些处理方式,类似 PopupMenuEntry 等,小菜仅就最基础的使用方式进行初步的学习和整理。

PopupMenuItem 基本样式

      PopupMenuItem 为单个 item 的弹出样式,默认为 48px 高,可根据需求自行定义。item 中可以自定义需要的样式,包括文字图片等一系列样式。

@override
Widget build(BuildContext context) {
  return new Scaffold(
      appBar: AppBar(
        title: Text('PopMenuDemo'),
        actions: <Widget>[_NomalPopMenu()],
      ),
      body: Center(child: new Text(_bodyStr)));
}

Widget _NomalPopMenu() {
  return new PopupMenuButton<String>(
      itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
            new PopupMenuItem<String>(
                value: 'value01', child: new Text('Item One')),
            new PopupMenuItem<String>(
                value: 'value02', child: new Text('Item Two')),
            new PopupMenuItem<String>(
                value: 'value03', child: new Text('Item Three')),
            new PopupMenuItem<String>(
                value: 'value04', child: new Text('I am Item Four'))
          ],
      onSelected: (String value) {
        setState(() { _bodyStr = value; });
      });
}

      Tips: 若需要处理带图标的样式时,官网提供的 Demo 是借助的 ListTile 来处理的,但是小菜测试发现图标与文字距离偏大,原因在于 ListTile 默认左侧图标 leading 距离不可直接调整,建议用 Row 或其他方式调整。

// ListTile 样式
new PopupMenuItem<String>(
    value: 'value01',
    child: ListTile( leading: Icon(Icons.looks_one), title: Text('Item One'))),

// 普通自定义样式
new PopupMenuItem<String>(
    value: 'value01',
    child: Row(children: <Widget>[
      Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0.0),
          child: Icon(Icons.looks_one)),
      Text('Item One')
    ])),

CheckedPopupMenuItem 选中样式

      CheckedPopupMenuItem 是一个带有复选标记的弹出菜单项。默认高度同样是 48px,水平布局使用 ListTile 复选标记是 Icons.done 图标,显示在 leading 位置;同时只有在状态为选中时才会显示图标。

Widget _CheckPopMenu() {
  return new PopupMenuButton<String>(
      itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
            new CheckedPopupMenuItem<String>(
                checked: false, value: 'value01', child: new Text('Item One')),
            new CheckedPopupMenuItem<String>(
                checked: true, value: 'value02', child: new Text('Item Two')),
            new CheckedPopupMenuItem<String>(
                checked: false, value: 'value03', child: new Text('Item Three')),
            new CheckedPopupMenuItem<String>(
                checked: false, value: 'value04', child: new Text('I am Item Four'))
          ],
      onSelected: (String value) {
        setState(() { _bodyStr = value; });
      });
}

PopupMenuDivider 分割线

      PopupMenuDivider 是一条水平分割线,注意数组要使用父类 PopupMenuEntry,配合其他 item 样式共同使用。PopupMenuDivider 可以调整高度,但无法调整颜色,有需要的话可以进行自定义。

Widget _DividerPopMenu() {
  return new PopupMenuButton<String>(
      itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
            new PopupMenuItem<String>( value: 'value01', child: new Text('Item One')),
            new PopupMenuDivider(height: 1.0),
            new PopupMenuItem<String>( value: 'value02', child: new Text('Item Two')),
            new PopupMenuDivider(height: 1.0),
            new PopupMenuItem<String>( value: 'value03', child: new Text('Item Three')),
            new PopupMenuDivider(height: 1.0),
            new PopupMenuItem<String>( value: 'value04', child: new Text('I am Item Four'))
          ],
      onSelected: (String value) {
        setState(() { _bodyStr = value; });
      });
}

showMenu 指定位置

      PopupMenu 默认的弹框位置都是在右上角,且会挡住标题栏,如果有需要在其他位置弹框就需要借助 showMenu,主要通过 position 属性定位弹框位置。

      menu 的宽高与内容相关,小菜的理解是在水平和竖直方向上会将设置的 position 位置加上 menu 宽高,再与屏幕匹配,超过屏幕宽高,根据 position 按照 LTRB 顺序贴近屏幕边框展示。

onTap: () async {
  final result = await showMenu(
    context: context,
    position: RelativeRect.fromLTRB(100.0, 200.0, 100.0, 100.0),
//    position: RelativeRect.fromLTRB(1000.0, 1000.0, 0.0, 10.0),
    items: <PopupMenuItem<String>>[
      new PopupMenuItem<String>( value: 'value01', child: new Text('Item One')),
      new PopupMenuItem<String>( value: 'value02', child: new Text('Item Two')),
      new PopupMenuItem<String>( value: 'value03', child: new Text('Item Three')),
      new PopupMenuItem<String>( value: 'value04', child: new Text('I am Item Four'))
    ] );
},



      Tips: 如果 item 个数过多也无需担心,Flutter 支持默认超过屏幕滑动效果。


      小菜目前的学习还仅限于基本的使用,稍高级的自定义涉及较少,如果又不对的地方还希望多多指出。

来源:阿策小和尚

【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。