dropdownbutton inside popupmenubutton works but wont show updated selected value (flutter)

212 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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.