我正在尝试创建一个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'),
],
)
我尝试过的
我已尝试实现PrefferedSizeWidget
,但无法在PopupMenuButton
上工作。
答案 0 :(得分:0)
编辑:我的意思是固定高度:S
PopUpMenuButton不支持固定高度。但是您可以做的是调整PopUpMenu包。 做了类似的事情 here 使用DropdownButton。对于PopUpMenu,实现应该类似地工作,因为两者具有相同的strucktur。 (Route,RouteLayout和PopUpMenu)
您查看original code of the DropdownButton,然后查看该人在the custom edition中对其所做的更改。
然后,您将code of the PopUpMenuButton复制到自己的项目中,并像使用DropDownButton一样进行调整。
然后,使用自定义版本的PopUpMenuButton以及参数height。
由于您在执行我的意思时遇到了一些问题,因此我为您做到了: 只需将此file复制到您的目录中并将其导入您的代码即可。 然后使用具有高度的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'),
),
],
)),
);
}
}
如果有任何问题无法解决,也许我稍后再调查。