35
loading...
This website collects cookies to deliver better user experience
DropdownFormField()
widget. A region dropdown field and a district dropdown field. class RegionDropdownFormField extends StatelessWidget {
const RegionDropdownFormField({
Key? key,
required this.state,
}) : super(key: key);
final _AddFarmerScreenController state;
@override
Widget build(BuildContext context) {
return Expanded(
flex: 5,
child: DropdownButtonFormField(
focusNode: state.regionFocusNode,
decoration: FormStyles.textFieldDecoration(labelText: 'Region'),
onChanged: (String? value) {
state.setState(() {
state.dropdownMenuItems = state._districtItem(value!);
state.value = state.dropdownMenuItems!.first.value;
});
},
validator: state.farmer.validateRequiredField,
onSaved: state.farmer.saveFarmerCategory,
items: Region.all
.map((e) => DropdownMenuItem(
child: Text(e),
value: e,
))
.toList(),
),
);
}
}
RegionDropdownFormField()
the onChanged:
function will be triggered.(String? value) {
state.setState(() {
state.districtDropdownMenuItems = state._getDistrictItems(value!);
state.districtValue = state.districtDropdownMenuItems!.first.value;
});
}
setState()
since we want the UI to update. Within setstate()
, state.districtDropdownMenuItems = state._getDistrictItems(value!);
creates a list of dropdownMenuItem()
based on the value selected by the user. state.districtDropdownMenuItems
will be assigned to the items:
property of the DistrictDropdownFormField()
.state.districtValue = state.districtDropdownMenuItems!.first.value;
selects the first value of the newly created state.districtDropdownMenuItems
. This variable will be assigned to the value:
property of the DistrictDropdownFormField()
. Failure to do this will create the below error.════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building Builder(dirty, dependencies: [_FocusMarker]):
There should be exactly one item with [DropdownButton]'s value: District 6.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 850 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
class DistrictDropdownFormField extends StatelessWidget {
const DistrictDropdownFormField({
Key? key,
required this.state,
}) : super(key: key);
final _AddFarmerScreenController state;
@override
Widget build(BuildContext context) {
return Expanded(
flex: 5,
child: DropdownButtonFormField(
focusNode: state.districtFocusNode,
decoration: FormStyles.textFieldDecoration(labelText: 'District'),
onChanged: (value) =>
state._handleDropdownOnChanged(state.districtFocusNode),
validator: state.farmer.validateRequiredField,
onSaved: state.farmer.saveDistrict,
value: state.districtValue,
items: state.districtDropdownMenuItems,
),
);
}
}
DropdownFormField()
but note, value: state.districtValue
, and items: state.districtDropdownMenuItems,
are dependent on the region selected in the DistrictDropdownFormField()
.35