flutter_multi_formatter中CountryDropDown无法通过setState更新初始国家代码问题求助
flutter_multi_formatter中CountryDropDown无法通过setState更新初始国家代码问题求助
各位大佬,我卡了整整12小时了,实在搞不定来求助!
情况是这样的:我自己封装了一个带GlobalKey的CountryDropDownCustom组件,里面的TextFormField完全正常——当我通过countryDropDownCustomKey.currentState?.updateValue(_initialCountryData)调用状态里的更新方法时,TextFormField能跟着刷新显示新的国家数据。
但换成flutter_multi_formatter里的原生CountryDropDown组件时,同样的操作却完全没反应,它就是不会更新初始选中的国家代码!
我把自定义组件的代码贴出来,麻烦帮我看看问题出在哪:
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; country... }); } }
(注:上面的代码没贴完,countryCode控制器的部分被截断了,但核心逻辑是通过setState更新_initialCountryCode)
我试过各种方式,包括直接给CountryDropDown传新的initialCountryData然后调用setState,也试过用GlobalKey去操作它的状态,但都没用。有没有人遇到过类似的问题?求指点!
备注:内容来源于stack exchange,提问作者Mirakle
相关产品推荐
相关产品推荐

