扑动-按钮没有响应



我正在学习Angela Yu博士在Udemy上的Flutter课程,目前正在制作一个Quizz应用程序。决定挑战自己,为按钮制作一个单独的课程。似乎一切正常,但测试时,出现错误。

错误消息

我的代码main.dart:

import 'package:flutter/material.dart';
import 'package:quizler/quiz_brain.dart';
import 'package:rflutter_alert/rflutter_alert.dart';
void main() => runApp(const Quizzler());
class Quizzler extends StatelessWidget {
const Quizzler({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.grey.shade900,
body: const SafeArea(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: QuizPage(),
),
),
),
);
}
}
class QuestionButton extends StatefulWidget {
@override
State<QuestionButton> createState() => _questionButtonState();
final String label;
final MaterialColor colour;
final void func;
const QuestionButton(
{required this.label, required this.colour, required this.func});
}
class _questionButtonState extends State<QuestionButton> {
@override
Widget build(BuildContext context) {
return Expanded(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: TextButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(widget.colour)),
child: Text(
widget.label,
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
onPressed: () {
setState() {
widget.func;
}
;
},
),
),
);
}
}
class QuizPage extends StatefulWidget {
const QuizPage({super.key});
@override
_QuizPageState createState() => _QuizPageState();
}
class _QuizPageState extends State<QuizPage> {
List<Widget> scoreKeeper = [];
bool isLast() {
if (scoreKeeper.length == 13) {
return true;
} else {
return false;
}
}
int score = 0;
String result(n) {
if (n == 0) {
return "are ya dumb??";
} else if (0 < n && n < 4) {
return "go to school";
} else {
return "testtest";
}
}
void checkAnswer(bool userPickedAnswer) {
bool correctAnswer = QuizBrain().getQuestionAnswer();
if (userPickedAnswer == correctAnswer) {
scoreKeeper.add(Icon(
Icons.check,
color: Colors.green,
));
score++;
} else {
scoreKeeper.add(Icon(
Icons.close,
color: Colors.red,
));
}
setState(() {
QuizBrain().nextQuestion();
});
if (isLast()) {
Alert(
context: context,
title: "END!",
desc: "You've finished this quiz! Ur score is - $score")
.show();
Alert(context: context, title: "END!", desc: result(score)).show();
scoreKeeper = [];
QuizBrain().startOver();
score = 0;
}
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 5,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Center(
child: Text(
QuizBrain().getQuestionText(),
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
QuestionButton(
label: 'True', colour: Colors.green, func: checkAnswer(true)),
QuestionButton(
label: 'False', colour: Colors.red, func: checkAnswer(false)),
Row(
children: scoreKeeper,
)
],
);
}
}

quiz_brain.dart:

int _questionNumber = 0;
bool finished = false;
class QuizBrain {
List<Question> _questionBank = [
Question('Some cats are actually allergic to humans', true),
Question('You can lead a cow down stairs but not up stairs.', false),
Question('Approximately one quarter of human bones are in the feet.', true),
Question('A slug's blood is green.', true),
Question('Buzz Aldrin's mother's maiden name was "Moon".', true),
Question('It is illegal to pee in the Ocean in Portugal.', true),
Question(
'No piece of square dry paper can be folded in half more than 7 times.',
false),
Question(
'In London, UK, if you happen to die in the House of Parliament, you are technically entitled to a state funeral, because the building is considered too sacred a place.',
true),
Question(
'The loudest sound produced by any animal is 188 decibels. That animal is the African Elephant.',
false),
Question(
'The total surface area of two human lungs is approximately 70 square metres.',
true),
Question('Google was originally called "Backrub".', true),
Question(
'Chocolate affects a dog's heart and nervous system; a few ounces are enough to kill a small dog.',
true),
Question(
'In West Virginia, USA, if you accidentally hit an animal with your car, you are free to take it home to eat.',
true),
];
void nextQuestion() {
if (_questionNumber < _questionBank.length - 1) {
_questionNumber++;
} else {
finished = true;
}
}
String getQuestionText() {
return _questionBank[_questionNumber].questionText;
}
bool getQuestionAnswer() {
return _questionBank[_questionNumber].questionAnswer;
}
bool isLast() {
if (_questionNumber == _questionBank.length) {
return true;
} else {
return false;
}
}
void startOver() {
_questionNumber = 0;
}
}

question.dart:

class Question {
late String questionText;
late bool questionAnswer;
Question(String q, bool a) {
questionText = q;
questionAnswer = a;
}
}

在网上找不到任何关于这个的信息。

在QuestionButton类中,func形参被定义为VoidCallback类型,这意味着它应该是一个不接受参数并返回void的函数。

在button元素的onPressed属性中,小部件。这意味着它将调用在构造QuestionButton小部件时作为参数传递的func函数。

当您在代码中创建QuestionButton小部件时,您可以像在示例中所示的那样传递回调函数。当按钮被按下时,checkAnswer(false)函数将被调用。

class QuestionButton extends StatefulWidget {
@override
State<QuestionButton> createState() => _questionButtonState();
final String label;
final MaterialColor colour;
final VoidCallback? func;
const QuestionButton(
{required this.label, required this.colour, required this.func});
}

OnPress of button应该是这样的

onPressed: widget.func;

在Widget调用中,你必须像

那样写回调
QuestionButton(
label: 'False', colour: Colors.red, func :  () {
checkAnswer(false); }),

最新更新