state - 如何从 Flutter 中的其他 StatefulWidget 设置/更新 StatefulWidget 的状态?

标签 state flutter

  1. 例如,在下面的代码中,加号按钮可以工作并且能够更新 文本,但减号按钮没有。
  2. 但如果我们按下 FloatingActionButton 则状态会被刷新。
  3. 减号按钮正在更改变量的值,但没有 更新父窗口小部件的状态。

enter image description here

这里是代码.....

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

int number;

EdgeInsets globalMargin = const EdgeInsets.symmetric(horizontal: 20.0, vertical: 20.0);
TextStyle textStyle = const TextStyle(
  fontSize: 100.0,
  color: Colors.black,
);

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    number = number ?? 0;
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Column(
        children: <Widget>[
          new Text(
            number.toString(),
            style: textStyle,
          ),
          new GridView.count(
            crossAxisCount: 2,
            shrinkWrap: true,
            scrollDirection: Axis.vertical,
            children: <Widget>[
              new InkResponse(
                child: new Container(
                    margin: globalMargin,
                    color: Colors.green,
                    child: new Center(
                      child: new Text(
                        "+",
                        style: textStyle,
                      ),
                    )),
                onTap: () {
                  setState(() {
                    number = number + 1;
                  });
                },
              ),
              new Sub(),
            ],
          ),
        ],
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: () {
          setState(() {});
        },
        child: new Icon(Icons.update),
      ),
    );
  }
}

class Sub extends StatefulWidget {
  @override
  _SubState createState() => new _SubState();
}

class _SubState extends State<Sub> {
  @override
  Widget build(BuildContext context) {
    return new InkResponse(
      child: new Container(
          margin: globalMargin,
          color: Colors.red,
          child: new Center(
            child: new Text(
              "-",
              style: textStyle,
            ),
          )),
      onTap: () {
        setState(() {
          number = number - 1;
        });
      },
    );
  }
}

最佳答案

1.On Child Widget : 添加参数 Function 参数

class ChildWidget extends StatefulWidget {
  final Function() notifyParent;
  ChildWidget({Key key, @required this.notifyParent}) : super(key: key);
}

2.On Parent Widget : 创建一个Function让 child 回调

refresh() {
  setState(() {});
}

3.On Parent Widget : 将 parentFunction 传递给 Child Widget

new ChildWidget( notifyParent: refresh );  

4.On Child Widget:调用父函数

  widget.notifyParent();

关于state - 如何从 Flutter 中的其他 StatefulWidget 设置/更新 StatefulWidget 的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481590/

相关文章:

reactjs - 动态 react 设置状态属性

reactjs - 在 React Redux 应用程序中规范化状态的示例是什么?

javascript - ReactJS,在另一个模态之上显示模态

javascript - 将值从数组传递给 React Native 中的函数

sqlite - Flutter:类型 'Future<dynamic>' 不是类型 'Widget' 的子类型

flutter - 当前位置动摇

javascript - 如何更新自定义组件上的 onClick 引用?

sqlite - 在 Sqflite 中插入多条记录

firebase - 从Firebase DB中的 'map'读入Flutter中的ListView

android - 无法在我的 flutter 演示应用程序中的 ListView 中显示 gridview