gpt4 book ai didi

dart - PopupMenuEntry 的水平对齐方式

转载 作者:IT王子 更新时间:2023-10-29 06:52:12 26 4
gpt4 key购买 nike

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

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

最佳答案

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

关于dart - PopupMenuEntry 的水平对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43859331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com