如何在Flutter中更改PopupMenuItem宽度?

but5z9lq  于 2022-11-30  发布在  Flutter
关注(0)|答案(3)|浏览(201)

我的代码是这样的

我希望使项目宽度与父项匹配,但无法更改PopupMenuItem宽度
我尝试文本换行继续和设置宽度准确,但它不工作。
我该怎么办呢?

8ulbf1ek

8ulbf1ek1#

容器内有弹出菜单项

new PopupMenuButton<Choice>(
            onSelected: _select,
            itemBuilder: (BuildContext context) {
              return choices.map((Choice choice) {
                return PopupMenuItem<Choice>(
                  value: choice,
                  child: new Container(
                    width: 100.0,
                    child: Text(choice.title)
                  ),
                );
              }).toList();
            },
          ),

完整示例的其他代码

void _select(Choice choice) {
      print(choice);
  }

class Choice {
  const Choice({this.title, this.icon});

  final String title;
  final IconData icon;
}

const List<Choice> choices = const <Choice>[
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.directions_bike),
  const Choice(title: 'Boat', icon: Icons.directions_boat),
  const Choice(title: 'Bus', icon: Icons.directions_bus),
  const Choice(title: 'Train', icon: Icons.directions_railway),
  const Choice(title: 'Walk', icon: Icons.directions_walk),
];
t30tvxxf

t30tvxxf2#

更改约束中maxWidth的值
样本代码

Widget _offsetPopup() => PopupMenuButton<int>(
constraints: BoxConstraints(
    minWidth: 2.0 * 56.0,
    maxWidth: MediaQuery.of(context).size.width,
  ),
  itemBuilder: (context) => [
        PopupMenuItem(
          value: 1,
          child: Container(
            width: MediaQuery.of(context).size.width,
            child: Expanded(
              child: Text(
                "To Receive",
                style: TextStyle(
                    fontSize: 16,
                    color: Colors.black,
                    fontWeight: FontWeight.w700),
              ),
            ),
          ),
        ),
      ],
  icon: Icon(Icons.add),
 

  offset: Offset(0, 40));
jexiocij

jexiocij3#

PopupMenuButton提供了设置框约束的选项。您可以使用相同的选项。
https://api.flutter.dev/flutter/material/PopupMenuButton/constraints.html

相关问题