保存modalbottomsheet状态
在 StatefulWidget 中,我想使用 ChoiceChip 创建一个 showModalBottomSheet。但是,我希望在关闭模态表时保留 ChoiceChip 状态。 我的意思是 ChoiceChip 小部件中的“选定”属性,这是我的代码片段,感谢您的帮助。
class CustomFilterChip extends StatefulWidget {
final String chipName;
CustomFilterChip(this.chipName);
@override
State<CustomFilterChip> createState() => _CustomFilterChipState();
}
class _CustomFilterChipState extends State<CustomFilterChip> {
bool status = false;
@override
Widget build(BuildContext context) {
return FilterChip(
label: Text(widget.chipName),
selected: status,
backgroundColor: Colors.purple[50],
selectedColor: kCustomGreen,
onSelected: (selected) {
setState(() {
statusHere = !statusHere;
});
},
);
}
}
showModalBottomSheet(
context: context,
builder: (ctx){
return Container(
child: Column(
children: [
Container(
child: Text(
'select your filter chip'
style: TextStyle(fontSize: 10.sp),
),
),
SizedBox(
height: 20,
),
Container(
child: Wrap(
spacing: 25,
children: [
CustomFilterChip('chip1')
CustomFilterChip('chip2')
CustomFilterChip('chip3')
],
),
In a StatefulWidget, I'd like to create a showModalBottomSheet with ChoiceChip. However, I want the ChoiceChip status to be preserved when the modalsheet is closed.
I mean the 'selected' property in ChoiceChip widget this is my code snippets , I appreciate your assistance.
class CustomFilterChip extends StatefulWidget {
final String chipName;
CustomFilterChip(this.chipName);
@override
State<CustomFilterChip> createState() => _CustomFilterChipState();
}
class _CustomFilterChipState extends State<CustomFilterChip> {
bool status = false;
@override
Widget build(BuildContext context) {
return FilterChip(
label: Text(widget.chipName),
selected: status,
backgroundColor: Colors.purple[50],
selectedColor: kCustomGreen,
onSelected: (selected) {
setState(() {
statusHere = !statusHere;
});
},
);
}
}
showModalBottomSheet(
context: context,
builder: (ctx){
return Container(
child: Column(
children: [
Container(
child: Text(
'select your filter chip'
style: TextStyle(fontSize: 10.sp),
),
),
SizedBox(
height: 20,
),
Container(
child: Wrap(
spacing: 25,
children: [
CustomFilterChip('chip1')
CustomFilterChip('chip2')
CustomFilterChip('chip3')
],
),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果是这种情况,您需要在小部件内拥有“selected”变量的主实例,从中调用底部工作表打开,然后将“selected”变量的值传递给在底部内呈现的小部件床单。
If that the case, you need to have the main instance of 'selected' variable inside the widget from where you are calling the bottom sheet to open and then pass the value of 'selected' variable to the widget which is being rendered inside the bottom sheet.