我正在使用flutter RadioListTile进行单选按钮选择,并在尝试检查时出现问题。即,未选择按钮,但检查后收到值。
有人能帮忙吗?
下面是示例代码,
var _selectedLanguageRadioIndex = 0;
List<String> languageTitle = ["English","हिंदी","ಕನ್ನಡ"];
List<String> languageType = ["en","hi","ka"];
_languageOptions(){
showModalBottomSheet(
isScrollControlled: true,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(15.0)),
),
context: context,
builder: (context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter myState) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.fromLTRB(15, 10, 0, 10),
Flexible(
fit: FlexFit.loose,
child: ListView.separated(
shrinkWrap: true,
itemBuilder: (context, index) {
return Container(
child: RadioListTile(
value: index,
groupValue: _selectedLanguageRadioIndex,
selected: _selectedLanguageRadioIndex == index,
onChanged: (val) => {
setState(() => _selectedLanguageRadioIndex = val as int),
},
title: ListTile(
title: Text(languageTitle[index]
),
controlAffinity: ListTileControlAffinity.trailing,
),
);
},
itemCount: languageTitle.length,
separatorBuilder: (context, index) => Divider(
thickness: 1,
),
)
)
],
);
},
);
}
);
}
1条答案
按热度按时间8oomwypt1#
由于您使用的是
StatefulBuilder
小部件,它会创建自己的State,在您的代码中是StateSetter myState
。这意味着BottomSheet
小部件的状态由myState
关键字控制和更改。在BottomSheet
小部件中,您调用setState
,它会告诉dart更新与setState
链接的小部件相关的UI,因此它不会更新UI。在您的代码中,只需替换以下行:-
完整代码:-