1

Suppose there is 2 item in my DropdownList. If user choose 1st item initially , next time only 2nd item should available to select( i.e.1st item non-clickable).

     child: DropdownButton<Datum>(
  enabled= enabled_Item,
    value: _selectedTest,
    hint: Text(""),
    //underline: SizedBox(),
    isExpanded: true,
    items: data
        .map(
            (Datum data) => DropdownMenuItem<Datum>(
                  child: Text("${data.testName}"),
                  enabled: data.testId != _selectedTest,
                  value: data,
                ))
        .toList()
        .cast<DropdownMenuItem<Datum>>(),
    onChanged: (value) {
      print(
          "This is the TestName : ${value!.testName}");
      print(
          "This is the EncTestId which is need to get Test Fee : ${value.testId}");
      setState(() {
        encTestId = value.testId; // == SELCTED TEST from drop down 'encTestId' needed for to get Test Fee
        testName = value.testName;
        _selectedTest = value;
      });
      //GetTestByLab(value!.encPartnerId); // passing encid to my next API function
    }),
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Ayyus Saha
  • 11
  • 2

2 Answers2

0

You can't remove the currently selected / default value from the list of selectable items. If you do, this will happen:

https://stackoverflow.com/a/61425939/3410660

TL;DR You have to disable the currently selected / default value rather than completely removing it

So a workaround is to set the value of enabled (boolean) to each item of the DropdownButton. Like this:

                       child: DropdownButton<Datum>(
                        value: _selectedTest,
                        hint: Text(""),
                        isExpanded: true,
                        items: data.map((Datum data) => DropdownMenuItem<Datum>(
                                      child: Text("${data.testName}"),
                                      enabled: data != _selectedTest,
                                      value: data,
                                    )).toList().cast<DropdownMenuItem<Datum>>(),

I'm assuming Datum data is a Model with it's hashCode and bool operator == overriden to make conditionals like this data != _selectedTest posible.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Eric Aig
  • 972
  • 1
  • 13
  • 18
  • Create a bool on state `bool enabled_Item= false;` then `enabled=enabled_Item` after then as you suggested `enabled: data != _selectedTest,` This is how its need to be implement right? because its giving error – Ayyus Saha Sep 13 '21 at 07:29
  • Do you have an ID or something in Datum model? I mean a way to identify each item uniquely. If you do, then it should be like this `enabled: data.id != _selectedTest`. Assuming _selectedTest = a valid ID as well. – Eric Aig Sep 13 '21 at 07:43
  • It would be nice to know if this worked for you :) – Eric Aig Sep 13 '21 at 09:56
  • Thanks for your time. But still give error `Undefined name 'enabled'.` I updated my code in my question – Ayyus Saha Sep 13 '21 at 10:09
  • I see you have `enabled= enabled_Item,` as a param in `DropdownButton`. I don't know if this is an error on your part, but if it isn't, then a syntax like that is invalid in Flutter when defining params of a Widget. You should remove this line and only rely on `enabled: data.testId != _selectedTest,` of each item to enable/disable items – Eric Aig Sep 13 '21 at 10:29
0

There are different ways to solve this issues, you can create a list of integers and add the clicked element's index to it, and check if the clicked index already exists in that list, then do not perform the operation you want, else you add the current index and execute the code.

Second option:

you might be storing the clicked data to any list or model, check if the current value already exists there, but this way you cannot click same values even the list index is different.

hope this will help you out.

Akhlaq Shah
  • 340
  • 2
  • 8