[英]Flutter PopupMenuButton - how to disable it conditionally
I have a simple PopupMenuButton
implementation on appTitle
. 我在
appTitle
上有一个简单的PopupMenuButton
实现。 Here it is: 这里是:
PopupMenuItem<int> _buildMenuItem(int size) {
return PopupMenuItem<int>(
value: size,
child: Text('${size}x$size'),
);
}
return Scaffold(
appBar: AppBar(
title: Text('Some title'),
actions: <Widget>[
PopupMenuButton<int>(
icon: Icon(Icons.refresh),
onSelected: _canReplay()
? (int size) {
// do something with the size
}
: null,
itemBuilder: (BuildContext context) => <PopupMenuItem<int>>[
_buildMenuItem(3),
_buildMenuItem(4),
_buildMenuItem(5),
],
),
],
),
body: ...
);
UPDATED - I have realized my initial question might not be quite clear, so I rephrased it below: 更新 - 我已经意识到我的初步问题可能不太清楚,所以我在下面重述:
What I would like to achieve is to disable PopupMenuButton
based on app condition. 我想要实现的是根据app条件禁用
PopupMenuButton
。 Using the above code, I can enable / disable onSelected
method invocation, however, icon appearance does not change. 使用上面的代码,我可以启用/禁用
onSelected
方法调用,但是,图标外观不会改变。
To summarize - I would like the entire PopupMenuButton
along with its icon to become disabled in the same fashion IconButton
is disabled with you pass null
to its onPressed
method. 总结一下 - 我希望整个
PopupMenuButton
及其图标以相同的方式被禁用IconButton
被禁用,你将null
传递给它的onPressed
方法。 That way, the user won't be able to even press the button and to invoke the menu. 这样,用户甚至无法按下按钮并调用菜单。
Is that achievable? 这可以实现吗?
You can use the enabled
property of the PopupMenuItem
widget. 您可以使用
PopupMenuItem
小部件的enabled
属性。
PopupMenuItem<int> _buildMenuItem(int size, bool enabled) {
return PopupMenuItem<int>(
value: size,
child: Text('${size}x$size'),
enabled: enabled,
);
}
itemBuilder: (BuildContext context) => <PopupMenuItem<int>>[
_buildMenuItem(3, true),
_buildMenuItem(4, false),
_buildMenuItem(5, true),
],
More info: https://docs.flutter.io/flutter/material/PopupMenuItem/enabled.html 更多信息: https : //docs.flutter.io/flutter/material/PopupMenuItem/enabled.html
UPDATE UPDATE
Based on the new information you give us about your question, it's not possible to disable the button using the PopupMenuButton
button, but this is Flutter, so you can create your own Widget. 根据您提供给我们的关于您的问题的新信息,不可能使用
PopupMenuButton
按钮禁用该按钮,但这是Flutter,因此您可以创建自己的Widget。
I created a custom PopupMenuButton
where you can use the enabled
property, take a look : 我创建了一个自定义
PopupMenuButton
,您可以在其中使用enabled
属性,看看:
https://gist.github.com/diegoveloper/a388dd42a01ffff04cd51ec026381fe3 https://gist.github.com/diegoveloper/a388dd42a01ffff04cd51ec026381fe3
Usage: 用法:
MyOwnPopupMenuButton<int>(
enabled: _canReplay(),
icon: Icon(
Icons.refresh,
color: _canReplay() ? Colors.black : Colors.black.withOpacity(0.4),
),
onSelected: _canReplay()
? (int size) {
// do something with the size
}
: null,
itemBuilder: (BuildContext context) => <PopupMenuItem<int>>[
_buildMenuItem(3),
_buildMenuItem(4),
_buildMenuItem(5),
],
)
After few hours, I ended up with a following solution, which fits my requirements and it is implemented w/o any complex custom widgets. 几个小时后,我最终得到了一个符合我要求的以下解决方案,并且无需任何复杂的自定义小部件即可实现。 Here it is:
这里是:
return Scaffold(
appBar: AppBar(
title: Text(allTranslations.text('Some title')),
actions: <Widget>[
_canReplay()
? PopupMenuButton<int>(
icon: Icon(Icons.refresh),
onSelected: (int size) {
// do something with size
},
itemBuilder: (BuildContext context) => <PopupMenuItem<int>>[
_buildMenuItem(3),
_buildMenuItem(4),
_buildMenuItem(5),
],
)
: IconButton(
icon: Icon(Icons.refresh),
onPressed: null,
),
],
),
body: ...
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.