保存modalbottomsheet状态

发布于 2025-01-19 17:24:24 字数 1517 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪装你 2025-01-26 17:24:24

如果是这种情况,您需要在小部件内拥有“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文