如何从父窗口小部件更改一个子窗口小部件的状态,而子窗口小部件是单独文件中的单独类?

比基·德卡(Biki Deka)

如何通过单击RaisedButton将框的颜色从蓝色更改为绿色?我正在将Flutter用于Web。

class _MyAppState extends State<MyApp> {
  Color colorOfBox = Colors.blue;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "App",
      home: Scaffold(
        body: Container(
          padding: EdgeInsets.all(50),
          child: Center(
            child: Column(
              children: <Widget>[
                Text("This blue has to change to green."),    
                Box(colorOfBox),
                RaisedButton(
                  color: Colors.red,
                  onPressed: () {
                    setState(() {
                      colorOfBox = Colors.green;

                    });
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
class Box extends StatefulWidget {
  Color c;
  Box(this.c, {Key key}) : super(key: key);

  @override
  _BoxState createState() => _BoxState(c);
}

class _BoxState extends State<Box> {
  Color c;
  _BoxState(this.c);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 40,
      height: 40,
      color: c,
    );
  }
}
玛丽

Color c从...中删除_BoxState

class Box extends StatefulWidget {
  Color c;
  Box(this.c, {Key key}) : super(key: key);

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

class _BoxState extends State<Box> {

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 40,
      height: 40,
      color: widget.c,
    );
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档