flutter_admobs_handler 1.1.5 copy "flutter_admobs_handler: ^1.1.5" to clipboard
flutter_admobs_handler: ^1.1.5 copied to clipboard

Reusable AdMob utilities for Flutter with banner, interstitial, and rewarded ads, test/production switching, and a central AppAdsUtil configuration API.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_admobs_handler/flutter_admobs_handler.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Defaults to Google test inventory. Set production unit IDs and
  // useTestAds = false before shipping.
  AppAdsUtil.instance.configure(useTestAds: true, onLog: debugPrint);

  await AppAdsUtil.instance.initializeAdsSdk();
  runApp(const ExampleApp());
}

class ExampleApp extends StatelessWidget {
  const ExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'flutter_admobs_handler example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
        useMaterial3: true,
      ),
      home: const AdsDemoPage(),
    );
  }
}

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

  @override
  State<AdsDemoPage> createState() => _AdsDemoPageState();
}

class _AdsDemoPageState extends State<AdsDemoPage> {
  late final InterstitialAdUtil _interstitial;
  late final RewardedAdUtil _rewarded;

  var _interstitialReady = false;
  var _rewardedReady = false;
  var _isShowingFullscreen = false;
  String _status = 'Loading ads…';

  @override
  void initState() {
    super.initState();

    _interstitial = InterstitialAdUtil(
      adUnitIds: AppAdsUtil.instance.interstitialAdUnitIds,
      onLoaded: () {
        if (!mounted) return;
        setState(() {
          _interstitialReady = true;
          _status = 'Interstitial ready';
        });
      },
      onError: (error) {
        if (!mounted) return;
        setState(() {
          _interstitialReady = false;
          _status = 'Interstitial error: $error';
        });
      },
      onDismissed: () {
        if (!mounted) return;
        setState(() {
          _interstitialReady = false;
          _status = 'Interstitial dismissed; reloading…';
        });
      },
    );

    _rewarded = RewardedAdUtil(
      adUnitIds: AppAdsUtil.instance.rewardedAdUnitIds,
      onLoaded: () {
        if (!mounted) return;
        setState(() {
          _rewardedReady = true;
          _status = 'Rewarded ready';
        });
      },
      onError: (error) {
        if (!mounted) return;
        setState(() {
          _rewardedReady = false;
          _status = 'Rewarded error: $error';
        });
      },
      onDismissed: () {
        if (!mounted) return;
        setState(() {
          _rewardedReady = false;
          _status = 'Rewarded dismissed; reloading…';
        });
      },
    );

    _preloadFullscreenAds();
  }

  Future<void> _preloadFullscreenAds() async {
    await Future.wait<bool>([_interstitial.load(), _rewarded.load()]);
    if (!mounted) return;
    setState(() {
      _status =
          'Banner uses Google test units. Tap a button to show a fullscreen ad.';
    });
  }

  Future<void> _showInterstitial() async {
    if (_isShowingFullscreen || !_interstitial.isReady) {
      setState(() => _status = 'Interstitial is not ready yet');
      return;
    }

    setState(() {
      _isShowingFullscreen = true;
      _interstitialReady = false;
      _status = 'Showing interstitial…';
    });

    final shown = await _interstitial.show();
    if (!mounted) return;
    setState(() {
      _isShowingFullscreen = false;
      _status = shown ? 'Interstitial closed' : 'Interstitial failed to show';
    });
  }

  Future<void> _showRewarded() async {
    if (_isShowingFullscreen || !_rewarded.isReady) {
      setState(() => _status = 'Rewarded ad is not ready yet');
      return;
    }

    setState(() {
      _isShowingFullscreen = true;
      _rewardedReady = false;
      _status = 'Showing rewarded…';
    });

    final RewardedAdShowResult result = await _rewarded.show(
      onUserEarnedReward: (reward) {
        debugPrint('Reward callback: ${reward.amount} ${reward.type}');
      },
    );

    if (!mounted) return;
    setState(() {
      _isShowingFullscreen = false;
      if (!result.shown) {
        _status = 'Rewarded ad failed to show';
      } else if (result.rewardEarned) {
        final reward = result.reward;
        _status = reward == null
            ? 'Reward earned'
            : 'Reward earned: ${reward.amount} ${reward.type}';
      } else {
        _status = 'Rewarded ad closed without earning a reward';
      }
    });
  }

  @override
  void dispose() {
    _interstitial.dispose();
    _rewarded.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('AdMob Example')),
      body: Column(
        children: [
          Expanded(
            child: ListView(
              padding: const EdgeInsets.all(24),
              children: [
                Text(
                  'flutter_admobs_handler',
                  style: Theme.of(context).textTheme.headlineSmall,
                ),
                const SizedBox(height: 8),
                Text(
                  'Demonstrates banner, interstitial, and rewarded ads with '
                  'RewardedAdShowResult from Google test inventory.',
                  style: Theme.of(context).textTheme.bodyMedium,
                ),
                const SizedBox(height: 24),
                Text(_status),
                const SizedBox(height: 24),
                FilledButton(
                  onPressed: _interstitialReady && !_isShowingFullscreen
                      ? _showInterstitial
                      : null,
                  child: Text(
                    _interstitialReady
                        ? 'Show interstitial'
                        : 'Loading interstitial…',
                  ),
                ),
                const SizedBox(height: 12),
                FilledButton.tonal(
                  onPressed: _rewardedReady && !_isShowingFullscreen
                      ? _showRewarded
                      : null,
                  child: Text(
                    _rewardedReady ? 'Show rewarded' : 'Loading rewarded…',
                  ),
                ),
              ],
            ),
          ),
          const AdMobBannerWidget(useAdaptiveWidth: true),
        ],
      ),
    );
  }
}
2
likes
0
points
252
downloads

Publisher

unverified uploader

Weekly Downloads

Reusable AdMob utilities for Flutter with banner, interstitial, and rewarded ads, test/production switching, and a central AppAdsUtil configuration API.

Repository (GitHub)
View/report issues

Topics

#admob #ads #advertising #google-mobile-ads #monetization

License

unknown (license)

Dependencies

flutter, google_mobile_ads

More

Packages that depend on flutter_admobs_handler