AhmedLSayed9 / dropdown_button2

Flutter's core Dropdown Button widget with steady dropdown menu and many other features.
https://pub.dev/packages/dropdown_button2
MIT License
264 stars 122 forks source link

Allow handling the handleTap event from a GlobalKey #304

Open MarvinQuevedo opened 2 months ago

MarvinQuevedo commented 2 months ago

We need to show the dropdown from a external widget equal to old versions

AhmedLSayed9 commented 2 months ago

This is already applicable.

Starting from version 3.0.0-beta.13, instead of:

final dropdownKey = GlobalKey<DropdownButton2State>();

@override
Widget build(BuildContext context) {
  return Column(
    children:[
      DropdownButton2<String>(
        // Other properties...
        key: dropdownKey,
      );
      // Open the dropdown programmatically, like when another button is pressed:
      ElevatedButton(
        onTap: () => dropdownKey.currentState!.callTap(),
      ),
    ],
  );
}

do:

final openDropdownListenable = ValueNotifier<Object?>(null);

@override
Widget build(BuildContext context) {
  return Column(
    children:[
      DropdownButton2<String>(
        // Other properties...
        openDropdownListenable: openDropdownListenable,
      );
      // Open the dropdown programmatically, like when another button is pressed:
      ElevatedButton(
        onTap: () => openDropdownListenable.value = Object(),
      ),
    ],
  );
}
MarvinQuevedo commented 1 month ago

Hi, If I am not wrong, you can see here that the state class is private, on my PR I make It to public removing the underscore and make public the handleTab method too, the two are private on your current code,,

https://github.com/AhmedLSayed9/dropdown_button2/blob/master/packages/dropdown_button2/lib/src/dropdown_button2.dart#L396

AhmedLSayed9 commented 1 month ago

Hi, If I am not wrong, you can see here that the state class is private, on my PR I make It to public removing the underscore and make public the handleTab method too, the two are private on your current code,,

https://github.com/AhmedLSayed9/dropdown_button2/blob/master/packages/dropdown_button2/lib/src/dropdown_button2.dart#L396

Yes, that's on purpose.

You don't need to call the callTap method anymore. just do:

final openDropdownListenable = ValueNotifier<Object?>(null);

@override
Widget build(BuildContext context) {
  return Column(
    children:[
      DropdownButton2<String>(
        // Other properties...
        openDropdownListenable: openDropdownListenable,
      );
      // Open the dropdown programmatically, like when another button is pressed:
      ElevatedButton(
        onTap: () => openDropdownListenable.value = Object(),
      ),
    ],
  );
}