为什么FlutterSwitch的onToggle在flutter中不起作用?

发布于 2025-01-13 20:44:42 字数 5110 浏览 3 评论 0原文

我在不同的地方使用了相同的代码,它可以工作,但在这里不起作用。我不明白情况。你能帮我解决这个问题吗? 这不起作用

void showFilters(){
    final width = MediaQuery.of(context).size.width;
    final height = MediaQuery.of(context).size.height;
    showCupertinoModalPopup(
        context: context,
        builder: (BuildContext ctx) => Container(
          padding: EdgeInsets.all(20),
            decoration: BoxDecoration(
                color: col.mostDarkGrey
            ),
            height: height,
            child: Column(
              //mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                SizedBox(
                  width: MediaQuery.of(context).size.width*.8,
                  child: Container(
                    decoration: BoxDecoration(
                      color: col.mostLightPurpleAccent,
                      borderRadius: BorderRadius.all(Radius.circular(10.0)),
                      shape: BoxShape.rectangle,
                      border: Border.all(
                        color: col.mostDarkPurpleAccent,
                        width: 3,
                      ),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.all(12.0),
                      child: Center(
                        child: DefaultTextStyle(
                          style: TextStyle(
                              color: col.mostDarkPurpleAccent,
                              fontSize: 18,
                              fontWeight: FontWeight.w800),
                          child: Text(
                            'OPERATÖRLER',
                          ),
                        ),
                      ),
                    )
                  ),
                ),
                SizedBox(
                    width: width*.8,
                    height: height*.45,
                    child: ListView.builder(
                      padding: EdgeInsets.only(top: 6),
                      shrinkWrap: true,
                      primary: false,
                      itemCount: MaintenanceApi.maintenanceUsers.length,
                      itemBuilder: (context, index) {
                        final employee = MaintenanceApi.maintenanceUsers;
                        return Padding(
                            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                            child: Container(
                              padding: EdgeInsets.all(4),
                              decoration: BoxDecoration(
                                color: col.mostLightPurpleAccent,
                                borderRadius:
                                BorderRadius.all(Radius.circular(24.0)),
                                shape: BoxShape.rectangle,
                                border: Border.all(
                                  color: col.mostDarkPurpleAccent,
                                  width: 3,
                                ),
                              ),
                              child: Row(
                                mainAxisAlignment:
                                MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  Padding(
                                    padding: const EdgeInsets.only(left: 16),
                                    child: Text(
                                      '${employee[index].userName}',
                                      style: TextStyle(fontSize: 16),
                                    ),
                                  ),
                                  FlutterSwitch(
                                    width: width >= 750 ? width * .08 : width * .2,
                                    height: width >= 750 ? height * .04 : height * .05,
                                    activeColor: col.mostDarkPurpleAccent,
                                    toggleSize: width >= 750 ? width * .04 : width * .08,
                                    value: employee[index].isActive!,
                                    borderRadius: height * .04,
                                    onToggle: (bool val) {
                                      setState(() {
                                        // debugPrint("1"+employee[index].isActive.toString());//false
                                        employee[index].isActive = val;
                                        // debugPrint("2"+employee[index].isActive.toString());//true
                                        employees[index].isActive = employee[index].isActive;
                                        // debugPrint("3"+employees[index].isActive.toString());//true
                                      });
                                    },
                                  ),
                                ],
                              ),
                            ),
                        );
                      })
                ),
],
            )
        )
    );
  }

我不明白为什么 setState 不起作用。我使用 debugPrint 并调试值正在更改,但没有显示。你们能帮我吗?

I used same code in the different place, it is working but in here is not working. I do not understand the situation. Could you help me to solve this?
This is not working

void showFilters(){
    final width = MediaQuery.of(context).size.width;
    final height = MediaQuery.of(context).size.height;
    showCupertinoModalPopup(
        context: context,
        builder: (BuildContext ctx) => Container(
          padding: EdgeInsets.all(20),
            decoration: BoxDecoration(
                color: col.mostDarkGrey
            ),
            height: height,
            child: Column(
              //mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                SizedBox(
                  width: MediaQuery.of(context).size.width*.8,
                  child: Container(
                    decoration: BoxDecoration(
                      color: col.mostLightPurpleAccent,
                      borderRadius: BorderRadius.all(Radius.circular(10.0)),
                      shape: BoxShape.rectangle,
                      border: Border.all(
                        color: col.mostDarkPurpleAccent,
                        width: 3,
                      ),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.all(12.0),
                      child: Center(
                        child: DefaultTextStyle(
                          style: TextStyle(
                              color: col.mostDarkPurpleAccent,
                              fontSize: 18,
                              fontWeight: FontWeight.w800),
                          child: Text(
                            'OPERATÖRLER',
                          ),
                        ),
                      ),
                    )
                  ),
                ),
                SizedBox(
                    width: width*.8,
                    height: height*.45,
                    child: ListView.builder(
                      padding: EdgeInsets.only(top: 6),
                      shrinkWrap: true,
                      primary: false,
                      itemCount: MaintenanceApi.maintenanceUsers.length,
                      itemBuilder: (context, index) {
                        final employee = MaintenanceApi.maintenanceUsers;
                        return Padding(
                            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                            child: Container(
                              padding: EdgeInsets.all(4),
                              decoration: BoxDecoration(
                                color: col.mostLightPurpleAccent,
                                borderRadius:
                                BorderRadius.all(Radius.circular(24.0)),
                                shape: BoxShape.rectangle,
                                border: Border.all(
                                  color: col.mostDarkPurpleAccent,
                                  width: 3,
                                ),
                              ),
                              child: Row(
                                mainAxisAlignment:
                                MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  Padding(
                                    padding: const EdgeInsets.only(left: 16),
                                    child: Text(
                                      '${employee[index].userName}',
                                      style: TextStyle(fontSize: 16),
                                    ),
                                  ),
                                  FlutterSwitch(
                                    width: width >= 750 ? width * .08 : width * .2,
                                    height: width >= 750 ? height * .04 : height * .05,
                                    activeColor: col.mostDarkPurpleAccent,
                                    toggleSize: width >= 750 ? width * .04 : width * .08,
                                    value: employee[index].isActive!,
                                    borderRadius: height * .04,
                                    onToggle: (bool val) {
                                      setState(() {
                                        // debugPrint("1"+employee[index].isActive.toString());//false
                                        employee[index].isActive = val;
                                        // debugPrint("2"+employee[index].isActive.toString());//true
                                        employees[index].isActive = employee[index].isActive;
                                        // debugPrint("3"+employees[index].isActive.toString());//true
                                      });
                                    },
                                  ),
                                ],
                              ),
                            ),
                        );
                      })
                ),
],
            )
        )
    );
  }

I do not understand why setState is not working. I use debugPrint and debug the value is changing but it is not showing. Could you help me guys?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文