【发布时间】:2022-01-25 11:50:03
【问题描述】:
我在下面创建了一个函数
nextQuestion() {
setState(() {
questionIndex++;
});
print('pressed');
}
然后我创建了下面的类,并在上面的方法中传递了一个 onPressed 属性的构造函数
class Answer extends StatelessWidget {
final String answer;
final VoidCallback? selectHandler;
// ignore: use_key_in_widget_constructors
const Answer({required this.answer, this.selectHandler});
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: selectHandler,
child: SizedBox(
width: double.infinity,
child: Center(
child: Text(
answer,
),
),
),
);
}
}
然后我使用类的构造函数并传入函数来更新屏幕上的文本,该文本位于以 questionIndex 作为其索引的列表中
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Question(question: questions[questionIndex]),
Answer(selectHandler: nextQuestion(), answer: 'Yellow'),
Answer(selectHandler: nextQuestion(), answer: 'Yellow'),
Answer(selectHandler: nextQuestion(), answer: 'Yellow'),
],
),
这不起作用/按下按钮后没有更新屏幕上的文本
【问题讨论】:
-
像
()=> selectHandler一样调用你的onPressed -
请它仍然没有更新问题