大佬教程收集整理的这篇文章主要介绍了【Flutter 实战】菜单(Menu)功能,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
使用PopupMenuButton,点击时弹出菜单,用法如下:
PopupMenuButton<String>(
itemBuilder: (context) {
return <PopupMenuEntry<String>>[
PopupMenuItem<String>(
value: '语文',child: Text('语文'),),PopupMenuItem<String>(
value: '数学',child: Text('数学'),PopupMenuItem<String>(
value: '英语',child: Text('英语'),PopupMenuItem<String>(
value: '生物',child: Text('生物'),PopupMenuItem<String>(
value: '化学',child: Text('化学'),];
},)
效果如下:
设置其初始值:
PopupMenuButton<String>(
initialValue: '语文',...
)
PopupMenuButton<String>(
onSELEcted: (value){
print('$value');
},onCanceled: (){
print('onCanceled');
},...
)
PopupMenuButton<String>(
tooltip: 'PopupMenuButton',...
)
效果如下:
设置其阴影值、内边距和弹出菜单的背景颜色:
PopupMenuButton<String>(
elevation: 5,padding: EdgeInsets.all(5),color: Colors.red,...
)
默认情况下,PopupMenuButton显示3个小圆点,我们也可以对齐进行设置,设置文字如下:
PopupMenuButton<String>(
child: Text('学科'),...
)
child
组件将会被InkWell包裹,点击弹出菜单,效果如下:
也可以设置其他图标:
PopupMenuButton<String>(
icon: Icon(Icons.add),...
)
效果如下:
设置弹出菜单边框:
PopupMenuButton<String>(
shape: RoundedRectangleBorder(
side: Borderside(
color: Colors.red
),borderRadius: BorderRadius.circular(10)
),...
)
效果如下:
@H_939_2@menu有一个非常重要的参数Offset
,这个参数是控制菜单弹出的位置,通常情况下,菜单在当前按钮下面展示:
PopupMenuButton<String>(
offset: Offset(0,100),itemBuilder: (context) {
return <PopupMenuEntry<String>>[
PopupMenuItem<String>(
value: '语文',)
PopupMenuButton
的每一项都需要是PopupMenuEntry
类型,PopupMenuEntry
为抽象类,其子类有PopupMenuItem、PopupMenuDivider、checkedPopupMenuItem。
构造函数为
参数说明:
onSELEcted
返回。用法如下:
PopupMenuButton<String>(
onSELEcted: (value) {
print('$value');
},enabled: false,textStyle: TextStyle(color: Colors.red),height: 100,)
PopupMenuButton<String>(
onSELEcted: (value) {
print('$value');
},PopupMenuDivider(),)
PopupMenuDivider默认高度为16,注意这个高度并不是分割线的高度,而是分割线控件的高度,设置为50代码:
PopupMenuDivider(height: 50,
checkedPopupMenuItem是前面带是否选中的控件,本质就是一个ListTile,用法如下:
PopupMenuButton<String>(
onSELEcted: (value) {
print('$value');
},itemBuilder: (context) {
return <PopupMenuEntry<String>>[
checkedPopupMenuItem(
value: '语文',checked: true,checkedPopupMenuItem(
value: '数学',)
如果你看下PopupMenuButton
的源码会发现,PopupMenuButton
也是使用showMenu实现的,用法如下:
showMenu(
context: context,position: RelativeRect.fill,items: <PopupMenuEntry>[
PopupMenuItem(child: Text('语文')),checkedPopupMenuItem(
child: Text('数学'),PopupMenuItem(child: Text('英语')),]);
position
参数表示弹出的位置,效果如下:
属性和PopupMenuButton
基本一样,但使用showMenu
需要我们指定位置,所以一般情况下,我们不会直接使用showMenu
,而是使用PopupMenuButton
,免去了计算位置的过程。
看下PopupMenuButton
是如何计算的,有助于帮助我们理解:
final PopupMenuThemeData popupMenuTheme = PopupMenuTheme.of(context);
final RenderBox button = context.findRenderObject();
final RenderBox overlay = Overlay.of(context).context.findRenderObject();
final RelativeRect position = RelativeRect.fromRect(
Rect.fromPoints(
button.localToGlobal(widget.offset,ancestor: overlay),button.localToGlobal(button.size.bottomRight(Offset.zero),Offset.zero & overlay.size,);
final List<PopupMenuEntry<T>> items = widget.itemBuilder(context);
老孟Flutter博客地址(330个控件用法):http://laomengit.com
欢迎加入Flutter交流群(微信:laomENGIt)、关注公众号【老孟Flutter】:
以上是大佬教程为你收集整理的【Flutter 实战】菜单(Menu)功能全部内容,希望文章能够帮你解决【Flutter 实战】菜单(Menu)功能所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。