是否可以在有状态的内部小部件(主上下文之外)中使用 setState
?
我想要实现的是当用户点击它时让 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和广播/订阅模式。
关于dart - 内部 Widget 中的 Flutter setState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51806728/