首页 文章

在内部Widget内部flutter setState

提问于
浏览
0

是否可以在statefulls内部窗口小部件(主要上下文之外)中使用 setState

我想要实现的是当用户点击它时_1179530_更改 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}'),
            )
        );

      },
    );

  }

1 回答

  • 1

    不,它不会工作,更好的选择将是使用StreamController和广播/订阅模型 .

相关问题