为什么在Flutter中添加定期Timer之后,每秒都会重新初始化所有内容?

如何解决为什么在Flutter中添加定期Timer之后,每秒都会重新初始化所有内容?

我正在尝试构建测验应用程序。当任何用户开始参加测验时,计时器就会启动,并且不知道为什么每秒钟都会重新初始化所有内容。布尔参数“已回答”每秒设置为false。结果,参与者可以多次回答相同的问题,这将导致错误的结果并且不会发生。这是一个片段-

class MathQuizPlay extends StatefulWidget {
  final String quizId;
  MathQuizPlay({Key key,this.quizId}) : super(key: key);

  @override
  _MathQuizPlayState createState() => _MathQuizPlayState(this.quizId);
}

int total = 0;
int _correct = 0;
int _incorrect = 0;
int _notAttempted = 0;
int timer;
String showtimer;

class _MathQuizPlayState extends State<MathQuizPlay> {
  var quizId;
  _MathQuizPlayState(this.quizId);
  QuerySnapshot questionSnapshot;
  bool isLoading = true;

  getQuestionData(String quizId) async {
    return await Firestore.instance
        .collection('math')
        .document(quizId)
        .collection('QNA')
        .getDocuments();
  }

  @override
  void initState() {
    getQuestionData(quizId).then((value) {
      questionSnapshot = value;
      setState(() {
        total = questionSnapshot.documents.length;
        _correct = 0;
        _incorrect = 0;
        _notAttempted = questionSnapshot.documents.length;
        isLoading = false;
        timer = total * 15;
        showtimer = timer.toString();
      });
    });

    starttimer();
    super.initState();
  }

  @override
  void setState(fn) {
    if (mounted) {
      super.setState(fn);
    }
  }

  Questions getQuestionModelFromDatasnapshot(
      DocumentSnapshot questionSnapshot) {
    final Questions questionModel = Questions(
        question: questionSnapshot.data['question'],option1: questionSnapshot.data['option1'],option2: questionSnapshot.data['option2'],option3: questionSnapshot.data['option3'],option4: questionSnapshot.data['option4'],correctOption: questionSnapshot.data['correctOption'],answered: false);

    return questionModel;
  }

