简体   繁体   English

如何在 statelessWidget 类中使用 setState() 函数

[英]How to use setState() function inside statelessWidget class

I can't use setState function inside dialogContent and I got this error :我不能在dialogContent使用setState函数,我收到了这个错误:

The method 'setState' isn't defined for the class 'CustomDialog'

and here I used setState()在这里我使用了setState()

Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: status ? <Widget>[
                    Container(
                      child: Padding(
                        padding: EdgeInsets.only(right: 5,top: 0),
                        child: Image.asset(
                          'assets/images/profile.png',
                          width: 60.0,
                          height: 60.0,
                        ),
                      ),
                    ),
                    Padding(
                        padding: const EdgeInsets.only(top: 0.0),
                        child: Container(
                          width: 200.0,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.only(
                              topLeft: const Radius.circular(50.0),
                              topRight: const Radius.circular(50.0),
                              bottomLeft: const Radius.circular(50.0),
                              bottomRight: const Radius.circular(50.0),
                            ),
                          ),
                          child: Padding(
                            padding: const EdgeInsets.all(20.0),
                            child: Center(
                              child: Text(
                                'test',
                                style: TextStyle(
                                    color: Colors.black,
                                    fontWeight: FontWeight.bold,
                                    fontSize: 25.0
                                ),
                              ),
                            ),
                          ),
                        )
                    ),
                    Padding(
                      padding: const EdgeInsets.only(left: 5.0),
                      child: CustomSwitch(
                        activeColor: Colors.green,
                        value: status,
                        onChanged: (value) {
                          print("VALUE : $value");
                          setState(() {
                            status = value;
                          });
                        },
                      ),
                    ),
                  ] :
                  [
                    Expanded(
                      child: Padding(
                        padding: const EdgeInsets.only(left :25.0),
                        child: Center(
                          child: Text(
                            'test',
                            style: TextStyle(
                              color: Colors.red,
                              fontSize: 15.0,
                            ),
                          ),
                        ),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(left: 0.0),
                      child: CustomSwitch(
                        activeColor: Colors.green,
                        value: status,
                        onChanged: (value) {
                          print("VALUE : $value");
                          setState(() {
                            status = value;
                          });
                        },
                      ),
                    ),
                  ]
              ),

Sure you can't use setState() in StatelessWidget, that's the idea of this widget.当然你不能在 StatelessWidget 中使用 setState(),这就是这个小部件的想法。 StatelessWidget should be used only for "dumb" views that shouldn't hold any state. StatelessWidget 应该仅用于不应保持任何状态的“哑”视图。 If you should set any state to your widget, consider to use StatefulWidget.如果您应该为小部件设置任何状态,请考虑使用 StatefulWidget。

Take a look on official flutter documentation: https://api.flutter.dev/flutter/widgets/StatelessWidget-class.html看看官方颤振文档: https : //api.flutter.dev/flutter/widgets/StatelessWidget-class.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个 class 或文件中使用 setState()? - how to use setState() from another class or file? 如何在内部函数中使用类变量? - How to use class variable inside inner function? 如何在 ViewModel class Z539A3A5859D24FB7BFFAZ9E74D6 中的 function 中使用存储库 object? - How to use a repository object inside a function in a ViewModel class Kotlin? 如何使用类中函数内部返回的 val? - How do you use a val returned inside a function in a class? 如何从 stateLessWidget 更新 StateFullWidget - How to update StateFullWidget from stateLessWidget 如何将应用栏添加到 StatelessWidget? - How to add an app bar to a StatelessWidget? 如何创建 class Searchp 扩展 StatelessWidget 实现 SearchDelegate<an_object> 从头开始</an_object> - how to create class Searchp extends StatelessWidget implements SearchDelegate <an_object> from scratch setState 在 substring function 内部无法正常工作 - setState not working properly with substring function inside 创建 StatelessWidget/StatefulWidget 的子类或 function 的区别? - the difference beteen creating a subclass of StatelessWidget/StatefulWidget or a function? Flutter:如何将变量从StatelessWidget传递到StatefulWidget - Flutter: How to pass variables from StatelessWidget to StatefulWidget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM