Advanced

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 a PaywallView.
  • Give it a bounded size. It fills the space its parent gives it.
  • There's no feature callback. The result arrives in onFinished, where you decide whether to unlock the feature. When shouldDismiss is true, the paywall has left the view, so remove the PaywallView.
  • onSkip and onError tell you when no paywall is shown. skippedBuilder and errorBuilder choose 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:

  1. Runs when Superwall presents the paywall.
  2. Handles the cases where the paywall was skipped.
  3. Catches any presentation errors.
  4. Runs when the paywall is dismissed. It switches over the result. If the result is PurchasedPaywallResult or RestoredPaywallResult the feature can be launched. However, if the result is DeclinedPaywallResult, it checks that the featureGatingBehavior of paywallInfo is nonGated and that the closeReason isn't forNextPaywall.

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?