Repository: flutter
Issue: DropdownButtonFormField with option to add custom menu item not working
Labels: framework,f: material design,has reproducible steps,P2,team-design,triaged-design,found in release: 3.13,found in release: 3.14

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

1. Open the dropdown menu, you will see two options ["preset", "Choose something custom"]
2. Click "Choose something custom"
3. You will see that the selected drop down item is "My custom value"
4. Open the dropdown menu again and there will be three options ["preset", "My custom value", "Choose something custom"]
5. Click "Choose something custom" again.
6. The selected option says "Choose something custom"

### Expected results

The goal is to have a `DropdownButtonFormField `that has some preset options, but also the option for the user to choose a custom option.

To achieve this I have two drop down menu item values alongside the preset options, `DropdownOptions.setCustom` and `DropdownOptions.custom`.

One is for selecting the custom option, and the other is conditionally shown when there is a custom option selected.

When the user clicks the dropdown option of `Choose something custom` (value of `DropdownOptions.setCustom`), in the `onChanged` callback the custom value is set (in this case as `My custom value`) and the dropdown value is then set as `DropdownOptions.custom`.

I expect the user to then see the selected option is `My custom value`, and they can open the dropdown menu again and repeat this step if they wish.

### Actual results

The user is able to click `Choose something custom`, and when they do so, the selected option becomes `My custom value` as expected.

If the user then opens the dropdown again and clicks `Choose something custom` again, this time, the selected option says `Choose something custom` instead of showing `My custom value`.

If the code is updated to use `DropdownButton` instead of `DropdownButtonFormField` then the code works as expected. 

### Code sample

<details><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: AppBar(), body: MySelector());
  }
}

enum DropdownOptions { preset, custom, setCustom }

class MySelector extends StatefulWidget {
  @override
  State<MySelector> createState() => _MySelectorState();
}

class _MySelectorState extends State<MySelector> {
  String? customValue;
  DropdownOptions selectedOption = DropdownOptions.preset;

  @override
  Widget build(BuildContext context) {
    List<DropdownMenuItem> items = [
      const DropdownMenuItem(
        value: DropdownOptions.preset,
        child: Text("preset"),
      ),
    ];

    if (customValue != null) {
      items.add(DropdownMenuItem(
        value: DropdownOptions.custom,
        child: Text(customValue as String),
      ));
    }

    items.add(const DropdownMenuItem(
      value: DropdownOptions.setCustom,
      child: Text("Choose something custom"),
    ));

    return DropdownButtonFormField(
      value: selectedOption,
      onChanged: (value) {
        if (value == DropdownOptions.setCustom) {
          setState(() {
            // This value could come from a dialog with a text field
            customValue = "My custom value";

            // This should be rebuild and show the customValue option as selected
            // It works the first time, but for the subsequent times
            // "Choose something custom" is shown instead
            selectedOption = DropdownOptions.custom;
          });
        } else {
          setState(() => selectedOption = value);
        }
      },
      items: items,
    );
  }
}

```

</details>



</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
[!] Flutter (Channel stable, 3.13.2, on macOS 13.5 22G74 darwin-arm64, locale en-GB)
    • Flutter version 3.13.2 on channel stable at /Users/tomassheers/Documents/Dev/flutter
    ! The flutter binary is not on your path. Consider adding /Users/tomassheers/Documents/Dev/flutter/bin to your path.
    ! The dart binary is not on your path. Consider adding /Users/tomassheers/Documents/Dev/flutter/bin to your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ff5b5b5fa6 (3 weeks ago), 2023-08-24 08:12:28 -0500
    • Engine revision b20183e040
    • Dart version 3.1.0
    • DevTools version 2.25.0
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/tomassheers/Library/Android/sdk
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.82.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.72.0

[✓] Connected device (3 available)
    • sdk gphone arm64 (mobile) • emulator-5554 • android-arm64  • Android 11 (API 30) (emulator)
    • macOS (desktop)           • macos         • darwin-arm64   • macOS 13.5 22G74 darwin-arm64
    • Chrome (web)              • chrome        • web-javascript • Google Chrome 116.0.5845.187

[✓] Network resources
    • All expected network resources are available.

```

</details>