// ignore_for_file: unnecessary_overrides import 'package:dreampad/app/models/models.dart'; import 'package:dreampad/app/routes/app_pages.dart'; import 'package:dreampad/app/shared/shared.dart'; import 'package:flustars_flutter3/flustars_flutter3.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import '../models/occupation_model.dart'; class SelectController extends GetxController { late int gender = 2; late int count = 4; late int page = 0; final step = 0.obs; final question = ''.obs; final guide = ''.obs; final btnTxt = '我要回答'.obs; final allAnswer = false.obs; final showQuestionDialog = false.obs; late List occupations = []; final confirm = false.obs; final confirmTitle = '为你推荐的梦想职业是'.obs; final recommend = false.obs; final showBtn = false.obs; final showOccupationName = false.obs; final selectOccupation = Rx(null); final selectOccupations = RxList>([]); final questionAnswers = RxList>([]); final TextEditingController textController = TextEditingController(); @override void onInit() { super.onInit(); gender = SpUtil.getInt(Constant.gender, defValue: 1)!; if (gender == 1) { initFemaleOccupation(); } else { initMaleOccupation(); } changeOccupation(); questionAnswers.add(QuestionAnswer( index: 1, left: 110.0, top: 147.0, question: '很酷的选择!能告诉我为什么选择这个梦想的职业吗?', display: false, answer: '', guide: '我想成为下一个“爱因斯坦”,他被称为最聪明的人', ).obs); questionAnswers.add(QuestionAnswer( index: 2, left: 720.0, top: 425.0, question: '还有很多选择也非常精彩哦!你还有其他想选择的职业吗?', display: false, answer: '', guide: '我只想做科学家', ).obs); } @override void onReady() { super.onReady(); } @override void onClose() { super.onClose(); } void initMaleOccupation() { occupations.add(Occupation( id: 1, gender: 2, name: '科学院士', enName: 'academician', vImage: 'btn_male_pic_academician', hImage: 'pic_male_academician', enable: true, )); occupations.add(Occupation( id: 2, gender: 2, name: '董事长', enName: 'president', vImage: 'btn_male_pic_president', hImage: 'pic_male_president', )); occupations.add(Occupation( id: 3, gender: 2, name: '歌手', enName: 'singer', vImage: 'btn_male_pic_singer', hImage: 'pic_male_singer', selected: false, )); occupations.add(Occupation( id: 4, gender: 2, name: '运动员', enName: 'athletes', vImage: 'btn_male_pic_athletes', hImage: 'pic_male_athletes', selected: false, )); occupations.add(Occupation( id: 5, gender: 2, name: '画家', enName: 'artist', vImage: 'btn_male_pic_artist', hImage: 'pic_male_artist', selected: false, enable: true, )); occupations.add(Occupation( id: 6, gender: 2, name: '军人', enName: 'soldier', vImage: 'btn_male_pic_soldier', hImage: 'pic_male_soldier', selected: false, )); occupations.add(Occupation( id: 7, gender: 2, name: '医生', enName: 'doctor', vImage: 'btn_male_pic_doctor', hImage: 'pic_male_doctor', selected: false, )); occupations.add(Occupation( id: 8, gender: 2, name: '饲养员', enName: 'breeder', vImage: 'btn_male_pic_breeder', hImage: 'pic_male_breeder', selected: false, )); } void initFemaleOccupation() { occupations.add(Occupation( id: 1, gender: 1, name: '科学院士', enName: 'academician', vImage: 'btn_female_pic_academician', hImage: 'pic_female_academician', enable: true, )); occupations.add(Occupation( id: 2, gender: 1, name: '董事长', enName: 'president', vImage: 'btn_female_pic_president', hImage: 'pic_female_president', )); occupations.add(Occupation( id: 3, gender: 1, name: '歌手', enName: 'singer', vImage: 'btn_female_pic_singer', hImage: 'pic_female_singer', selected: false, )); occupations.add(Occupation( id: 4, gender: 1, name: '运动员', enName: 'athletes', vImage: 'btn_female_pic_athletes', hImage: 'pic_female_athletes', selected: false, )); occupations.add(Occupation( id: 5, gender: 1, name: '画家', enName: 'artist', vImage: 'btn_female_pic_artist', hImage: 'pic_female_artist', selected: false, enable: true, )); occupations.add(Occupation( id: 6, gender: 1, name: '军人', enName: 'soldier', vImage: 'btn_female_pic_soldier', hImage: 'pic_female_soldier', selected: false, )); occupations.add(Occupation( id: 7, gender: 1, name: '医生', enName: 'doctor', vImage: 'btn_female_pic_doctor', hImage: 'pic_female_doctor', selected: false, )); occupations.add(Occupation( id: 8, gender: 1, name: '饲养员', enName: 'breeder', vImage: 'btn_female_pic_breeder', hImage: 'pic_female_breeder', selected: false, )); } Future change() async { page++; if (page * count >= occupations.length) { page = 0; } changeOccupation(); } void changeOccupation() { var mOccupations = occupations .where((t) => t.gender == gender) .skip(page * count) .take(count) .toList(); selectOccupations.clear(); for (var occupation in mOccupations) { selectOccupations.add(occupation.obs); } } Future confimSelected() async { showBtn.value = false; allAnswer.value = false; showOccupationName.value = false; confirm.value = true; recommend.value = false; confirmTitle.value = '你的梦想职业是'; showOccupationName.value = true; await Future.delayed(const Duration(milliseconds: 1000)); showOccupationName.value = false; } Future selectedOccupation(Occupation occupation) async { selectOccupation.value = occupation; await setStep(); for (var occupation in selectOccupations) { occupation.update((val) { val!.selected = false; }); } } Future recommendOccupation(int id) async { var occupation = selectOccupations.firstWhere((t) => t.value.id == id); selectOccupation.value = occupation.value; confirm.value = true; recommend.value = true; for (var occupation in selectOccupations) { occupation.update((val) { val!.selected = false; }); } } 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; hasQuestion = true; allAnswer.value = false; break; } } if (!hasQuestion) { showQuestionDialog.value = false; allAnswer.value = true; } } 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 = ''; if (allAnswer.value) { await Future.delayed(const Duration(milliseconds: 1000)); for (var questionAnswer in questionAnswers) { questionAnswer.update((val) { val!.display = false; }); } showBtn.value = true; showOccupationName.value = true; } } Future reflect() async { confirm.value = false; for (var questionAnswer in questionAnswers) { questionAnswer.update((val) { val!.display = false; }); } } Future openDream() async { SpUtil.putString(Constant.occupationName, selectOccupation.value!.name!); SpUtil.putInt(Constant.occupationId, selectOccupation.value!.id!); await Get.offAllNamed(Routes.HOME); } }