purchase_controller.dart
1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class PurchasePlan {
const PurchasePlan({
required this.title,
required this.subtitle,
required this.price,
required this.badge,
});
final String title;
final String subtitle;
final String price;
final String badge;
}
class PurchaseController extends GetxController {
final scrollController = ScrollController();
final planScrollController = ScrollController();
final plans = <PurchasePlan>[].obs;
final selectedIndex = 0.obs;
final isScrolled = false.obs;
static const fallbackPlans = [
PurchasePlan(
title: '终身会员',
subtitle: '终身使用,免费更新',
price: '118',
badge: '特别优惠',
),
PurchasePlan(
title: '年度会员',
subtitle: '每月仅¥6.5',
price: '58',
badge: '20%优惠',
),
];
@override
void onInit() {
super.onInit();
plans.assignAll(fallbackPlans);
scrollController.addListener(_handleScroll);
}
@override
void onClose() {
planScrollController.dispose();
scrollController
..removeListener(_handleScroll)
..dispose();
super.onClose();
}
void _handleScroll() {
final next = scrollController.hasClients && scrollController.offset > 250;
if (next != isScrolled.value) {
isScrolled.value = next;
}
}
void selectPlan(int index) {
if (index == selectedIndex.value) return;
selectedIndex.value = index;
if (!planScrollController.hasClients) return;
planScrollController.animateTo(
index * 232,
duration: const Duration(milliseconds: 220),
curve: Curves.easeOutCubic,
);
}
void restorePurchase() {}
void unlock() {}
}