zee*_*eed 3 dart flutter flutter-getx
\n我有一个 QuestionController 类扩展GetxController
当我使用控件退出页面时,我希望它停止工作(因为它仍在后台运行),并在我返回该页面时重新启动。
\nScoreScreen()我已经尝试过:我在(in )的路线之后添加了这些nextQuestion ():
_isAnswered = false;\n_questionNumber.value = 1; \nRun Code Online (Sandbox Code Playgroud)\n在进入分数页面之前,我重置了值 \xe2\x80\x8b\xe2\x80\x8b。如果您进入乐谱页面,它可能会起作用,但如果您早点回来,它就不会起作用。(上面的问题 num/4 在这里不起作用)。所以这种方式不适合。
\n页面退出时如何停止并重置它?
\n控制器类代码:
\nclass QuestionController extends GetxController\n with SingleGetTickerProviderMixin {\n PageController _pageController;\n\n PageController get pageController => this._pageController;\n\n List<Question> _questions = questions_data\n .map(\n (e) => Question(\n id: e["id"],\n question: e["question"],\n options: e["options"],\n answer: e["answer_index"]),\n )\n .toList();\n\n List<Question> get questions => this._questions;\n\n bool _isAnswered = false;\n\n bool get isAnswered => this._isAnswered;\n\n int _correctAns;\n\n int get correctAns => this._correctAns;\n\n int _selectedAns;\n\n int get selectedAns => this._selectedAns;\n\n RxInt _questionNumber = 1.obs;\n\n RxInt get questionNumber => this._questionNumber;\n\n int _numOfCorrectAns = 0;\n\n int get numOfCorrectAns => this._numOfCorrectAns;\n\n @override\n void onInit() {\n _pageController = PageController();\n super.onInit();\n }\n\n @override\n void onClose() {\n super.onClose();\n _pageController.dispose();\n }\n\n void checkAns(Question question, int selectedIndex) {\n _isAnswered = true;\n _correctAns = question.answer;\n _selectedAns = selectedIndex;\n\n if (_correctAns == _selectedAns) _numOfCorrectAns++;\n\n update();\n\n Future.delayed(Duration(seconds: 2), () {\n nextQuestion();\n });\n }\n\n void nextQuestion() {\n if (_questionNumber.value != _questions.length) {\n _isAnswered = false;\n _pageController.nextPage(\n duration: Duration(milliseconds: 300), curve: Curves.ease);\n } else {\n Get.off(ScoreScreen(correctNum: _numOfCorrectAns)); // GetMaterialApp()\n\n // _isAnswered = false;\n\n _numOfCorrectAns = 0;\n\n //_questionNumber.value = 1;\n\n }\n }\n\n void updateTheQuestionNum(int index) {\n _questionNumber.value = index + 1;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n完整代码如下
\nimport \'package:flutter/material.dart\';\nimport \'dart:async\';\nimport \'package:get/get.dart\'; // get: ^3.25.4\n\n// QuizPage() ===============> 50. line (Question 1/4) 81. line\n// QuestionCard() ==============> 116. line\n// Option() ===================> 163. line\n// QuestionController() ========> 218. line\n// ScoreScreen() ================> 345. line\n\nvoid main() => runApp(new MyApp());\n\nclass MyApp extends StatelessWidget {\n @override\n Widget build(BuildContext context) {\n return GetMaterialApp(\n debugShowCheckedModeBanner: false,\n theme: ThemeData(canvasColor: Colors.blue),\n home: HomeScreen(),\n );\n }\n}\n\nclass HomeScreen extends StatelessWidget {\n @override\n Widget build(BuildContext context) {\n return Scaffold(\n appBar: AppBar(\n title: Text("Home Page"),\n ),\n body: Center(\n child: InkWell(\n onTap: () {\n Navigator.push(\n context, MaterialPageRoute(builder: (context) => QuizPage()));\n },\n child: Container(\n padding: EdgeInsets.all(22),\n color: Colors.green,\n child: Text(\n "Go Quiz Page",\n style: TextStyle(color: Colors.white),\n ),\n ),\n ),\n ),\n );\n }\n}\n\nclass QuizPage extends StatelessWidget {\n const QuizPage({\n Key key,\n }) : super(key: key);\n\n @override\n Widget build(BuildContext context) {\n QuestionController _questionController = Get.put(QuestionController());\n\n return Scaffold(\n appBar: AppBar(\n title: Text("Quiz Page"),\n ),\n body: Stack(\n children: [\n SafeArea(\n child: Column(\n crossAxisAlignment: CrossAxisAlignment.start,\n children: [\n SizedBox(\n height: 16,\n ),\n Padding(\n padding: EdgeInsets.symmetric(horizontal: 16),\n child: Obx(\n () => Center(\n child: RichText(\n text: TextSpan(\n // text,style default adjust here OR:children[TextSpan(1,adjust 1),TextSpan(2,adjust 2),..]\n text:\n "Question ${_questionController._questionNumber.value}",\n style: TextStyle(\n fontSize: 33, color: Colors.white70),\n children: [\n TextSpan(\n text:\n "/${_questionController._questions.length}",\n style: TextStyle(fontSize: 25))\n ])),\n ),\n ),\n ),\n Divider(color: Colors.white70, thickness: 1),\n SizedBox(\n height: 16,\n ),\n Expanded(\n child: PageView.builder(\n physics: NeverScrollableScrollPhysics(),\n controller: _questionController._pageController,\n onPageChanged: _questionController.updateTheQuestionNum,\n itemCount: _questionController.questions.length,\n itemBuilder: (context, index) => QuestionCard(\n question: _questionController.questions[index],\n ),\n ),\n )\n ],\n ),\n )\n ],\n ),\n );\n }\n}\n\nclass QuestionCard extends StatelessWidget {\n final Question question;\n\n const QuestionCard({\n Key key,\n @required this.question,\n }) : super(key: key);\n\n @override\n Widget build(BuildContext context) {\n QuestionController _controller = Get.put(QuestionController());\n\n return Container(\n margin: EdgeInsets.only(left: 16, right: 16, bottom: 16),\n padding: EdgeInsets.all(16),\n decoration: BoxDecoration(\n borderRadius: BorderRadius.circular(25),\n color: Colors.white,\n ),\n child: Column(\n children: [\n Text(\n question.question,\n style: TextStyle(fontSize: 22),\n ),\n SizedBox(\n height: 8,\n ),\n Flexible(\n child: SingleChildScrollView(\n child: Column(\n children: [\n ...List.generate(\n question.options.length,\n (index) => Option(\n text: question.options[index],\n index: index,\n press: () => _controller.checkAns(question, index)))\n ],\n ),\n ),\n )\n ],\n ),\n );\n }\n}\n\nclass Option extends StatelessWidget {\n final String text;\n final int index;\n final VoidCallback press;\n\n const Option({\n Key key,\n @required this.text,\n @required this.index,\n @required this.press,\n }) : super(key: key);\n\n @override\n Widget build(BuildContext context) {\n return GetBuilder<QuestionController>(\n init: QuestionController(),\n builder: (q) {\n Color getRightColor() {\n if (q.isAnswered) {\n if (index == q._correctAns) {\n return Colors.green;\n } else if (index == q.selectedAns &&\n q.selectedAns != q.correctAns) {\n return Colors.red;\n }\n }\n return Colors.blue;\n }\n\n return InkWell(\n onTap: press,\n child: Container(\n //-- Option\n margin: EdgeInsets.only(top: 16),\n padding: EdgeInsets.all(16),\n decoration: BoxDecoration(\n color: getRightColor(),\n borderRadius: BorderRadius.circular(16)),\n child: Row(\n mainAxisAlignment: MainAxisAlignment.spaceBetween,\n children: [\n Text(\n "${index + 1}. $text",\n style: TextStyle(fontSize: 16, color: Colors.white),\n ),\n ],\n ),\n ),\n );\n });\n }\n}\n\nclass QuestionController extends GetxController\n with SingleGetTickerProviderMixin {\n PageController _pageController;\n\n PageController get pageController => this._pageController;\n\n List<Question> _questions = questions_data\n .map(\n (e) => Question(\n id: e["id"],\n question: e["question"],\n options: e["options"],\n answer: e["answer_index"]),\n )\n .toList();\n\n List<Question> get questions => this._questions;\n\n bool _isAnswered = false;\n\n bool get isAnswered => this._isAnswered;\n\n int _correctAns;\n\n int get correctAns => this._correctAns;\n\n int _selectedAns;\n\n int get selectedAns => this._selectedAns;\n\n RxInt _questionNumber = 1.obs;\n\n RxInt get questionNumber => this._questionNumber;\n\n int _numOfCorrectAns = 0;\n\n int get numOfCorrectAns => this._numOfCorrectAns;\n\n @override\n void onInit() {\n _pageController = PageController();\n //_pageController.addListener(() { _questionNumber.value = _pageController.page.round()+1; });\n super.onInit();\n }\n\n @override\n void onClose() {\n super.onClose();\n _pageController.dispose();\n }\n\n void checkAns(Question question, int selectedIndex) {\n _isAnswered = true;\n _correctAns = question.answer;\n _selectedAns = selectedIndex;\n\n if (_correctAns == _selectedAns) _numOfCorrectAns++;\n\n update();\n\n Future.delayed(Duration(seconds: 2), () {\n nextQuestion();\n });\n }\n\n void nextQuestion() {\n if (_questionNumber.value != _questions.length) {\n _isAnswered = false;\n _pageController.nextPage(\n duration: Duration(milliseconds: 300), curve: Curves.ease);\n } else {\n Get.off(ScoreScreen(correctNum: _numOfCorrectAns)); // GetMaterialApp()\n\n // _isAnswered = false;\n\n _numOfCorrectAns = 0;\n\n //_questionNumber.value = 1;\n\n }\n }\n\n void updateTheQuestionNum(int index) {\n _questionNumber.value = index + 1;\n }\n}\n\nclass Question {\n final int id, answer;\n final String question;\n final List<String> options;\n\n Question({\n @required this.id,\n @required this.question,\n @required this.options,\n @required this.answer,\n });\n}\n\nconst List questions_data = [\n {\n "id": 1,\n "question": "Question 1",\n "options": [\'option A\', \'B\', \'C\', \'D\'],\n "answer_index": 3,\n },\n {\n "id": 2,\n "question": "Question 2",\n "options": [\'option A\', \'B\', \'C\', \'D\'],\n "answer_index": 2,\n },\n {\n "id": 3,\n "question": "Question 3",\n "options": [\'option A\', \'B\', \'C\', \'D\'],\n "answer_index": 0,\n },\n {\n "id": 4,\n "question": "Question 4",\n "options": [\'option A\', \'B\', \'C\', \'D\'],\n "answer_index": 0,\n },\n];\n\nclass ScoreScreen extends StatelessWidget {\n final int correctNum;\n\n ScoreScreen({@required this.correctNum});\n\n @override\n Widget build(BuildContext context) {\n QuestionController _qController = Get.put(QuestionController());\n\n return Scaffold(\n body: Stack(\n fit: StackFit.expand,\n children: [\n Column(\n children: [\n Spacer(\n flex: 2,\n ),\n Text(\n "Score",\n style: TextStyle(fontSize: 55, color: Colors.white),\n ),\n Spacer(),\n Text(\n "${correctNum * 10}/${_qController.questions.length * 10}",\n style: TextStyle(fontSize: 33, color: Colors.white),\n ),\n Spacer(\n flex: 2,\n ),\n InkWell(\n onTap: () => Get.back(),\n borderRadius: BorderRadius.circular(16),\n child: Container(\n padding: EdgeInsets.all(16),\n decoration: BoxDecoration(\n borderRadius: BorderRadius.circular(16),\n color: Colors.white24),\n child: Text(\n "Back to Home Page",\n style: TextStyle(color: Colors.white),\n ),\n ),\n ),\n Spacer(),\n ],\n ),\n ],\n ),\n );\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
14331 次 |
| 最近记录: |