  void starttimer() async {
    const onesec = Duration(seconds: 1);
    Timer.periodic(onesec,(Timer t) {
      setState(() {
        if (timer < 1) {
          t.cancel();
          Navigator.pushReplacement(
              context,MaterialPageRoute(
                  builder: (context) => Result(
                        correct: _correct,incorrect: _incorrect,total: total,notattempt: _notAttempted,collection: 'math',quizId: quizId,)));
        } else {
          timer = timer - 1;
        }
        showtimer = timer.toString();
      });
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.teal[300],title: Text("Questions",style: TextStyle(
              color: Colors.white,)),elevation: 5.0,centerTitle: true,),body: isLoading
          ? Container(
              child: Center(child: CircularProgressIndicator()),)
          : SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
                  SizedBox(
                    height: 10,Center(
                      child: Container(
                          height: 60,width: 60,decoration: Boxdecoration(
                            borderRadius: BorderRadius.circular(36),border: Border.all(
                              width: 2.0,color: Colors.red.withOpacity(0.8),child: Center(
                              child: Text(
                            showtimer,style: TextStyle(
                                fontWeight: FontWeight.w500,fontSize: 19.0,color: Colors.red.withOpacity(0.8)),)))),SizedBox(
                    height: 10,Center(child: Text('Tap on the option to select answer')),questionSnapshot.documents == null
                      ? Container(
                          child: Center(
                            child: Text("No Data"),)
                      : ListView.builder(
                          itemCount: questionSnapshot.documents.length,shrinkWrap: true,physics: ClampingScrollPhysics(),itemBuilder: (context,index) {
                            return Padding(
                              padding: const EdgeInsets.symmetric(
                                  vertical: 15.0,horizontal: 25),child: QuizPlayTile(
                                questionModel: getQuestionModelFromDatasnapshot(
                                    questionSnapshot.documents[index]),index: index,);
                          }),SizedBox(
                    height: 30,Center(
                    child: RaisedButton(
                        padding:
                            EdgeInsets.symmetric(vertical: 18,horizontal: 60),color: Colors.teal[300],textColor: Colors.white,shape: RoundedRectangleBorder(
                            borderRadius: new BorderRadius.circular(8.0)),child: Text(
                          'Submit',style: TextStyle(fontSize: 16),elevation: 7.0,onpressed: () {
                          Navigator.pushReplacement(
                              context,MaterialPageRoute(
                                  builder: (context) => Result(
                                        correct: _correct,)));
                        }),SizedBox(
                    height: 50,)
                ],);
  }
}


解决方法

我也无法使用ChangeNotifierProvider解决问题。但是幸运的是我找到了一个可以完全解决我问题的软件包。因此,我使用该程序包而不是定期的Timer来设置时间。这是更新-

import 'package:circular_countdown_timer/circular_countdown_timer.dart';

class PhyQuizPlay extends StatefulWidget {
  final String quizId;
  PhyQuizPlay({Key key,this.quizId}) : super(key: key);

  @override
  _PhyQuizPlayState createState() => _PhyQuizPlayState(this.quizId);
}

int total = 0;
int _correct = 0;
int _incorrect = 0;
int _notAttempted = 0;
int timer;

class _PhyQuizPlayState extends State<PhyQuizPlay> {
  var quizId;
  _PhyQuizPlayState(this.quizId);
  QuerySnapshot questionSnapshot;
  bool isLoading = true;

  getQuestionData(String quizId) async {
    return await Firestore.instance
        .collection('physics')
        .document(quizId)
        .collection('QNA')
        .getDocuments();
  }

  @override
  void initState() {
    getQuestionData(quizId).then((value) {
      questionSnapshot = value;
      setState(() {
        total = questionSnapshot.documents.length;
        _correct = 0;
        _incorrect = 0;
        _notAttempted = total;
        isLoading = false;
        timer = total * 15;
      });
    });
    super.initState();
  }

  Questions getQuestionModelFromDatasnapshot(
      DocumentSnapshot questionSnapshot) {
    final Questions questionModel = Questions(
        question: questionSnapshot.data['question'],option1: questionSnapshot.data['option1'],option2: questionSnapshot.data['option2'],option3: questionSnapshot.data['option3'],option4: questionSnapshot.data['option4'],correctOption: questionSnapshot.data['correctOption'],answered: false);

    return questionModel;
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.teal[300],title: Text("Questions",style: TextStyle(
              color: Colors.white,)),elevation: 5.0,centerTitle: true,),body: isLoading
          ? Container(
              child: Center(child: CircularProgressIndicator()),)
          : SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
                  SizedBox(
                    height: 10,Center(
                      child: CircularCountDownTimer(
                    width: 80,height: 80,duration: timer,fillColor: Colors.red,color: Colors.white38,isReverse: true,onComplete: () {
                      Navigator.pushReplacement(
                          context,MaterialPageRoute(
                              builder: (context) => Result(
                                    correct: _correct,incorrect: _incorrect,total: total,notattempt: _notAttempted,collection: 'physics',quizId: quizId,)));
                    },SizedBox(
                    height: 10,Center(child: Text('Tap on the option to select answer')),questionSnapshot.documents == null
                      ? Center(
                          child: CircularProgressIndicator(),)
                      : ListView.builder(
                          itemCount: questionSnapshot.documents.length,shrinkWrap: true,physics: ClampingScrollPhysics(),itemBuilder: (context,index) {
                            return Padding(
                              padding: const EdgeInsets.symmetric(
                                  vertical: 15.0,horizontal: 25),child: QuizPlayTile(
                                questionModel: getQuestionModelFromDatasnapshot(
                                    questionSnapshot.documents[index]),index: index,);
                          }),SizedBox(
                    height: 30,Center(
                    child: RaisedButton(
                        padding:
                            EdgeInsets.symmetric(vertical: 18,horizontal: 60),color: Colors.teal[300],textColor: Colors.white,shape: RoundedRectangleBorder(
                            borderRadius: new BorderRadius.circular(8.0)),child: Text(
                          'Submit',style: TextStyle(fontSize: 16),elevation: 7.0,onPressed: () {
                          Navigator.pushReplacement(
                              context,MaterialPageRoute(
                                  builder: (context) => Result(
                                        correct: _correct,)));
                        }),SizedBox(
                    height: 50,)
                ],);
  }
} 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?