[英]how to pass index inside Radio listTile in Flutter?
我正在嘗試在單選按鈕列表磁貼中傳遞索引,以設置區域設置語言並將設置保存到共享首選項中。
這是我要傳遞索引的主要代碼的一部分:
body: CupertinoPageScaffold(
child: Container(
child: SingleChildScrollView(
child: Container(
child: CupertinoFormSection.insetGrouped(
header: Text('choose_a_language'.tr()),
children: [
RadioListTile( title: Text('English'), value: Language.English, groupValue: _selecLangIndex, subtitle: Text('English'), selected: _selectedIndex == 0 , onChanged: (newValue) =>
setState(() => _selecIndex = newValue)),
RadioListTile(title: Text('French'), value: Language.French, groupValue: _selecLangIndex, subtitle: Text('French'), selected: _selectedIndex == 1, onChanged: (newValue) =>
setState(() => _selecIndex = newValue)),
RadioListTile(title: Text('Italian'), value: Language.Italian, groupValue: _selecLangIndex, subtitle: Text('Italian'), selected: _selectedIndex == 2, onChanged: (newValue) =>
setState(() => _selecIndex = newValue)),
TextButton(onPressed:
_saveSettings,
child: Text('save'.tr().toUpperCase(),
style: TextStyle(fontWeight: FontWeight.bold),),
現在在Save button
上,使用 onPressed 方法,我可以保存設置,但我無法設置語言環境。 我也嘗試實現此代碼,但沒有任何結果。
void setLocale() {
int index = 0;
setState(() => _selectedIndex == index );
if (index == 0){
context.setLocale(Locale('en','US'));
}
else if (index == 1){
context.setLocale(Locale('fr','FR'));
}
else if (index == 2){
context.setLocale(Locale('it','IT'));
}
print(index);
}
你可以做這樣的事情
onChanged: (newValue) {
setLocale(); //
_selecIndex = newValue ;
setState((){});
},
如果您的setLocale
沒有使用/更新_selectedIndex
。 您可以在setLocale(0)
上傳遞索引,它不需要 setState 因為我們已經調用了onChanged
的 setState end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.