PopupMenuEntry的水平对齐方式

时间:2017-05-09 00:47:02

标签: dart flutter

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

我希望向窗口小部件添加更多行为并将其传递,因为PopupMenu的子窗口提供除渲染之外的所有要求。

1 个答案:

答案 0 :(得分:3)

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

如果您对布局不太挑剔,可以通过将Row窗口小部件作为单个弹出菜单项嵌入来伪造它。这里有一些代码证明了这种方法:

screenshot

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')),
                    ],
                  ),
                ),
              ];
            }
          ),
        ],
      ),
    );
  }
}