here an example on how my code works having a TextFormField that actually change when updateValue as called
countryDropDownCustomKey.currentState?.updateValue(_initialCountryData);
meanwhile the CountryDropDown doesn’t…
Here I’ve recreated a StateFulWidget and I’ve using a GlobalKey to address the state
I’ve spent 12 hours trying to figure this out… please help
thank you
class CountryDropDownCustom extends StatefulWidget {
CountryDropDownCustom({super.key, this.initialCountryData, required this.onChanged});
PhoneCountryData? initialCountryData;
final ValueChanged<PhoneCountryData> onChanged;
@override
State<CountryDropDownCustom> createState() => CountryDropDownCustomState();
}
class CountryDropDownCustomState extends State<CountryDropDownCustom> {
PhoneCountryData? _initialCountryCode;
late TextEditingController countryCode;
void updateValue(PhoneCountryData? newValue) {
setState(() {
_initialCountryCode = newValue;
countryCode = TextEditingController(text: _initialCountryCode?.countryCode?.toString() ?? '');
});
}
@override
void initState() {
_initialCountryCode = widget.initialCountryData;
countryCode = TextEditingController(text: _initialCountryCode?.countryCode?.toString() ?? '');
super.initState();
}
@override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
flex: 1,
child: TextFormField(
controller: countryCode,
decoration: textInputDecoration,
)),
const SizedBox(
width: 5,
),
Expanded(
flex: 4,
child: CountryDropdown(
initialCountryData: _initialCountryCode,
selectedItemBuilder: (phoneCountryData) {
return Row(
children: [
Padding(
padding: const EdgeInsets.only(right: 10.0),
child: CountryFlag(
height: 20,
width: 30,
countryId: phoneCountryData.countryCode!,
),
),
Flexible(
child: Text(
'+${phoneCountryData.phoneCode} - ${phoneCountryData.country}',
overflow: TextOverflow.ellipsis,
),
),
],
);
},
listItemBuilder: (phoneCountryData) {
return Row(
children: [
Padding(
padding: const EdgeInsets.only(right: 2.0),
child: CountryFlag(
height: 20,
width: 30,
countryId: phoneCountryData.countryCode!,
),
),
Flexible(
child: Text(
'+${phoneCountryData.phoneCode} - ${phoneCountryData.country}',
overflow: TextOverflow.ellipsis,
),
),
],
);
},
decoration: textInputDecoration,
enableFeedback: true,
menuMaxHeight: 300,
iconSize: 18,
printCountryName: false,
onCountrySelected: widget.onChanged,
),
),
],
);
}
}
2
Answers
unfortunatelly is not what I need, here a full code where demostrate that I cannot update the second CountryDropDown based on the first one, meanwhile the 2nd TextFormField (that is just a reference) is changing, I guess that's a CountryDropDown issue... or not? I've tried everything see next example:
Your code requires two changes