dart - Flutter setState 在内部 Widget 中
问题描述
是否可以setState
在 statefulls 内部小部件中使用(在主上下文之外)?
我想要实现的是在用户点击它时进行Button
更改。color
它在里面,ListView.builder
因为有多个按钮,但我希望它只发生在_buildAnsers
小部件中
Widget _buildAnswers(int questionIndex) {
//I need this to be inside this widget
Color _buttonColor = Colors.red;
return new ListView.builder(
itemCount: 10,
physics: ClampingScrollPhysics(),
shrinkWrap: true,
itemBuilder: (context, answerIndex) {
return Padding(
child: new RaisedButton(
onPressed: () {
setState(() {
_buttonColor = Colors.green; //this doesn't work?
});
},
color: _buttonColor,
splashColor: Color(0xFFde1f28),
disabledColor: Color(0xFFc54b51),
child: new Text('Container ${answerIndex}'),
)
);
},
);
}
解决方案
不,它不起作用,更好的选择是使用StreamController和广播/订阅模型。
推荐阅读
- html - 在svg中添加谷歌字体Roboto不显示所需的字体
- mongoose - TypeError:在使用 Jests 和 Mongoose 测试 NestJS API 时,updatedService.save 不是一个函数
- flutter - Flutter Dropdown 对齐所选项目和提示文本
- vba - 基于另一个值检索 Microsoft Access 表单中的最新值
- c# - .Net WebApi 忽略查询参数
- c++ - 如何让Make在编译前生成头文件?
- python-3.x - 像熊猫一样思考:如何按顺序更新所有行(行使用前行的信息)
- c# - 将自定义 JsonConverter 添加到 Web API 会影响传递给自定义验证属性的字符串值
- docker - Docker:需要在 symfony 项目中持久化文件夹
- ios - .weekday 返回错误日期的最简洁修复