我正在尝试创建一个已经使用过的PopupMenuButton.I的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上工作。
发布于 2019-07-23 21:03:13
编辑:我的意思是固定高度:S
PopUpMenuButton不支持固定高度。但是你能做的就是调整PopUpMenu包。类似的事情是用DropdownButton在here上完成的。对于PopUpMenu,实现应该类似地工作,因为两者具有相同的结构。(路由、RouteLayout和PopUpMenu)
编辑:
您先看一下original code of the DropdownButton,然后再看一下这个人在the custom edition中对它所做的更改。
然后,您可以将code of the PopUpMenuButton复制到您自己的项目中,并像使用DropDownButton一样对它们进行调整。
然后,使用带有参数height的PopUpMenuButton的ure自定义版本。
编辑2:
由于您在执行我所说的操作时遇到了一些问题,我为您做了以下工作:只需将此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'),
),
],
)),
);
}
}如果有什么不起作用,请随时询问,也许我稍后会调查一下。
https://stackoverflow.com/questions/57162290
复制相似问题