easy_ads_flutter 26.3.12 copy "easy_ads_flutter: ^26.3.12" to clipboard
easy_ads_flutter: ^26.3.12 copied to clipboard

Easy Ads is a wrapper around famous ad packages which let you integrate ads easily

example/lib/main.dart

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

import 'package:easy_ads_flutter/easy_ads_flutter.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 EasyAds.instance.initialize(
    isShowAppOpenOnAppStateChange: false,
    adIdManager,
    adMobAdRequest: const AdRequest(),
    admobConfiguration: RequestConfiguration(testDeviceIds: []),
    showAdBadge: Platform.isIOS,
    autoLoadAds: true,
  );

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter Easy Ads 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("Ad Network List"), centerTitle: true),
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            children: [
              Text(
                "Auto load ad is ${EasyAds.instance.autoLoadAds ? 'Enabled' : 'Disabled'}",
              ),
              SizedBox(height: 10),
              Text(
                'AppOpen',
                style: Theme.of(context).textTheme.headlineMedium!.copyWith(
                  color: Colors.blue,
                  fontWeight: FontWeight.bold,
                ),
              ),
              AdButton(
                networkName: 'Admob AppOpen',
                onTap: () => _showAd(AdUnitType.appOpen),
              ),
              AdButton(
                networkName: 'JIT Admob AppOpen',
                onTap: () => _showJitAppOpen(),
              ),
              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(AdUnitType.interstitial),
              ),
              AdButton(
                networkName: 'Jit Admob Interstitial',
                onTap: () => _showJitInterstitial(context),
              ),

              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(AdUnitType.rewarded),
              ),
              AdButton(
                networkName: 'Jit Admob Rewarded',
                onTap: () => _showJitRewarded(),
              ),

              AdButton(
                networkName: 'Available Rewarded',
                onTap: () => _showAvailableAd(AdUnitType.rewarded),
              ),
              EasyBannerAd(),
            ],
          ),
        ),
      ),
    );
  }

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

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

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

  void _showJitAppOpen() {
    EasyAds.instance.showJitAppOpen(
      onFailedToLoadOrShow: () => _showSnack('AppOpen failed'),
      onAdShowed: () => _showSnack('AppOpen showed'),
      onAdDismissed: () => _showSnack('AppOpen dismissed'),
    );
  }

  void _showJitInterstitial(BuildContext context) {
    EasyAds.instance.showJitInterstitial(
      context,
      onFailedToLoadOrShow: () => _showSnack('Interstitial failed'),
      onAdShowed: () => _showSnack('Interstitial showed'),
      onAdDismissed: () => _showSnack('Interstitial dismissed'),
    );
  }

  void _showJitRewarded() {
    EasyAds.instance.showJitRewarded(
      context,
      onEarnedReward: (ctx) => _showSnack('Reward earned!'),
    );
  }

  void _showSnack(String message) {
    if (!mounted) return;
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text(message), duration: const Duration(seconds: 2)),
    );
  }
}

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

  @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',
                ),
              ),
            ),
          ),
          EasyBannerAd(adSize: AdSize.largeBanner),
          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),
                ),
              ),
            ),
          ),
          EasyAds.instance.createNativeAd(),
          SizedBox(height: 20),
        ],
      ),
    );
  }
}

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),
          ),
        ),
      ),
    );
  }
}
124
likes
150
points
549
downloads

Documentation

API reference

Publisher

verified publishernooralibutt.com

Weekly Downloads

Easy Ads is a wrapper around famous ad packages which let you integrate ads easily

Repository (GitHub)
View/report issues

License

BSD-3-Clause (license)

Dependencies

collection, flutter, google_mobile_ads, logger

More

Packages that depend on easy_ads_flutter