dreampad/lib/app/modules/question/controllers/question_controller.dart

114 lines
3.1 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ignore_for_file: unnecessary_overrides
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../models/question_answer_model.dart';
class QuestionController extends GetxController {
final step = 0.obs;
final ipLeft = 149.obs;
final ipTop = 42.obs;
final question = ''.obs;
final guide = ''.obs;
final btnTxt = ''.obs;
final allAnswer = false.obs;
final showQuestionDialog = false.obs;
final questionAnswers = RxList<Rx<QuestionAnswer>>([]);
final TextEditingController textController = TextEditingController();
final int second = 5;
StreamSubscription<dynamic>? subscription;
@override
void onInit() {
super.onInit();
btnTxt.value = '开始';
question.value = '没想好梦想职业吗?没关系,我来问你几个关于你自己的问题,我们一起想想吧!';
questionAnswers.add(QuestionAnswer(
index: 1,
left: 37.0,
top: 126.0,
question: '你平常最喜欢的事情是什么?做什么事最能让你开心?',
display: false,
answer: '',
guide: '我平常喜欢滑板,因为喜欢速度和挑战',
).obs);
questionAnswers.add(QuestionAnswer(
index: 2,
left: 802.0,
top: 222.0,
question: '你的偶像是谁你最喜欢TA的哪些地方',
display: false,
answer: '',
guide: '我的偶像时IU',
).obs);
questionAnswers.add(QuestionAnswer(
index: 3,
left: 186.0,
top: 349.0,
question: '你想变成电影、动画、故事中的哪个角色?原因是什么?',
display: false,
answer: '',
guide: '我想变成某某电影里的某某,因为电影',
).obs);
}
@override
void onReady() {
super.onReady();
}
@override
void onClose() {
textController.dispose();
subscription?.cancel();
super.onClose();
}
Future setStep() async {
btnTxt.value = '我要回答';
bool hasQuestion = false;
for (var questionAnswer in questionAnswers) {
if (!questionAnswer.value.display!) {
if (questionAnswer.value.index != step.value) {
showQuestionDialog.value = false;
} else {
showQuestionDialog.value = true;
}
step.value = questionAnswer.value.index!;
question.value = questionAnswer.value.question!;
guide.value = questionAnswer.value.guide;
allAnswer.value = false;
hasQuestion = true;
break;
}
}
if (!hasQuestion) {
ipLeft.value = 409;
ipTop.value = 42;
showQuestionDialog.value = false;
allAnswer.value = true;
subscription = Stream.periodic(const Duration(seconds: 1), (int i) => i)
.take(second)
.listen((int i) {
if (i >= second - 1) {
Get.back(result: {'id': '2'});
}
});
}
}
Future updateAnswer(String answer) async {
var questionAnswer =
questionAnswers.firstWhere((t) => t.value.index == step.value);
questionAnswer.update((val) {
val!.answer = answer;
val.display = true;
});
await setStep();
textController.text = '';
}
}