dreampad/lib/app/modules/select/controllers/select_controller.dart

336 lines
8.6 KiB
Dart
Raw Normal View History

2023-11-28 10:44:58 +08:00
// ignore_for_file: unnecessary_overrides
2023-12-02 00:45:45 +08:00
import 'package:dreampad/app/models/models.dart';
2023-11-28 10:44:58 +08:00
import 'package:dreampad/app/routes/app_pages.dart';
import 'package:dreampad/app/shared/shared.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
2023-12-02 00:45:45 +08:00
import 'package:flutter/material.dart';
2023-11-28 10:44:58 +08:00
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;
2023-12-02 00:45:45 +08:00
final step = 0.obs;
final question = ''.obs;
final guide = ''.obs;
final btnTxt = '我要回答'.obs;
final allAnswer = false.obs;
final showQuestionDialog = false.obs;
2023-11-28 10:44:58 +08:00
late List<Occupation> occupations = [];
final confirm = false.obs;
final confirmTitle = '为你推荐的梦想职业是'.obs;
2023-12-02 00:45:45 +08:00
final recommend = false.obs;
final showBtn = false.obs;
final showOccupationName = false.obs;
2023-11-28 10:44:58 +08:00
final selectOccupation = Rx<Occupation?>(null);
final selectOccupations = RxList<Rx<Occupation>>([]);
2023-12-02 00:45:45 +08:00
final questionAnswers = RxList<Rx<QuestionAnswer>>([]);
final TextEditingController textController = TextEditingController();
2023-11-28 10:44:58 +08:00
@override
void onInit() {
super.onInit();
gender = SpUtil.getInt(Constant.gender, defValue: 1)!;
if (gender == 1) {
initFemaleOccupation();
} else {
initMaleOccupation();
}
changeOccupation();
2023-12-02 00:45:45 +08:00
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);
2023-11-28 10:44:58 +08:00
}
@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(
2023-11-29 20:37:45 +08:00
id: 2,
2023-11-28 10:44:58 +08:00
gender: 2,
name: '董事长',
enName: 'president',
vImage: 'btn_male_pic_president',
hImage: 'pic_male_president',
));
occupations.add(Occupation(
2023-11-29 20:37:45 +08:00
id: 3,
2023-11-28 10:44:58 +08:00
gender: 2,
name: '歌手',
enName: 'singer',
vImage: 'btn_male_pic_singer',
hImage: 'pic_male_singer',
selected: false,
));
2023-11-29 20:37:45 +08:00
occupations.add(Occupation(
id: 4,
gender: 2,
2023-11-30 10:16:09 +08:00
name: '运动员',
enName: 'athletes',
vImage: 'btn_male_pic_athletes',
hImage: 'pic_male_athletes',
selected: false,
));
occupations.add(Occupation(
id: 5,
gender: 2,
2023-11-29 20:37:45 +08:00
name: '画家',
enName: 'artist',
vImage: 'btn_male_pic_artist',
hImage: 'pic_male_artist',
2023-11-30 14:58:48 +08:00
selected: false,
2023-11-29 20:37:45 +08:00
enable: true,
));
2023-11-28 10:44:58 +08:00
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',
2023-11-30 17:12:13 +08:00
enable: true,
2023-11-28 10:44:58 +08:00
));
occupations.add(Occupation(
2023-11-29 20:37:45 +08:00
id: 2,
2023-11-28 10:44:58 +08:00
gender: 1,
name: '董事长',
enName: 'president',
vImage: 'btn_female_pic_president',
hImage: 'pic_female_president',
));
occupations.add(Occupation(
2023-11-29 20:37:45 +08:00
id: 3,
2023-11-28 10:44:58 +08:00
gender: 1,
name: '歌手',
enName: 'singer',
vImage: 'btn_female_pic_singer',
hImage: 'pic_female_singer',
selected: false,
));
occupations.add(Occupation(
2023-11-29 20:37:45 +08:00
id: 4,
2023-11-28 10:44:58 +08:00
gender: 1,
name: '运动员',
enName: 'athletes',
vImage: 'btn_female_pic_athletes',
hImage: 'pic_female_athletes',
selected: false,
));
2023-11-29 20:37:45 +08:00
occupations.add(Occupation(
id: 5,
gender: 1,
name: '画家',
enName: 'artist',
vImage: 'btn_female_pic_artist',
hImage: 'pic_female_artist',
2023-11-30 14:58:48 +08:00
selected: false,
2023-11-29 20:37:45 +08:00
enable: true,
));
2023-11-28 10:44:58 +08:00
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);
}
}
2023-12-02 00:45:45 +08:00
Future confimSelected() async {
2023-12-02 12:28:56 +08:00
showBtn.value = false;
allAnswer.value = false;
showOccupationName.value = false;
2023-12-02 00:45:45 +08:00
confirm.value = true;
recommend.value = false;
confirmTitle.value = '你的梦想职业是';
showOccupationName.value = true;
await Future.delayed(const Duration(milliseconds: 1000));
showOccupationName.value = false;
}
2023-11-28 10:44:58 +08:00
Future selectedOccupation(Occupation occupation) async {
selectOccupation.value = occupation;
2023-12-02 00:45:45 +08:00
await setStep();
2023-11-28 10:44:58 +08:00
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;
2023-11-30 17:12:13 +08:00
confirm.value = true;
2023-12-02 00:45:45 +08:00
recommend.value = true;
2023-11-28 10:44:58 +08:00
for (var occupation in selectOccupations) {
occupation.update((val) {
val!.selected = false;
});
}
}
2023-12-02 00:45:45 +08:00
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;
});
}
}
2023-11-28 10:44:58 +08:00
Future openDream() async {
SpUtil.putString(Constant.occupationName, selectOccupation.value!.name!);
SpUtil.putInt(Constant.occupationId, selectOccupation.value!.id!);
await Get.offAllNamed(Routes.HOME);
}
}