弹出菜单按钮内的下拉按钮可以工作,但不会显示更新的选定值(颤动)

发布于 2025-01-13 02:06:36 字数 1645 浏览 2 评论 0原文

将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那些过往 2025-01-20 02:06:36

我最终剥离了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文