Skip to content

[DropdownMenu] Improve document on how to reset selectedValue #177599

@UriDevzera

Description

@UriDevzera

Steps to reproduce

1 - Create a simple statefull home page;
2 - Add a DropdownMenu with some options;
3 - Inform initialSelection with a selectedValue;
4 - Create one button with a pressed function that should change the value of selectedValue for null;
5 - Select one item;
6 - Press the button;

OBS: I think that I know why it happens. I checked the DropdownMenu flutter implementation and I saw that in didUpdateWidget function do not implement something when widget.initialSelection is null, of course there is a check to see if oldWidget.initialSelection != widget.initialSelection but it is just trying to find the current option in the list and effective change the value if there is. Of course I don't know if there is a reason for it, I'm just wondering that maybe this function should check if initialSelection is null to clear the controller text;

Expected results

The expected results is clean the dropdown selection UI;

Actual results

Don't clean selection.

Code sample

Code sample
import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String? selectedValue1;
  String? selectedValue2;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('Welcome to Flutter!'),
            const SizedBox(height: 20),
            DropdownMenu<String>(
              width: 200,
              label: const Text('First Dropdown'),
              onSelected: (String? value) {
                setState(() {
                  selectedValue1 = value;
                });
              },
              initialSelection: selectedValue1,
              dropdownMenuEntries: const <DropdownMenuEntry<String>>[
                DropdownMenuEntry<String>(value: 'option1', label: 'Option 1'),
                DropdownMenuEntry<String>(value: 'option2', label: 'Option 2'),
                DropdownMenuEntry<String>(value: 'option3', label: 'Option 3'),
              ],
            ),
            const SizedBox(height: 20),
            DropdownMenu<String>(
              width: 200,
              label: const Text('Second Dropdown'),
              initialSelection: selectedValue2,
              onSelected: (String? value) {
                setState(() {
                  selectedValue2 = value;
                });
              },
              dropdownMenuEntries: const <DropdownMenuEntry<String>>[
                DropdownMenuEntry<String>(value: 'choice1', label: 'Choice 1'),
                DropdownMenuEntry<String>(value: 'choice2', label: 'Choice 2'),
                DropdownMenuEntry<String>(value: 'choice3', label: 'Choice 3'),
              ],
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                setState(() {
                  selectedValue2 = null;
                });
              },
              child: const Text('Clear Second Dropdown'),
            ),
          ],
        ),
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.35.7, on Microsoft Windows [Version 10.0.22631.6060], locale en-US)
[√] Windows Version (11 Enterprise 64-bit, 23H2, 2009)
[√] Android toolchain - develop for Android devices (Android SDK version 36.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Professional 2022 17.13.3)
[√] Android Studio (version 2024.1)
[√] VS Code (version 1.105.1)
[√] Connected device (5 available)
[√] Network resources

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: proposalA detailed proposal for a change to Flutterd: api docsIssues with https://api.flutter.dev/f: material designflutter/packages/flutter/material repository.frameworkflutter/packages/flutter repository. See also f: labels.team-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions