简体   繁体   English

在 StatefulWidget Flutter 中访问变量的最佳方式

[英]Best way to access variable in StatefulWidget Flutter

I'm trying to create a radio button widget.我正在尝试创建一个单选按钮小部件。 Everything is fine until I have to get the state of the button.一切都很好,直到我必须获得按钮的状态。 (if it's clicked or not) (如果它被点击或没有)

I made something really simple to get it, I created a bool variable in the StatefulWidget and I'm using it in the state class to update the widget when it gets clicked, and I return this variable with a function in the StatefulWidget.我做了一些非常简单的事情来获取它,我在 StatefulWidget 中创建了一个 bool 变量,我在状态类中使用它来在点击时更新小部件,然后我用 StatefulWidget 中的函数返回这个变量。

It works well but it gives me this warning :它运行良好,但它给了我这个警告:

This class (or a class that this class inherits from) is marked as '@immutable', but one or more of its instance fields aren't final: RadioButton._isSelecteddart(must_be_immutable)

But how am I supposed to access variable if I declare them in the state class?但是如果我在 state 类中声明它们,我应该如何访问变量?

I saw a lot of ways to deal with this problem but they all look way too much for a simple problem like this.我看到了很多处理这个问题的方法,但对于像这样的简单问题来说,它们看起来都太多了。

Here is my StatefulWidget :这是我的 StatefulWidget :

class RadioButton extends StatefulWidget{

  bool _isSelected = false;

  bool isSelected() {
    return _isSelected;
  }

  @override
  _RadioButtonState createState() => new _RadioButtonState();
}

And my State :而我的状态:

class _RadioButtonState extends State<RadioButton> {


  void _changeSelect(){
    setState(() {
      widget._isSelected = !widget._isSelected;
    });
  }

  @override
  Widget build(BuildContext context){
    return GestureDetector(
      onTap: _changeSelect,
      child: Container(
        width: 16.0,
        height: 16.0,
        padding: EdgeInsets.all(2.0),
        decoration: BoxDecoration(
            shape: BoxShape.circle,
            border: Border.all(width: 2.0, color: Colors.black)),
        child: widget._isSelected
            ? Container(
                width: double.infinity,
                height: double.infinity,
                decoration:
                    BoxDecoration(shape: BoxShape.circle, color: Colors.black),
              )
            : Container(),
      )
    );
  }
}

and to access the variable, I declare the widget as a variable of my app class and I use the function isSelected on it :为了访问变量,我将小部件声明为我的应用程序类的变量,并在其上使用函数 isSelected :

  RadioButton myRadio = new RadioButton();

  ...

  print(myRadio.isSelected()); //For exemple

I could let this code but I want to learn the best way to do this.我可以让这个代码,但我想学习最好的方法来做到这一点。

The proposed way is also correct but preferred way is to have them in your State class as it maintains the State of your widget建议的方法也是正确的,但首选方法是将它们放在 State 类中,因为它维护小部件的状态

Example :例子 :

class _RadioButtonState extends State<RadioButton> {

  bool _isSelected = false;

  bool isSelected() {
    return _isSelected;
  }
  // other code here

}
  1. The Widgets are Immutable in flutter, So you cannot modify the fields in the Radio widget.小部件在颤动中是不可变的,因此您无法修改 Radio 小部件中的字段。

  2. you can only change the values in State class您只能更改 State 类中的值

  3. you better assign the boolean value inside the State class and then use it.您最好在 State 类中分配布尔值,然后使用它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM