Flutter-multilevel Combobox value fetching from api state and city - flutter

First ComboBox contains a list of States(provinces)
Combobox(
isExpanded: true,
placeholder: const Text("Select State..."),
items: states!
.map((e) => ComboboxItem<Province.State>(
value: e,
child: Text(e.name.toString()),
))
.toList(),
value: selectedState,
onChanged: (value) {
setState(() {
if (value != null) {
selectedState = value as Province.State?;
selectedDistrict = null;
districts!.clear();
getDistrictsFromServer(
selectedState?.id as int);
}
});
},
)),
The second ComboBox will be populated by the list of districts based on the selected state in the first one. Here's the code...
Combobox(
isExpanded: true,
placeholder: const Text("Select District..."),
disabledHint:
const Text("Select a state first..."),
items: districts!
.map((district) => ComboboxItem<District>(
value: district,
child: Text(district.name.toString()),
))
.toList(),
value: selectedDistrict,
onChanged: (value) {
setState(() {
if (value != null) {
selectedDistrict = value as District?;
}
});
},
)
When the app runs the first ComboBox loads the states and when a state is selected the districts are fetched (json api) however the second combo does not update its items (the districts) when we choose any item in any other ComboBox (excluding the two in question) the SecondCombo just refreshes itself and is populated with the correct district names.

Related

Error: Either zero or 2 or more [DropdownMenuItem]s were detected with the same value, Scenario explained in description

I have 2 dropdowns, if I selected 1st one, based on that I am populating second dropdown list, same as states in country. for example when I select 1st dropdown, value is India, in second dropdown india's states are populating, but when I am selecting 2nd time, value is America, then I am getting below error;
Error: Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
How to fix this,
AppDropdown(
hint: Text('Select country'),
onChanged: (value) {
_getAllStates(value);
setState(() {
_countryDetails.country = value.toString();
countryID = value;
});
},
validator: (val) => (val == null ? 'Required' : null),
items: _countryList
.map((e) => DropdownMenuItem(
child: Text(e.country.toString().toUpperCase()),
value: e.id))
.toList(),
),
AppDropdown(
hint: Text('Select state'),
items: _stateList
.map((e) => DropdownMenuItem(
child: Text(e.state.toString().toUpperCase()),
value: e.id))
.toList(),
onChanged: (value) {
setState(() {
_stateDetails.state = value.toString();
stateID = value;
});
},
validator: (val) => (val == null ? 'Required' : null),
),

How to disable Dropdownlist once user has selected an item in the Dropdownlist in flutter

My DropdownMenuItem coming from API. How to disable the dropDown once user select a item from the list?
child: DropdownButton<Partner>(
value:
_selectedLab, //USER SELECTED DROPDOWN ITEM VALUE
hint: Text("Select Lab"),
isExpanded: true,
items: data.map((Partner data) =>
DropdownMenuItem<Partner>(
child: Text("${data.partnerName}"),
value: data,
)).toList().cast<DropdownMenuItem<Partner>>(),
onChanged: (val) {
setState(() {
_selectedLab = val!;
encLabId = val.encPartnerId;
getTestByLabResult = getTestByLab();
});
},
),
Create a bool on state bool absoreTap = false; and wrap you DropdownButton with AbsorbPointer like
AbsorbPointer(
absorbing: absoreTap,
child: DropdownButton<Partner>(
value:
_selectedLab, //USER SELECTED DROPDOWN ITEM VALUE
hint: Text("Select Lab"),
isExpanded: true,
items: data.map((Partner data) =>
DropdownMenuItem<Partner>(
child: Text("${data.partnerName}"),
value: data,
)).toList().cast<DropdownMenuItem<Partner>>(),
onChanged: (val) {
setState(() {
_selectedLab = val!;
encLabId = val.encPartnerId;
getTestByLabResult = getTestByLab();
absoreTap = true; ///
});
},
),
You can also check Ignore-pointer here is the different between them

Searchable Dropdown search is not working when i access the id as value inside DropdownMenuItem | Flutter

Iam using searchable_dropdown: ^1.1.3 package to search elements in dropdownmenu, when i use district (which give district name) for the value inside DropdownMenuItem it works properly. But the problem is when a user selects district i want the value as districtId. but when i give districtId as value the search is not working. Below is my code.
Center(
child: SearchableDropdown.single(
isExpanded: true,
value: selectedName,
hint: "Select one",
searchHint: "Select one",
// hint: Text('Select Name'),
items: districtList.map(
(list) {
return DropdownMenuItem(
child: Text(list['district']),
value: list['districtId'],
);
},
).toList(),
onChanged: (value) {
setState(() {
selectedDistrict = value;
print(selectedDistrict );
});
},
),
),
This is the package https://pub.dev/packages/searchable_dropdown

In multiple dropdown menu, how to disable an option if already selected in Flutter?

I have 4 different dropdown fields with 5 options and I want to disable/remove an option from other fields if the option is already selected.
Screenshot:
Screenshot 1
Screenshot 2
DropDown Menu Code:
String opemo1, opemo2, opemo3, opemo4;
List<String> emoji = [
"❤️",
"🤩",
"✌️",
"😂",
"😡",
];
DropdownButtonFormField(
validator: (value) =>
value == null ? 'required' : null,
hint: Text('❤️'),
value: opemo1,
icon: Icon(Icons.arrow_drop_down),
iconSize: 36,
isExpanded: true,
style: TextType.regularDarkText,
onChanged: (newValue) {
setState(() {
opemo1 = newValue;
pollDataController.setop1Emoji(newValue);
});
},
items: emoji.map((opemo1) {
return DropdownMenuItem(
value: opemo1,
child: Text(opemo1),
);
}).toList(),
),
To manage it you need to remove the selected emoji from List<String> emoji.
onChanged: (newValue) {
setState(() {
emoji.removeWhere((element) => element == newValue); /// This removed selected emoji.
opemo1 = newValue;
pollDataController.setop1Emoji(newValue);
});
},
You can replace your onChanged with this snippet.

change default value of DropDownFormField form null to ""

I have a dropDownFormField with certain items now I want to insert the selected item into my model if the item is selected, since the field is not required I want to insert an empty string "" if the item is not selected.
I couldn't achieve that.
Tried giving a ternary option on onChange.
child: DropdownButtonFormField(
decoration: InputDecoration(border: OutlineInputBorder()),
hint: Text("Select instruction type"),
items: DummyData()
.instructions
.map(
(value) => DropdownMenuItem(
child: Text(value),
value: value,
),
)
.toList(),
onChanged: (val) {
if (val == null) {
setState(() {
selectedInstructionItem = "";
});
} else {
setState(() {
selectedInstructionItem = val;
});
}
},
value: selectedInstructionItem == null ? "" : selectedInstructionItem,
),
I get a null value while looping and getting values I inserted.