flutter - 当我更新我的列表颤动时我的屏幕没有更新
问题描述
您好,我是 Flutter 开发的新手,我尝试制作一个简单的测验应用程序并将分数保存在列表中。
我有一个图标列表,我把它放在一个 Row 小部件中,所以当我按下真或假按钮时,我的列表将一个小部件(图标)添加到我的行并显示它。
但是当我按下按钮时我的屏幕不会更新,直到我热重新加载或热重启应用程序
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(Quizzler());
class Quizzler extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.grey.shade900,
body: SafeArea(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: QuizPage(),
),
),
),
);
}
}
class QuizPage extends StatefulWidget {
@override
_QuizPageState createState() => _QuizPageState();
}
class _QuizPageState extends State<QuizPage> {
List<Widget> scorekepper = [];
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
'This is where the question text will go.',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(15.0),
child: FlatButton(
textColor: Colors.white,
color: Colors.green,
child: Text(
'True',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
),
onPressed: () {
scorekepper.add(Icon(
Icons.check,
color: Colors.green,
));
},
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(15.0),
child: FlatButton(
color: Colors.red,
child: Text(
'False',
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
onPressed: () {
scorekepper.add(
Icon(
Icons.close,
color: Colors.red,
),
);
},
),
),
),
Row(
children: scorekepper,
),
],
);
}
}
解决方案
setState(() {
scorekepper.add(
Icon(
Icons.close,
color: Colors.red,
),
);
});