我定义了我的应用程序,并且将counter
变量作为如下构造函数传递:
class AppThreePlusThree extends StatelessWidget {
@override
Widget build(BuildContext context) {
var game = new Game();
var counter = 265;
return new GameRedux(
counter: counter,
child: new MaterialApp(
title: '3 + 3',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new Scaffold(
...
...
new Text('$counter'),
...
我在counter
内修改了GameRedux
,我看到了变量的变化,但是它不会影响UI中的Text,为什么它的库存为零?
答案 0 :(得分:2)
您必须将StatelessWidget
小部件设置为StatefulWidget
,因为这是在counter
变量的状态更改时(使用setState()
)重新构建布局的方式。您的代码应如下所示,
class AppThreePlusThree extends StatefulWidget {
_AppThreePlusThreeState createState() => _AppThreePlusThreeState ();
}
class _AppThreePlusThreeState extends State<AppThreePlusThree> {
var counter = 265;
@override
Widget build(BuildContext context) {
var game = new Game();
setState(() => counter = 265);//I seriously doesnt have any idea how you are going to change or update your counter variable. something like this should work
return new GameRedux(
counter: counter,
child: new MaterialApp(
title: '3 + 3',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new Scaffold(
...
...
new Text('$counter'),
...