avory_ad_control 1.0.0 copy "avory_ad_control: ^1.0.0" to clipboard
avory_ad_control: ^1.0.0 copied to clipboard

PlatformAndroidiOS
unlisted

Avory Ad Control is a wrapper around Google Admob which let you integrate ads easily

example/lib/main.dart

import 'dart:async';
import 'dart:io';

import 'package:avory_ad_control/avory_ad_control.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

const IAdIdManager adIdManager = TestAdIdManager();

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await ConsentManager.gatherGdprConsent(
    debugGeography: kDebugMode ? DebugGeography.debugGeographyEea : null,
  );
  await ConsentManager.gatherPrivacyConsent();

  await AivoryAdControl.instance.initialize(
    isShowAppOpenOnAppStateChange: false,
    adIdManager,
    adMobAdRequest: const AdRequest(),
    admobConfiguration: RequestConfiguration(testDeviceIds: []),
    showAdBadge: Platform.isIOS,
  );

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter Ad Control Example',
      home: CountryListScreen(),
    );
  }
}

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

  @override
  State<CountryListScreen> createState() => _CountryListScreenState();
}

class _CountryListScreenState extends State<CountryListScreen> {
  /// Using it to cancel the subscribed callbacks
  StreamSubscription? _streamSubscription;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("AdMob Ads Example"), centerTitle: true),
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            children: [
              Text(
                'AppOpen',
                style: Theme.of(context).textTheme.headlineMedium!.copyWith(
                  color: Colors.blue,
                  fontWeight: FontWeight.bold,
                ),
              ),
              AdButton(
                networkName: 'Admob AppOpen',
                onTap: () => _showAd(AdNetwork.admob, AdUnitType.appOpen),
              ),
              const Divider(thickness: 2),
              Text(
                'Interstitial',
                style: Theme.of(context).textTheme.headlineMedium!.copyWith(
                  color: Colors.blue,
                  fontWeight: FontWeight.bold,
                ),
              ),
              AdButton(
                networkName: 'Admob Interstitial',
                onTap: () => _showAd(AdNetwork.admob, AdUnitType.interstitial),
              ),
              AdButton(
                networkName: 'Available Interstitial',
                onTap: () => _showAvailableAd(AdUnitType.interstitial),
              ),
              const Divider(thickness: 2),
              Text(
                'Rewarded',
                style: Theme.of(context).textTheme.headlineMedium!.copyWith(
                  color: Colors.blue,
                  fontWeight: FontWeight.bold,
                ),
              ),
              AdButton(
                networkName: 'Admob Rewarded',
                onTap: () => _showAd(AdNetwork.admob, AdUnitType.rewarded),
              ),
              AdButton(
                networkName: 'Available Rewarded',
                onTap: () => _showAvailableAd(AdUnitType.rewarded),
              ),
              // Banner ads can be added here when needed
            ],
          ),
        ),
      ),
    );
  }

  void _showAd(AdNetwork adNetwork, AdUnitType adUnitType) {
    if (AivoryAdControl.instance.showAd(
      adUnitType,
      adNetwork: adNetwork,
      context: context,
      loaderDuration: 1,
    )) {
      // Canceling the last callback subscribed
      _streamSubscription?.cancel();
      // Listening to the callback from showRewardedAd()
      _streamSubscription = AivoryAdControl.instance.onEvent.listen((event) {
        if (event.adUnitType == adUnitType) {
          _streamSubscription?.cancel();
          goToNextScreen(adNetwork: adNetwork);
        }
      });
    } else {
      goToNextScreen(adNetwork: adNetwork);
    }
  }

  void _showAvailableAd(AdUnitType adUnitType) {
    if (AivoryAdControl.instance.showAd(adUnitType)) {
      // Canceling the last callback subscribed
      _streamSubscription?.cancel();
      // Listening to the callback from showRewardedAd()
      _streamSubscription = AivoryAdControl.instance.onEvent.listen((event) {
        if (event.adUnitType == adUnitType) {
          _streamSubscription?.cancel();
          goToNextScreen();
        }
      });
    } else {
      goToNextScreen();
    }
  }

  void goToNextScreen({AdNetwork? adNetwork}) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => CountryDetailScreen(adNetwork: adNetwork),
      ),
    );
  }
}

class CountryDetailScreen extends StatefulWidget {
  final AdNetwork? adNetwork;
  const CountryDetailScreen({super.key, this.adNetwork});

  @override
  State<CountryDetailScreen> createState() => _CountryDetailScreenState();
}

class _CountryDetailScreenState extends State<CountryDetailScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('United States'), centerTitle: true),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Container(
            height: 200,
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: NetworkImage(
                  'https://cdn.britannica.com/33/4833-050-F6E415FE/Flag-United-States-of-America.jpg',
                ),
              ),
            ),
          ),
          widget.adNetwork == null
              ? Container() // Placeholder for banner ad
              : AivoryAdControl.instance
                      .createBanner(
                        adNetwork: widget.adNetwork!,
                        adSize: AdSize.largeBanner,
                      )
                      ?.show() ??
                  Container(),
          const Expanded(
            child: SingleChildScrollView(
              child: Padding(
                padding: EdgeInsets.all(20.0),
                child: Text(
                  'The U.S. is a country of 50 states covering a vast swath of North America, with Alaska in the northwest and Hawaii extending the nation\'s presence into the Pacific Ocean. Major Atlantic Coast cities are New York, a global finance and culture center, and capital Washington, DC. Midwestern metropolis Chicago is known for influential architecture and on the west coast, Los Angeles\' Hollywood is famed for filmmaking',
                  style: TextStyle(fontWeight: FontWeight.w600, fontSize: 22),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class AdButton extends StatelessWidget {
  final String networkName;
  final VoidCallback onTap;
  const AdButton({super.key, required this.onTap, required this.networkName});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Card(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Text(
            networkName,
            style: const TextStyle(fontSize: 28, fontWeight: FontWeight.w300),
          ),
        ),
      ),
    );
  }
}
0
likes
130
points
57
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Avory Ad Control is a wrapper around Google Admob which let you integrate ads easily

Repository (GitHub)

License

MIT (license)

Dependencies

collection, flutter, google_mobile_ads, logger

More

Packages that depend on avory_ad_control