gpt4 book ai didi

flutter - 下拉选框保持选择

转载 作者:行者123 更新时间:2023-12-03 04:21:22 25 4
gpt4 key购买 nike

我有一个用于英语和马拉雅拉姆语的语言选择下拉菜单。选择语言后,选择保持空白。只能看到提示文字。

我没有任何错误。 flutter 医生很好

              Container(
height: 250,
width: double.infinity,
child: Column(
children: <Widget>[
Flexible(
flex: 0,
child: Padding(
padding: EdgeInsets.only(left: 10, right: 10, top: 15),
child: Container(
width: double.infinity,
height: 45,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5),
border: Border.all(color: Colors.blueGrey)
),
child: DropdownButton(
isExpanded: true,
itemHeight: 50,
icon: Icon(Icons.arrow_drop_down),
iconSize: 40,
underline: SizedBox(),
hint: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(AppLocalization.of(context).getTranslatedValues('selectlang'),style: TextStyle(fontSize: 15,color: Colors.black54),),
),
onChanged: (Language language) {
_changeLanguage(language);
setState(() {
_selectedeLang = language;
});
},
items: Language.languageList()
.map<DropdownMenuItem<Language>>((lang) => DropdownMenuItem(
value: lang,

child: Row(
children: <Widget>[
Center(child: Text((lang.name),style: TextStyle(fontSize: 15),))
],
),
))
.toList(),
),
),
),
),
Flexible(
flex: 0,
child: Padding(padding: EdgeInsets.only(left: 8,top: 25,bottom: 8),
child: GestureDetector(
child: Container(
height: 45,
width: 200,
child: Center(child: Text(AppLocalization.of(context).getTranslatedValues('submit'),style: TextStyle(color: Colors.white,fontSize: 12,fontWeight: FontWeight.bold),),),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Color.fromRGBO( 34, 83, 148,1),
),
),
onTap: (){
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => RegisterScreen(),
),
);

},),
),
),
],
),
)

变更功能
 void _changeLanguage(Language language) {
Locale _temp;
switch (language.languageCode) {
case 'en':
_temp = Locale(language.languageCode, 'US');
break;
case 'ml':
_temp = Locale(language.languageCode, 'IN');
break;
default:
_temp = Locale(language.languageCode, 'US');
}

MyApp.setLocale(context, _temp);
}

最佳答案

您需要在自己的DropdownButton中添加value: dropdownValue

    DropdownButton(value:_selectedeLang, ...

同时更新您的语言类(class):
class Language {
final int id;
final String name;
final String flag;
final String languageCode;
Language(this.id, this.name, this.flag, this.languageCode);

// add these operators, so could compare two Language instances
@override
bool operator ==(Object other) => other is Language && other.id == id;

@override
int get hashCode => id.hashCode;

static List<Language> languageList() {
return <Language>[
Language(1, 'English', '🇺🇸', 'en'),
Language(2, 'മലയാളം', '🇮🇳', 'ml'),
];
}
}

关于flutter - 下拉选框保持选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62470089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com