我正在try 将文本从子窗口小部件设置为父窗口小部件.但是文本不会反映在父小部件中.
也try 使用setState(),但仍无法获得预期结果.
以下是我的代码:
void main() => runApp(new TestApp());
class TestApp extends StatefulWidget {
@override
_TestState createState() => new _TestState();
}
class _TestState extends State<TestApp>{
String abc = "";
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
body: new Column(
children: <Widget>[
new Text("This is text $abc"),
TestApp2(abc)
],
),
),
);
}
}
class TestApp2 extends StatefulWidget {
String abc;
TestApp2(this.abc);
@override
_TestState2 createState() => new _TestState2();
}
class _TestState2 extends State<TestApp2>{
@override
Widget build(BuildContext context) {
return new Container(
width: 150.0,
height: 30.0,
margin: EdgeInsets.only(top: 50.0),
child: new FlatButton(
onPressed: (){
setState(() {
widget.abc = "RANDON TEXT";
});
},
child: new Text("BUTTON"),
color: Colors.red,
),
);
}
}
我错过什么了吗?