繁体   English   中英

如何在Flutter中给PopUpMenuButton一个高度?

[英]How to give a height to the PopUpMenuButton in 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'),
  ],
)

我已经尝试实现PreferredSizeWidget但无法在PopupMenuButton

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

PopUpMenuButton不支持固定高度。 但是您可以做的是调整PopUpMenu包。 类似的事情做在这里与DropdownButton。 对于PopUpMenu,实现应该类似地工作,因为两者具有相同的strucktur。 (Route,RouteLayout和PopUpMenu)

编辑:

您查看一下DropdownButton原始代码,然后查看该用户在定制版中对其所做的更改。

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

然后,使用带有参数height的ure自定义版本的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'),
          ),
        ],
      )),
    );
  }
}

如果有任何问题无法解决,请稍后再讨论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM