Flutter -按钮无响应

m528fe3b  于 2023-01-18  发布在  Flutter
关注(0)|答案(1)|浏览(246)

我正在学习的博士安吉拉余Flutter课程Udemy,目前正在做一个测验应用程序。决定挑战自己,使按钮单独类。一切似乎工作正常,但测试时,错误发生。
Error message
我的密码,主星

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,
        )
      ],
    );
  }
}

智力测验_大脑.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;
  }
}

未在Web上找到有关此内容的任何信息。

tzdcorbm

tzdcorbm1#

在QuestionButton类中,func参数被定义为VoidCallback类型,这意味着它应该是一个不接受参数并返回void的函数。
在button元素的onPressed属性中,使用了widget.func,这意味着它将在构造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应如下所示

onPressed: widget.func;

在小部件调用中,您必须编写如下回调

QuestionButton(
            label: 'False', colour: Colors.red, func :  () {
                          checkAnswer(false); }),

相关问题