You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter问答APP随机答案输出异常问题求助

问题排查与修复

你的核心问题是answers列表仅在组件初始化阶段(initState)填充了一次数据,而initState只会在组件创建时执行一次。当你点击Next切换到下一个问题时,没有重新获取新问题的答案并更新这个列表,所以界面显示的始终是第一个问题的选项。

修复方案

  1. 将加载并洗牌答案的逻辑抽成独立方法,方便重复调用
  2. 在切换问题后重新调用该方法更新答案
  3. 重置选项的颜色列表,避免上一题的选中状态残留

修改后的完整代码

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() {
    super.initState();
    // 初始化加载第一个问题的答案
    _loadCurrentQuestionAnswers();
  }

  // 抽离加载答案的方法
  void _loadCurrentQuestionAnswers() {
    // 清空旧的答案,避免数据叠加
    answers.clear();
    final currentQuestion = QuestionsList.shared.getCurrentQuestion();
    answers.addAll([
      currentQuestion.correctAnswer,
      currentQuestion.dis1,
      currentQuestion.dis2,
      currentQuestion.dis3,
    ]);
    answers.shuffle();
    // 重置选项颜色
    colorsList = List.generate(4, (_) => Colors.white);
  }

  @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) {
                  setState(() {
                    colorsList[0] = Colors.green;
                  });
                }
              },
            ),
          ),
          Card(
            color: colorsList[1],
            child: InkWell(
              child: Text(answers[1]),
              onTap: () {
                if (answers[1] ==
                    QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                  setState(() {
                    colorsList[1] = Colors.green;
                  });
                }
              },
            ),
          ),
          Card(
            color: colorsList[2],
            child: InkWell(
              child: Text(answers[2]),
              onTap: () {
                if (answers[2] ==
                    QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                  setState(() {
                    colorsList[2] = Colors.green;
                  });
                }
              },
            ),
          ),
          Card(
            color: colorsList[3],
            child: InkWell(
              child: Text(answers[3]),
              onTap: () {
                if (answers[3] ==
                    QuestionsList.shared.getCurrentQuestion().correctAnswer) {
                  setState(() {
                    colorsList[3] = Colors.green;
                  });
                }
              },
            ),
          ),
          ElevatedButton(
              onPressed: () {
                QuestionsList.shared.nextQuestion();
                setState(() {
                  // 切换问题后重新加载答案
                  _loadCurrentQuestionAnswers();
                });
              },
              child: Text('NEXT'))
        ]));
  }
}

关键修改点说明

  • 新增_loadCurrentQuestionAnswers()方法,负责清空旧答案、加载当前问题的所有选项并洗牌,同时重置颜色列表
  • 在initState中调用该方法初始化第一个问题的答案
  • 在Next按钮的点击事件中,切换问题后调用_loadCurrentQuestionAnswers()更新答案,并通过setState()触发界面重建
  • 将颜色修改的逻辑移到setState()内部,确保状态更新能正确触发界面刷新

内容的提问来源于stack exchange,提问作者skv500

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 17:39:24