如何为 Flutter 中的 PopUpMenuButton 设置高度?

med*_*tat 6 popupmenu flutter

我正在尝试创建一个PopupMenuButton. 我已经使用了PopupMenuButton class.

PopupMenuButton(
  padding: EdgeInsets.only(right: 8.0),
  offset: Offset(-16, 0),
  child: Container(
    decoration: BoxDecoration(
        color: Colors.orange,
        borderRadius: BorderRadius.all(
          Radius.circular(16.0),
        )),
    padding: EdgeInsets.symmetric(vertical: 8.0, horizontal: 12.0),
    child: Text(
      "Category",
      style: TextStyle(color: Colors.white),
    ),
  ),
  itemBuilder: (_) => <PopupMenuItem<String>>[
    new PopupMenuItem<String>(
        //I want this context to be scrollable with some fixed height on the screen
        child: Row(
          children: <Widget>[
            Icon(Icons.arrow_right),
            Text("Dairy & Bakery")
          ],
        ),
        value: '1'),
  ],
)
Run Code Online (Sandbox Code Playgroud)

我曾尝试实施,PreferredSizeWidget但没有在PopupMenuButton.

Mar*_*ein 5

编辑:我的意思是固定高度:S

PopUpMenuButton 不支持固定高度。但是你可以做的是调整 PopUpMenu 包。此处 使用 DropdownButton完成类似的操作 。对于 PopUpMenu,实现应该类似地工作,因为两者具有相同的冲击。(Route, RouteLayout 和 PopUpMenu)

编辑:

您查看DropdownButton原始代码,然后查看此人在自定义版本中对其所做的更改。

然后,您将 PopUpMenuButton代码复制到您自己的项目中,然后像使用 DropDownButton 一样调整它们。

然后你使用自定义版本的 PopUpMenuButton 和参数高度。

编辑2:

由于您在执行我的意思时遇到了一些问题,我为您做了:只需将此文件复制到您的目录中并将其导入到您的代码中。然后使用带有高度的 CustomPopupMenuButton 而不是原始高度。

用法:

import 'package:flutter/material.dart';

import 'custom_popup_menu_button.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Home(),
    );
  }
}
class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

enum WhyFarther { harder, smarter, selfStarter, tradingCharter }

class _HomeState extends State<Home> {
  WhyFarther _selection;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'it does work here',
          style: TextStyle(fontSize: 20),
        ),
      ),
      body: Center(
          child: CustomPopupMenuButton<WhyFarther>(
        onSelected: (WhyFarther result) {
          setState(() {
            _selection = result;
          });
        },
        height: 100,
        itemBuilder: (BuildContext context) => <PopupMenuEntry<WhyFarther>>[
          const PopupMenuItem<WhyFarther>(
            value: WhyFarther.harder,
            child: Text('Working a lot harder'),
          ),
          const PopupMenuItem<WhyFarther>(
            value: WhyFarther.smarter,
            child: Text('Being a lot smarter'),
          ),
          const PopupMenuItem<WhyFarther>(
            value: WhyFarther.selfStarter,
            child: Text('Being a self-starter'),
          ),
          const PopupMenuItem<WhyFarther>(
            value: WhyFarther.tradingCharter,
            child: Text('Placed in charge of trading charter'),
          ),
        ],
      )),
    );
  }
}

Run Code Online (Sandbox Code Playgroud)

如果有任何不工作,请随时询问,也许我稍后会研究它。