109 lines
2.9 KiB
Dart
109 lines
2.9 KiB
Dart
// 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 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: '',
|
||
).obs);
|
||
questionAnswers.add(QuestionAnswer(
|
||
index: 2,
|
||
left: 802.0,
|
||
top: 222.0,
|
||
question: '你的偶像是谁?你最喜欢TA的哪些地方?',
|
||
display: false,
|
||
answer: '',
|
||
).obs);
|
||
questionAnswers.add(QuestionAnswer(
|
||
index: 3,
|
||
left: 186.0,
|
||
top: 349.0,
|
||
question: '你想变成电影、动画、故事中的哪个角色?原因是什么?',
|
||
display: false,
|
||
answer: '',
|
||
).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!;
|
||
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 = '';
|
||
}
|
||
}
|