Skip to content

DropdownMenu does not highlight correct selected item #147516

@albe-jj

Description

@albe-jj

Steps to reproduce

With the following code, when item with label test is selected the item with label my test is selected instead, Although the object has different uuid. if I select bla or test 1 then it works as expected.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Custom Dropdown Menu',
      home: DropdownMenuExample(),
    );
  }
}

class DropdownMenuExample extends StatefulWidget {
  @override
  _DropdownMenuExampleState createState() => _DropdownMenuExampleState();
}

class _DropdownMenuExampleState extends State<DropdownMenuExample> {
  MyObject? selectedValue;

  @override
  Widget build(BuildContext context) {
    List<MyObject> items = [
    MyObject(
      name: 'my test',
      uuid: '3fa85f64-5617-4562-b3fc-2c963f66afa6',
    ),
    MyObject(
      name: "bla",
      uuid: "4fa85f64-5717-4562-b3fc-2c963f66afa6",

    ),
    MyObject(
      name: "test",
      uuid: "3fa85f64-5717-4562-b3fc-2c963f68afa6",
    ),
    MyObject(
      name: "test 2",
      uuid: "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    ),


  ];
    return Scaffold(
      appBar: AppBar(
        title: Text('Custom Dropdown Menu'),
      ),
      body: Center(
        child: DropdownMenu<MyObject>(
          initialSelection: selectedValue,
    
          onSelected: (MyObject? newValue) {
            setState(() {
              selectedValue = newValue;
            });
          },
          dropdownMenuEntries: items.map<DropdownMenuEntry<MyObject>>((MyObject scope) {
            return DropdownMenuEntry<MyObject>(
              value: scope,
              label: scope.name,
            );
          }).toList(),
        ),
      ),
    );
  }
}


class MyObject {
  final String name;
  final String uuid;

  MyObject({
    required this.name,
    required this.uuid,
  });
}

Expected results

if I select test then test should be highlighted the next time the menu is opened.

Actual results

With the following code, when item with label test is selected the item with label my test is selected instead, Although the object has different uuid. if I select bla or test 1 then it works as expected.

Code sample

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Custom Dropdown Menu',
      home: DropdownMenuExample(),
    );
  }
}

class DropdownMenuExample extends StatefulWidget {
  @override
  _DropdownMenuExampleState createState() => _DropdownMenuExampleState();
}

class _DropdownMenuExampleState extends State<DropdownMenuExample> {
  MyObject? selectedValue;

  @override
  Widget build(BuildContext context) {
    List<MyObject> items = [
    MyObject(
      name: 'my test',
      uuid: '3fa85f64-5617-4562-b3fc-2c963f66afa6',
    ),
    MyObject(
      name: "bla",
      uuid: "4fa85f64-5717-4562-b3fc-2c963f66afa6",

    ),
    MyObject(
      name: "test",
      uuid: "3fa85f64-5717-4562-b3fc-2c963f68afa6",
    ),
    MyObject(
      name: "test 2",
      uuid: "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    ),


  ];
    return Scaffold(
      appBar: AppBar(
        title: Text('Custom Dropdown Menu'),
      ),
      body: Center(
        child: DropdownMenu<MyObject>(
          initialSelection: selectedValue,
    
          onSelected: (MyObject? newValue) {
            setState(() {
              selectedValue = newValue;
            });
          },
          dropdownMenuEntries: items.map<DropdownMenuEntry<MyObject>>((MyObject scope) {
            return DropdownMenuEntry<MyObject>(
              value: scope,
              label: scope.name,
            );
          }).toList(),
        ),
      ),
    );
  }
}


class MyObject {
  final String name;
  final String uuid;

  MyObject({
    required this.name,
    required this.uuid,
  });
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel beta, 3.22.0-0.1.pre, on macOS 14.4.1 23E224 darwin-arm64, locale en-GB)
[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[!] Xcode - develop for iOS and macOS (Xcode 15.3)
    ✗ Unable to get list of installed Simulator runtimes.
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] VS Code (version 1.87.2)```

</details>

Metadata

Metadata

Assignees

No one assigned

    Labels

    f: material designflutter/packages/flutter/material repository.found in release: 3.19Found to occur in 3.19found in release: 3.22Found to occur in 3.22frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions