繁体   English   中英

从父 Stateful 小部件调用子 Stateful 上的 setState

[英]Invoking setState on child Stateful from parent Stateful widget

当我在子有状态小部件中调用 setState 时。 它显示错误或警告。 有没有办法在有状态小部件中调用有状态小部件而不会导致错误或任何好的方法来做同样的事情?

这是我的示例代码:

父.dart

class Parent extends StatefulWidget {
  @override
  _ParentState createState() => _ParentState();
}

class _ParentState extends State<Parent> {
  var title = "Parent";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: ListView(
          children: <Widget>[
            Text(title),
            Child(init:true),                    // <-- Calling Child Widget
          ],
        ),
      ),
    );
  }
}

child.dart

class Child extends StatefulWidget {
  final bool init;        // <- Showing warning on removing 'final' 
                         //  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: Child.init

  Child({
    Key? key,
    required this.init,
  }) : super(key: key);
  @override
  _ChildState createState() => _ChildState();
}

class _ChildState extends State<Child> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.init ? Colors.red : Colors.blue,
      child: TextButton(
        onPressed: () {
          setState(
            () {
              // widget.init = false;
              // want to change 'wiget.init' but its final
              // removing final causing warning
            },
          );
        },
        child: Text("Click me"),
      ),
    );
  }
}

您可以通过 function 更改初始化变量。

class Parent extends StatefulWidget {
  @override
  _ParentState createState() => _ParentState();
}

class _ParentState extends State<Parent> {
  var title = "Parent";
  var init = true;

  void setInitFalse(){
      setState((){
        init = false;
      })
  }

bool getInit(){
         return init;
      }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: ListView(
          children: <Widget>[
            Text(title),
            Child(init: getInit(), setInitFalse: setInitFalse()),
          ],
        ),
      ),
    );
  }
}

然后在孩子

class Child extends StatefulWidget {
  final Function init;        // <- Showing warning on removing 'final' 
                         //  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: Child.init
  final Function setInitFalse;

  Child({
    Key? key,
    required this.init,
    required this.setInitFalse
  }) : super(key: key);
  @override
  _ChildState createState() => _ChildState();
}

class _ChildState extends State<Child> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.init() ? Colors.red : Colors.blue,
      child: TextButton(
        onPressed: () {
          widget.setInitFalse();
        },
        child: Text("Click me"),
      ),
    );
  }
}

暂无
暂无

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

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