弹出菜单按钮内的下拉按钮可以工作,但不会显示更新的选定值(颤动)
将 DropdownButton 放置在 PopupMenuButton 内部时,下拉菜单仍然有效,但新选择的值不会反映在 DropdownButton 的标头中。
重新创建的方法:
flutter create -t skeleton test_app
测试:通过单击设置齿轮并更改主题,您会看到应用程序的背景已更新,所选主题的名称也已更新。
现在,在 test_app 中找到生成的设置视图,并将其 DropdownButton 放置在 PopupMenuButton 中,如下所示:
class SettingsView extends StatelessWidget {
const SettingsView({Key? key, required this.controller}) : super(key: key);
static const routeName = '/settings';
final SettingsController controller;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Settings'),
),
body: Padding(
padding: const EdgeInsets.all(16),
child: PopupMenuButton(
itemBuilder: (context) => [
PopupMenuItem(
child: DropdownButton<ThemeMode>(
value: controller.themeMode,
onChanged: controller.updateThemeMode,
items: const [
DropdownMenuItem(
value: ThemeMode.system,
child: Text('System Theme'),
),
DropdownMenuItem(
value: ThemeMode.light,
child: Text('Light Theme'),
),
DropdownMenuItem(
value: ThemeMode.dark,
child: Text('Dark Theme'),
)
],
),
),
],
),
),
);
}
}
现在更改主题时,您会看到应用程序的背景已更新,但在 DropdownButton 中所选主题的名称没有相应更改。
为什么会发生这种情况?
When placing a DropdownButton inside of a PopupMenuButton the dropdown still works but the newly selected value isn't reflected in the DropdownButton's header.
Way to recreate:
flutter create -t skeleton test_app
Test: By clicking on the settings gear and changing the theme, you see that the background of the app is updated and the name of the selected theme is also updated.
Now, inside the test_app find the generated settings view and place its DropdownButton inside of a PopupMenuButton as below:
class SettingsView extends StatelessWidget {
const SettingsView({Key? key, required this.controller}) : super(key: key);
static const routeName = '/settings';
final SettingsController controller;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Settings'),
),
body: Padding(
padding: const EdgeInsets.all(16),
child: PopupMenuButton(
itemBuilder: (context) => [
PopupMenuItem(
child: DropdownButton<ThemeMode>(
value: controller.themeMode,
onChanged: controller.updateThemeMode,
items: const [
DropdownMenuItem(
value: ThemeMode.system,
child: Text('System Theme'),
),
DropdownMenuItem(
value: ThemeMode.light,
child: Text('Light Theme'),
),
DropdownMenuItem(
value: ThemeMode.dark,
child: Text('Dark Theme'),
)
],
),
),
],
),
),
);
}
}
When changing the theme now you see that the background of the app is updated but in the DropdownButton the name of the selected theme did not change accordingly.
Why is this happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终剥离了 settings_controller 并将其替换为常规更改通知程序。然后我注意到弹出窗口按预期刷新。我怀疑骨架模板的 settings_controller 的实现存在一些问题,导致出现问题。
I wound up stripping out the settings_controller and replacing it with a regular change notifier. Then I noticed the popup was refreshing as expected. I suspect there is some issue with the implementation of the skeleton template's settings_controller that causes the hiccup.