Display interstitial ad on Flutter

Step 1: (Optional) Construct event listener

final AdropInterstitialListener listener = AdropInterstitialListener(
    onAdReceived: (ad) =>
        debugPrint('Adrop Interstitial Ad loaded with unitId ${ad.unitId}!'),
    onAdFailedToReceive: (ad, errorCode) => 
        debugPrint('error in ${ad.unitId} while loading: $errorCode'),
    onAdFailedToShowFullScreen: (ad, errorCode) =>
        debugPrint('error in ${ad.unitId} while showing: $errorCode'),
    ...
);

Step 2: Display an interstitial ad

class YourComponent extends StatefulWidget {
  const YourComponent({super.key});

  @override
  State<StatefulWidget> createState() => _YourComponentState();
}

class _YourComponentState extends State<YourComponent> {
  final AdropInterstitialAd interstitialAd = AdropInterstitialAd(
    unitId: 'YOUR_UNIT_ID',
    listener: listener,
  );

  @override
  void initState() {
    super.initState();
    interstitialAd.load();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextButton(
          onPressed: () {
            final isLoaded = interstitialAd.isLoaded ?? false;
            if (isLoaded) {
              interstitialAd.show();
            } else {
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(content: Text('interstitial ad is loading...'))
              );
            }
          },
          child: const Text('display ad'),
        ),
      ),
    );
  }
}

AdropInterstitialAd must be disposed of when access to it is no longer needed.

@override
void dispose() {
  super.dispose();
  interstitialAd.dispose();
}

Last updated