为颤动中的下拉按钮选择初始值



我已经为我的问题找到了类似的答案,但这还不够,也许我的方法很难。

以下是我要做的,我有一个API国家价值。为了注册用户,我使用了一个下拉列表,如下所示,将CountryApi值映射到其项目。我手里只有国名。

我如何设置下拉列表的初始值以与我的国家名称相匹配?

国家/地区选择下拉列表

CountryModal _selectedCountry;
onChangeDropdownItem(CountryModel selectedCountry) {
setState(() {
// fieldFocusChange(context, _countryFocusNode, _phoneFocusNode);
_selectedUserCountry = selectedCountry;
_userCountry = _selectedUserCountry.name;
countryCodeTxt = _selectedUserCountry.dial_code;
countryCode = _selectedUserCountry.code;
_userCountryId = _selectedUserCountry.id.toString();
});
}
/////
userCountryDropdown = Container(
padding: EdgeInsets.all(2.0),
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(
10.0,
)),
color: Color(0xFFF0F0F0),
shape: BoxShape.rectangle),
child: DropdownButton<CountryModel>(
isExpanded: true,
hint: Text(
'Select Country',
style: kMainContentStyleLightBlack,
),
autofocus: true,
value: _selectedUserCountry,
isDense: false,
onChanged: onChangeDropdownItem,
items: _countryList.map((country) {
return DropdownMenuItem<CountryModel>(
child: new Text(country.name),
value: country,
);
}).toList(),
style: kMainContentStyleLightBlack,
),
);

我的国家API以这种格式查看

CountryAPI

[
{
"id": 1,
"code": "AF",
"dial_code": "+93",
"name": "افغانستان"
},
{
"id": 2,
"code": "AX",
"dial_code": "+358",
"name": "Åland"
},
....

有人能帮我解决这个问题吗?

value属性用于指定下拉菜单的默认选定值
Container(
padding: EdgeInsets.all(2.0),
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(
10.0,
)),
color: Color(0xFFF0F0F0),
shape: BoxShape.rectangle),
child: DropdownButton(
isExpanded: true,
hint: Text(
'Select Country',
style: kMainContentStyleLightBlack,
),
autofocus: true,
value: _selectedUserCountry,
isDense: false,
onChanged: onChangeDropdownItem,
items: _countryList.map((country) {
return DropdownMenuItem(
child: new Text(country.name),
value: country.name,
);
}).toList(),
style: kMainContentStyleLightBlack,
),
);

我没怎么读你的问题,但这里有你的参考资料下拉值是此处的默认值

Padding(
padding: EdgeInsets.all(8.0),
child: DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.arrow_drop_down),
iconSize: 24,
elevation: 16,
// style: TextStyle(color: Colors.white),
underline: Container(
height: 2,
width: double.infinity,
// color: Colors.deepPurpleAccent,
),
onChanged: (String newValue) {
setState(() {
print(newValue);
dropdownValue = newValue;
});
},
items: <String>['Male', 'Female']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),

相关内容

  • 没有找到相关文章

最新更新