Retrieving and Presenting a Paywall Yourself
Embed a paywall in your own Flutter layout with PaywallView, or control what happens around a presented paywall with a PaywallPresentationHandler.
Flutter SDK 2.9.0 and later has a PaywallView widget. It shows a placement's paywall inside your own widget tree instead of over the app. Flutter has no getPaywall.
import 'package:flutter/material.dart';
import 'package:superwallkit_flutter/superwallkit_flutter.dart';
class UpgradeSection extends StatefulWidget {
const UpgradeSection({super.key});
@override
State<UpgradeSection> createState() => _UpgradeSectionState();
}
class _UpgradeSectionState extends State<UpgradeSection> {
bool _showPaywall = true;
@override
Widget build(BuildContext context) {
if (!_showPaywall) return const SizedBox.shrink();
return SizedBox(
height: 500,
child: PaywallView(
placement: 'campaign_trigger',
loadingBuilder: (context) =>
const Center(child: CircularProgressIndicator()),
onFinished: (paywallInfo, result, shouldDismiss) {
if (result is PurchasedPaywallResult ||
result is RestoredPaywallResult) {
// Unlock the feature.
}
if (shouldDismiss) {
setState(() => _showPaywall = false);
}
},
),
);
}
}- Call
Superwall.configure()before you build aPaywallView. - Give it a bounded size. It fills the space its parent gives it.
- There's no
featurecallback. The result arrives inonFinished, where you decide whether to unlock the feature. WhenshouldDismissistrue, the paywall has left the view, so remove thePaywallView. onSkipandonErrortell you when no paywall is shown.skippedBuilderanderrorBuilderchoose what shows instead.- Don't show the same paywall in two
PaywallViews at once. Give each one on screen a placement that leads to a different paywall. - It works on iOS and Android only.
To let Superwall present the paywall and control what happens around it, pass a PaywallPresentationHandler to registerPlacement. Its callbacks tell you when the paywall is presented, skipped, dismissed or fails. The following code mimics register with a handler:
Future<void> presentPaywall() async {
final handler = PaywallPresentationHandler();
// 1
handler.onPresent((paywallInfo) {
print('Presented ${paywallInfo.name}');
});
// 2
handler.onSkip((reason) {
switch (reason) {
case PaywallSkippedReasonHoldout():
case PaywallSkippedReasonNoAudienceMatch():
case PaywallSkippedReasonPlacementNotFound():
case PaywallSkippedReasonUserIsSubscribed():
break;
}
});
// 3
handler.onError((error) {
print(error);
});
// 4
handler.onDismiss((paywallInfo, result) {
switch (result) {
case PurchasedPaywallResult():
case RestoredPaywallResult():
launchFeature();
case DeclinedPaywallResult():
if (paywallInfo.closeReason != PaywallCloseReason.forNextPaywall &&
paywallInfo.featureGatingBehavior == FeatureGatingBehavior.nonGated) {
launchFeature();
}
}
});
await Superwall.shared.registerPlacement(
'campaign_trigger',
handler: handler,
);
}
void launchFeature() {
// Insert code to launch a feature that's behind your paywall.
}This does the following:
- Runs when Superwall presents the paywall.
- Handles the cases where the paywall was skipped.
- Catches any presentation errors.
- Runs when the paywall is dismissed. It switches over the
result. If the result isPurchasedPaywallResultorRestoredPaywallResultthe feature can be launched. However, if the result isDeclinedPaywallResult, it checks that thefeatureGatingBehaviorofpaywallInfoisnonGatedand that thecloseReasonisn'tforNextPaywall.
If you only need the feature to run, pass a feature callback to registerPlacement instead. Superwall then runs it based on the paywall's gating mode.
To find out whether a placement would show a paywall without presenting it, use getPresentationResult.
How is this guide?
Using Superwall Deep Links
(iOS only) How to use Superwall Deep Links to trigger paywalls or custom in-app behavior.
Using the Presentation Handler
Track when a paywall presents, dismisses, errors or is skipped in your Flutter app by attaching a PaywallPresentationHandler when you register a placement.