flutter 测验应用程序,随机答案输出出现问题

ua4mk5z4  于 2023-02-25  发布在  Flutter
关注(0)|答案(1)|浏览(136)

我正在尝试制作我的第一个测验应用程序。该应用程序以随机顺序显示第一个问题和答案。第一个问题工作正常。当您单击“下一步”按钮时,第二个问题出现,但答案不变。可能是Listanswers = [];没有你的帮助我无法决定。

import 'package:flutter/material.dart';
import 'package:australia/qlist';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  List<Color> colorsList = [
    Colors.white,
    Colors.white,
    Colors.white,
    Colors.white
  ];

  List<String> answers = [];

  @override
  void initState() {
    answers.addAll([
      QuestionsList.shared.getCurrentQuestion().correctAnswer,
      QuestionsList.shared.getCurrentQuestion().dis1,
      QuestionsList.shared.getCurrentQuestion().dis2,
      QuestionsList.shared.getCurrentQuestion().dis3,
    ]);
    answers.shuffle();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Column(children: [
          Center(
              child: Text(QuestionsList.shared.getCurrentQuestion().question)),
          Card(
            color: colorsList[0],
            child: InkWell(
              child: Text(answers[0]),
              onTap: () {
                if (answers[0] ==
                    QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                  colorsList[0] = Colors.green;
                  setState(() {});
                }
              },
            ),
          ),
          Card(
            color: colorsList[1],
            child: InkWell(
              child: Text(answers[1]),
              onTap: () {
                if (answers[1] ==
                    QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                  colorsList[1] = Colors.green;
                  setState(() {});
                }
              },
            ),
          ),
          Card(
            color: colorsList[2],
            child: InkWell(
              child: Text(answers[2]),
              onTap: () {
                if (answers[2] ==
                    QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                  colorsList[2] = Colors.green;
                  setState(() {});
                }
              },
            ),
          ),
          Card(
            color: colorsList[3],
            child: InkWell(
              child: Text(answers[3]),
              onTap: () {
                if (answers[3] ==
                    QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                  colorsList[3] = Colors.green;
                  setState(() {});
                }
              },
            ),
          ),
          ElevatedButton(
              onPressed: () {
                QuestionsList.shared.nextQuestion();
                setState(() {});
              },
              child: Text('NEXT'))
        ]));
  }
}
h43kikqp

h43kikqp1#

检查我的onTap()代码的下一次点击,我认为该代码有助于您实现您的功能.

import 'package:flutter/material.dart';
import 'package:australia/qlist';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  List<Color> colorsList = [
    Colors.white,
    Colors.white,
    Colors.white,
    Colors.white
  ];

  List<String> answers = [];

  @override
  void initState() {
    answers.addAll([
      QuestionsList.shared.getCurrentQuestion().correctAnswer,
      QuestionsList.shared.getCurrentQuestion().dis1,
      QuestionsList.shared.getCurrentQuestion().dis2,
      QuestionsList.shared.getCurrentQuestion().dis3,
    ]);
    answers.shuffle();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Column(children: [
          Center(
              child: Text(QuestionsList.shared.getCurrentQuestion().question)),
          Card(
            color: colorsList[0],
            child: InkWell(
              child: Text(answers[0]),
              onTap: () {
                if (answers[0] ==
                    QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                  colorsList[0] = Colors.green;
                  setState(() {});
                }
              },
            ),
          ),
          Card(
            color: colorsList[1],
            child: InkWell(
              child: Text(answers[1]),
              onTap: () {
                if (answers[1] ==
                    QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                  colorsList[1] = Colors.green;
                  setState(() {});
                }
              },
            ),
          ),
          Card(
            color: colorsList[2],
            child: InkWell(
              child: Text(answers[2]),
              onTap: () {
                if (answers[2] ==
                    QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                  colorsList[2] = Colors.green;
                  setState(() {});
                }
              },
            ),
          ),
          Card(
            color: colorsList[3],
            child: InkWell(
              child: Text(answers[3]),
              onTap: () {
                if (answers[3] ==
                    QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                  colorsList[3] = Colors.green;
                  setState(() {});
                }
              },
            ),
          ),
          ElevatedButton(
              onPressed: () {
                QuestionsList.shared.nextQuestion();
                answers.clear();
                answers.addAll([
                    QuestionsList.shared.getCurrentQuestion().correctAnswer,
                    QuestionsList.shared.getCurrentQuestion().dis1,
                    QuestionsList.shared.getCurrentQuestion().dis2,
                    QuestionsList.shared.getCurrentQuestion().dis3,
                ]);
                answers.shuffle();
                setState(() {});
              },
              child: Text('NEXT'))
        ]));
  }
}

相关问题