Last active
March 24, 2024 12:52
-
-
Save arafaysaleem/85d50000f6b8b078c93149fce8df0109 to your computer and use it in GitHub Desktop.
Reusable PaywallGate for Flutter RevenueCat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:hooks_riverpod/hooks_riverpod.dart'; | |
import 'package:lottie/lottie.dart'; | |
import 'package:purchases_ui_flutter/purchases_ui_flutter.dart'; | |
// Config | |
import '../../../config/config.dart'; | |
// Helpers | |
import '../../../helpers/constants/constants.dart'; | |
// Providers | |
import '../providers/subscription_provider.codegen.dart'; | |
// Screens | |
import '../screens/paywall_sheet.dart'; | |
class PaywallGate extends ConsumerWidget { | |
const PaywallGate({ | |
required this.child, | |
this.disableWhile, | |
this.behavior = PaywallBehavior.showPaywall, | |
this.logoWidth = 70, | |
this.logoHeight = 70, | |
this.logoLeft, | |
this.logoTop = -40, | |
this.logoRight = -15, | |
this.logoBottom, | |
this.message, | |
// this.goal, | |
super.key, | |
}); | |
final PaywallBehavior behavior; | |
// final OnboardingGoal? goal; | |
final bool Function()? disableWhile; | |
final String? message; | |
final Widget child; | |
final double? logoWidth; | |
final double? logoHeight; | |
final double? logoTop; | |
final double? logoRight; | |
final double? logoBottom; | |
final double? logoLeft; | |
@override | |
Widget build(BuildContext context, WidgetRef ref) { | |
final isDisabled = disableWhile?.call() ?? false; | |
if (isDisabled) { | |
return child; | |
} | |
final customerSubscription = | |
ref.watch(customerSubscriptionProvider).valueOrNull; | |
final isPremiumActive = customerSubscription?.isActive ?? false; | |
return switch (behavior) { | |
_ when isPremiumActive => child, | |
PaywallBehavior.hidden => const SizedBox.shrink(), | |
PaywallBehavior.showPaywall => InkWell( | |
onTap: () => Config.enableRCRemotePaywall | |
? RevenueCatUI.presentPaywall() | |
: AppUtils.showBottomSheet<void>( | |
context: context, | |
child: PaywallSheet(message: message), | |
), | |
child: Stack( | |
alignment: Alignment.center, | |
clipBehavior: Clip.none, | |
children: [ | |
// Intended CTA | |
AbsorbPointer(child: child), | |
// Premium logo | |
Positioned( | |
width: logoWidth, | |
height: logoHeight, | |
left: logoLeft, | |
top: logoTop, | |
right: logoRight, | |
bottom: logoBottom, | |
child: Lottie.asset( | |
AppAssets.premiumLottie, | |
), | |
), | |
], | |
), | |
) | |
}; | |
} | |
} | |
enum PaywallBehavior { | |
// Hidden for members, visible for non-members | |
hidden, | |
// Visible for all but shows the paywall when tapped for non-members | |
showPaywall, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment