PopupMenuEntry 的水平对齐方式

Horizontal Alignment of PopupMenuEntry

有没有办法让 PopupMenu 的项目水平对齐,而不仅仅是垂直对齐?

我想向小部件添加更多行为并将其作为 PopupMenu 的子项传递提供除渲染之外的所有要求。

Flutter的popup menu内部常量很多,要求弹窗的轴是垂直的。如果您想更改轴并完全控制布局和大小,您可以复制该文件并开始编辑它。

如果您对布局不太挑剔,可以通过将 Row 小部件嵌入为单个弹出菜单项来伪造它。下面是一些演示该方法的代码:

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new MyHomePage(),
    );
  }
}

/// An arbitrary widget that lives in a popup menu
class PopupMenuWidget<T> extends PopupMenuEntry<T> {
  const PopupMenuWidget({ Key key, this.height, this.child }) : super(key: key);

  @override
  final Widget child;

  @override
  final double height;

  @override
  bool get enabled => false;

  @override
  _PopupMenuWidgetState createState() => new _PopupMenuWidgetState();
}

class _PopupMenuWidgetState extends State<PopupMenuWidget> {
  @override
  Widget build(BuildContext context) => widget.child;
}


class MyHomePage extends StatelessWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        actions: <Widget>[
          new PopupMenuButton<String>(
            onSelected: (String value) {
              print("You selected $value");
            },
            itemBuilder: (BuildContext context) {
              return [
                new PopupMenuWidget(
                  height: 40.0,
                  child: new Row(
                    children: [
                      new IconButton(
                        icon: new Icon(Icons.add),
                        onPressed: () => Navigator.pop(context, 'add')),
                      new IconButton(
                        icon: new Icon(Icons.remove),
                        onPressed: () => Navigator.pop(context, 'remove')),
                    ],
                  ),
                ),
              ];
            }
          ),
        ],
      ),
    );
  }
}
import 'package:flutter/material.dart';

/// An arbitrary widget that lives in a popup menu
class PopupMenuWidget<T> extends PopupMenuEntry<T> {
  const PopupMenuWidget({
    Key? key,
    required this.height,
    required this.child,
  }) : super(key: key);
  final Widget child;

  @override
  final double height;

  @override
  _PopupMenuWidgetState createState() => _PopupMenuWidgetState();

  @override
  bool represents(T? value) => false;
}

class _PopupMenuWidgetState extends State<PopupMenuWidget> {
  @override
  Widget build(BuildContext context) => widget.child;
}


在零安全应用程序中使用它