从有状态 parent 修改有状态 child 小部件中的值

Modify value in statefull child widget from its statefull parent

我有两个小部件。两者都是有状态的 widgets.I 在主页小部件中放置一个按钮和 Child 小部件。单击主页小部件中的按钮时,我想从主页小部件更改 Child 小部件中的计数器变量。我应该为此使用 setState 方法,但我不能从 Child 外部调用它 widget.What 是正确的方法吗?

Parent

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          Child(),
          FlatButton(
            onPressed: () {

              //Increase Child widget's counter variable
              
            },
            child: Text("Button In Parent"),
            color: Colors.amber,
          )
        ],
      ),
    );
  }
}

Child

class Child extends StatefulWidget {
  @override
  _ChildState createState() => _ChildState();
}

class _ChildState extends State<Child> {
  int counter = 0;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.green,
      child: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Text In Child: ',
              style: TextStyle(fontSize: 30),
            ),
            Text(
              //Increase this variable from parent
              counter.toString(),
              style: TextStyle(fontSize: 50),
            ),
          ],
        ),
      ),
    );
  }
}


你必须在同一个 class 中使用 FlatButton 和 setState 否则你无法访问 class 变量,使用下面的代码并尝试理解 setState 的逻辑。祝你有个美好的一天。

class Child extends StatefulWidget {
  @override
  _ChildState createState() => _ChildState();
}

class _ChildState extends State<Child> {
  int counter = 0;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.green,
      child: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Text In Child: ',
              style: TextStyle(fontSize: 30),
            ),
            Text(
              //Increase this variable from parent
              counter.toString(),
              style: TextStyle(fontSize: 50),
            ),
          Expanded(
            child:Container(
              color:Colors.white,
              child:FlatButton(
                onPressed(){
                  setState((){
                    counter=counter+1;
                });
              };
            ),
          ),          
        ),         
      ],
    );
  }
}

您可以使用父控件的 setState 来重建子控件。在父部件中初始化一个计数器变量并递增它并调用 setState.

在您的 FlatButtononTap 父窗口小部件中

setState(() {
   counter++;
});

Child小部件以此为参数Child(counts: counter)

class Child extends StatefulWidget {
  final int counts;

  Child({this.counts});
  @override
  _ChildState createState() => _ChildState();
}

并将其显示在 Child

Text
Text(
  widget.counts.toString(),
  style: TextStyle(fontSize: 50),
),

这是一种方法。