dropdown_button2 icon indicating copy to clipboard operation
dropdown_button2 copied to clipboard

Does DropdownButton2 support keyboard for desktop apps

Open tkumark opened this issue 2 years ago • 5 comments
trafficstars

I am making a dektop app. Does DropdownButton2 support keyboard selection? In the standard DropdownButton I can expand the menu by pressing the enter button and then use keyboard up and down to go to the item and to select press enter again. I tried your example code I could not do that.

tkumark avatar Oct 22 '23 10:10 tkumark

Can you try the latest beta version?

AhmedLSayed9 avatar Oct 22 '23 10:10 AhmedLSayed9

tried with the latest beta keyboard input is working thanks. any eta when it will be out of beta? thanks

tkumark avatar Oct 22 '23 10:10 tkumark

I'm still thinking if we can add pagination or not. It shouldn't take much time if we don't include pagination in the next release :)

AhmedLSayed9 avatar Oct 22 '23 10:10 AhmedLSayed9

tried with the latest beta keyboard input is working thanks. any eta when it will be out of beta? thanks

I have the same problem but I can't solve it with the beta, could you give me an example of the implementation you did :) in this moment need use DropdownButtonFormField2

gusadolfo123 avatar Nov 19 '23 11:11 gusadolfo123

I could solve it in the following way, I can that the DropdownButtonFormField2 does not allow me to select an element with the keyboard until it has not been previously selected, so I had to create an extension of DropdownItem that has the property focusNode which serves me to assign the focus programmatically to any element of the DropdownButtonFormField2. In my case I indicate that if there are no previously selected elements it assigns the focus to the first element of the list otherwise it maintains the focus to the element that at the moment has it.

CustomDropdownItem:

class CustomDropdownItem<T> extends DropdownItem<T> {
  final FocusNode focusNode;

  const CustomDropdownItem({
    super.key,
    required super.value,
    required super.child,
    required this.focusNode,
  });

  @override
  Widget build(BuildContext context) {
    return Focus(
      focusNode: focusNode,
      child: child,
    );
  }
}

Building the DropdownButtonFormField2 with the function onMenuStateChange:

onMenuStateChange: (isOpen) {
 if (isOpen) {
            final CustomDropdownItem<T>? newVal =
                selectedItem ?? (widget.items.isNotEmpty ? widget.items.first : null);
            if (newVal != null) {
              Future.delayed(const Duration(milliseconds: 100), () {
                if (widget.items.length > 1) {
                  if (newVal.focusNode.hasPrimaryFocus || newVal.focusNode.canRequestFocus) {
                    FocusScope.of(context).requestFocus(newVal.focusNode);
                  }
                }
              });
            }
          }
},

Result: mobile123

gusadolfo123 avatar Nov 20 '23 03:11 gusadolfo123

Closing in favor of #339

AhmedLSayed9 avatar Jan 18 '25 05:01 AhmedLSayed9