intimacy_scale_controller.dart 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import 'package:injectable/injectable.dart';
  2. import 'package:keyboard/base/base_controller.dart';
  3. import 'package:get/get.dart';
  4. import 'package:keyboard/data/repository/keyboard_repository.dart';
  5. import 'package:keyboard/utils/toast_util.dart';
  6. import '../../data/bean/keyboard_info.dart';
  7. import '../../utils/error_handler.dart';
  8. import '../../utils/http_handler.dart';
  9. import '../../widget/flutter_ruler_picker.dart';
  10. import '../store/store_page.dart';
  11. @injectable
  12. class IntimacyScaleController extends BaseController {
  13. final tag = "IntimacyScaleController";
  14. final KeyboardRepository keyboardRepository;
  15. Rxn<KeyboardInfo> get currentKeyboardInfo =>
  16. keyboardRepository.chooseKeyboardInfo;
  17. late Rx<RulerPickerController> rulerPickerController;
  18. List<RulerRange> ranges = const [RulerRange(begin: 0, end: 100, scale: 1)];
  19. // 当前亲密度
  20. RxInt currentCustomIntimacy = 0.obs;
  21. IntimacyScaleController(this.keyboardRepository);
  22. @override
  23. void onInit() {
  24. super.onInit();
  25. currentCustomIntimacy.value = currentKeyboardInfo.value?.intimacy ?? 0;
  26. rulerPickerController =
  27. RulerPickerController(
  28. value: currentKeyboardInfo.value?.intimacy ?? 0,
  29. ).obs;
  30. rulerPickerController.value.addListener(() {
  31. currentCustomIntimacy.value = rulerPickerController.value.value.toInt();
  32. });
  33. }
  34. @override
  35. void onReady() {
  36. super.onReady();
  37. }
  38. @override
  39. void onClose() {
  40. super.onClose();
  41. }
  42. void clickBack() {
  43. Get.back();
  44. }
  45. void onChangeIntimacy(num value) {
  46. currentCustomIntimacy.value = value.toInt();
  47. }
  48. Future<void> clickSaveButton() async {
  49. if (currentKeyboardInfo.value == null &&
  50. currentKeyboardInfo.value?.id == null) {
  51. ToastUtil.show("请选择ta的键盘");
  52. return;
  53. }
  54. try {
  55. await keyboardRepository.updateKeyboardInfo(
  56. keyboardId: currentKeyboardInfo.value!.id!,
  57. intimacy: currentCustomIntimacy.value,
  58. );
  59. await keyboardRepository.getKeyboardHomeInfo();
  60. Get.back();
  61. } catch (error) {
  62. if (error is ServerErrorException && error.code == 1005) {
  63. ToastUtil.show('请开通会员解锁权益~');
  64. StorePage.start();
  65. } else {
  66. ToastUtil.show(error.toString());
  67. }
  68. }
  69. }
  70. }