0

i am trying to use a container to select a dropdown menu by wrapping the dropdown button with a container and then use a Gesture Detector. but i am getting an error

The argument type 'void Function(String?)' can't be assigned to the parameter type 'void Function()?'

but if i make use of the unchange function on the dropdown menu its working fine

 GestureDetector(

      onTap: (String? newValue){
        setState(() {
                dropdowntargetGroupValue = newValue!;
              });
                      },
      child: DropdownButtonHideUnderline(
              child: DropdownButton(
                value: dropdowntargetGroupValue,
                icon: Visibility(
                    visible: false, child: Icon(Icons.arrow_downward)),
                items: targetGroup.map((String items) {
                  return DropdownMenuItem(
                      value: items, child: Text(items));
                }).toList(),
                onChanged: (String? newValue) {
                  setState(() {
                    dropdowntargetGroupValue = newValue!;
                  });
                },
              ),
            ),
    ),

James Z
  • 12,209
  • 10
  • 24
  • 44
Yusuf
  • 193
  • 5
  • 18
  • In a second line of code "onTap: (String? newValue)". onTap of GestureDetector is not accept any parameter. I think you conflict with onChanged in DropdownButtonHideUnderline widget. – Wai Han Ko Dec 13 '21 at 17:50
  • The GestureDectectors function signature is `void Function()`. In contrast to the `onChanged` callback of the DropdownButton the GestureDetector cannot know which value was selected. What are you trying to achieve by wrapping your DropdownButton in a GestureDetector in the first place? – hnnngwdlch Dec 13 '21 at 18:00
  • Thank you, I want to be able to tap on the parent container to select from the dropdown instead of using the dropdown arrow button – Yusuf Dec 13 '21 at 18:11
  • Okay, looks like this [answer](https://stackoverflow.com/questions/57529394/how-to-open-dropdownbutton-when-other-widget-is-tapped-in-flutter) could be helpful in your case. – hnnngwdlch Dec 13 '21 at 18:28
  • Thanks a lot man. I will check it out now. – Yusuf Dec 13 '21 at 18:43

1 Answers1

0

You can't use onChanged callback in onTap.

I don't know what you're trying to do by wrapping DropdownButton with GestureDetector here but to make the DropdownButton open when another widget is tapped you've to make custom DropdownButton or use existing one. If you trying to do something when the DropdownButton is tapped, DropdownButton has it's own onTap property, you can use it.

Ahmed Elsayed
  • 538
  • 1
  • 4
  • 11
  • Thank you Ahmed. What wanted to do is to select the dropdown options once I tap/click on the container to parent the dropdown button. If that is achievable – Yusuf Dec 14 '21 at 06:50
  • You've to use Custom DropdownButton, check this [answer](https://stackoverflow.com/questions/57529394/how-to-open-dropdownbutton-when-other-widget-is-tapped-in-flutter) – Ahmed Elsayed Dec 14 '21 at 11:17
  • thanks man. im grateful. – Yusuf Dec 14 '21 at 11:44