purchase_controller.dart 1.71 KB
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() {}
}