Flutter PopupMenuEntry


简介

Material 风格中 弹出菜单的一个基类

  • 如果需要创建一个显示弹出菜单的按钮,请考虑使用 PopupMenuButton.

基本用法

  • PopupMenuDivider 是一条水平分割线,注意数组要使用父类 PopupMenuEntry,配合其他 item 样式共同使用

实例演示

import 'package:flutter/material.dart';

class PopupMenuDividerDemo extends StatefulWidget {
  _PopupMenuDividerDemoState createState() => _PopupMenuDividerDemoState();
}

class _PopupMenuDividerDemoState extends State<PopupMenuDividerDemo> {
  void showInSnackBar(String value) {
    Scaffold.of(context)
        .showSnackBar( SnackBar(content:  Text('You selected: $value')));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListTile(
          title: const Text('An item with a sectioned menu'),
          trailing: PopupMenuButton<String>(
              padding: EdgeInsets.zero,
              onSelected: showInSnackBar,
              itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
                    const PopupMenuItem<String>(
                        value: 'Preview',
                        child: ListTile(
                            leading: Icon(Icons.visibility),
                            title: Text('Preview'))),
                    const PopupMenuItem<String>(
                        value: 'Share',
                        child: ListTile(
                            leading: Icon(Icons.person_add),
                            title: Text('Share'))),
                    const PopupMenuItem<String>(
                        value: 'Get Link',
                        child: ListTile(
                            leading: Icon(Icons.link),
                            title: Text('Get link'))),
                    const PopupMenuDivider(),
                    const PopupMenuItem<String>(
                        value: 'Remove',
                        child: ListTile(
                            leading: Icon(Icons.delete), title: Text('Remove')))
                  ])),
    );
  }
}