audience_network 0.0.2 copy "audience_network: ^0.0.2" to clipboard
audience_network: ^0.0.2 copied to clipboard

outdated

Facebook Audience Network plugin for Flutter apps. You can show Facebook Banner ads, Interstitial ads, Rewarded video ads and Native ads though this plugin.

example/lib/main.dart

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

void main() => runApp(AdExampleApp());

class AdExampleApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Audience Network Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        buttonTheme: ButtonThemeData(
          textTheme: ButtonTextTheme.primary,
          buttonColor: Colors.blue,
        ),
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text(
            'Audience Network Example',
          ),
        ),
        body: AdsPage(),
      ),
    );
  }
}

class AdsPage extends StatefulWidget {
  final String idfa;

  const AdsPage({Key? key, this.idfa = ''}) : super(key: key);

  @override
  AdsPageState createState() => AdsPageState();
}

class AdsPageState extends State<AdsPage> {
  bool _isInterstitialAdLoaded = false;
  bool _isRewardedAdLoaded = false;
  InterstitialAd? _interstitialAd;
  RewardedAd? _rewardedAd;

  /// All widget ads are stored in this variable. When a button is pressed, its
  /// respective ad widget is set to this variable and the view is rebuilt using
  /// setState().
  Widget _currentAd = SizedBox(
    width: 0.0,
    height: 0.0,
  );

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

    // testingId is useful when you want to test if your implementation works in production
    // without getting real ads, I believe it does not work properly on iOS,
    // if you want to get your testingId, don't set any testingId and don't set testMode
    AudienceNetwork.init(
      // testingId: "b602d594afd2b0b327e07a06f36ca6a7e42546d0",
      testMode: true,
    ).then((_) {
      _loadInterstitialAd();
      _loadRewardedVideoAd();
    });
  }

  void _loadInterstitialAd() {
    final interstitialAd = InterstitialAd('YOUR_PLACEMENT_ID');
    interstitialAd.listener = InterstitialAdListener(
      onLoaded: () {
        _isInterstitialAdLoaded = true;
        print('interstitial ad loaded');
      },
      onError: (code, message) {
        print('interstitial ad error\ncode = $code\nmessage = $message');
      },
      onDismissed: () {
        // load next ad already
        interstitialAd.destroy();
        _isInterstitialAdLoaded = false;
        _loadInterstitialAd();
      },
    );
    interstitialAd.load();
    _interstitialAd = interstitialAd;
  }

  void _loadRewardedVideoAd() {
    final rewardedAd = RewardedAd('YOUR_PLACEMENT_ID');
    rewardedAd.listener = RewardedAdListener(
      onLoaded: () {
        _isRewardedAdLoaded = true;
        print('rewarded ad loaded');
      },
      onError: (code, message) {
        print('rewarded ad error\ncode = $code\nmessage = $message');
      },
      onVideoClosed: () {
        // load next ad already
        rewardedAd.destroy();
        _isRewardedAdLoaded = false;
        _loadRewardedVideoAd();
      },
    );
    rewardedAd.load();
    _rewardedAd = rewardedAd;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Flexible(
          child: Align(
            alignment: Alignment(0, -1.0),
            child: Padding(
              padding: EdgeInsets.all(16),
              child: _getAllButtons(),
            ),
          ),
          fit: FlexFit.tight,
          flex: 2,
        ),
        // Column(children: <Widget>[
        //   _nativeAd(),
        //   // _nativeBannerAd(),
        //   _nativeAd(),
        // ],),
        Flexible(
          child: Align(
            alignment: Alignment(0, 1.0),
            child: _currentAd,
          ),
          fit: FlexFit.tight,
          flex: 3,
        )
      ],
    );
  }

  Widget _getAllButtons() {
    return GridView.count(
      shrinkWrap: true,
      crossAxisCount: 2,
      childAspectRatio: 3,
      children: <Widget>[
        _getRaisedButton(title: "Banner Ad", onPressed: _showBannerAd),
        _getRaisedButton(title: "Native Ad", onPressed: _showNativeAd),
        _getRaisedButton(
            title: "Native Banner Ad", onPressed: _showNativeBannerAd),
        _getRaisedButton(
            title: "Intestitial Ad", onPressed: _showInterstitialAd),
        _getRaisedButton(title: "Rewarded Ad", onPressed: _showRewardedAd),
      ],
    );
  }

  Widget _getRaisedButton({required String title, void Function()? onPressed}) {
    return Padding(
      padding: EdgeInsets.all(8),
      child: ElevatedButton(
        onPressed: onPressed,
        child: Text(
          title,
          textAlign: TextAlign.center,
        ),
      ),
    );
  }

  _showInterstitialAd() {
    final interstitialAd = _interstitialAd;

    if (interstitialAd != null && _isInterstitialAdLoaded == true)
      interstitialAd.show();
    else
      print("Interstial Ad not yet loaded!");
  }

  _showRewardedAd() {
    final rewardedAd = _rewardedAd;

    if (rewardedAd != null && _isRewardedAdLoaded) {
      rewardedAd.show();
    } else {
      print("Rewarded Ad not yet loaded!");
    }
  }

  _showBannerAd() {
    setState(() {
      _currentAd = BannerAd(
        // placementId: "YOUR_PLACEMENT_ID",
        placementId:
            "IMG_16_9_APP_INSTALL#2312433698835503_2964944860251047", //testid
        bannerSize: BannerSize.STANDARD,
        listener: (result, value) {
          print("Banner Ad: $result -->  $value");
        },
      );
    });
  }

  _showNativeBannerAd() {
    setState(() {
      _currentAd = _nativeBannerAd();
    });
  }

  Widget _nativeBannerAd() {
    return NativeAd(
      // placementId: "YOUR_PLACEMENT_ID",
      placementId: "IMG_16_9_APP_INSTALL#2312433698835503_2964953543583512",
      adType: NativeAdType.NATIVE_BANNER_AD,
      bannerAdSize: NativeBannerAdSize.HEIGHT_100,
      width: double.infinity,
      backgroundColor: Colors.blue,
      titleColor: Colors.white,
      descriptionColor: Colors.white,
      buttonColor: Colors.deepPurple,
      buttonTitleColor: Colors.white,
      buttonBorderColor: Colors.white,
      listener: (result, value) {
        print("Native Banner Ad: $result --> $value");
      },
    );
  }

  _showNativeAd() {
    setState(() {
      _currentAd = _nativeAd();
    });
  }

  Widget _nativeAd() {
    return NativeAd(
      placementId: "IMG_16_9_APP_INSTALL#2312433698835503_2964952163583650",
      adType: NativeAdType.NATIVE_AD_VERTICAL,
      width: double.infinity,
      height: 300,
      backgroundColor: Colors.blue,
      titleColor: Colors.white,
      descriptionColor: Colors.white,
      buttonColor: Colors.deepPurple,
      buttonTitleColor: Colors.white,
      buttonBorderColor: Colors.white,
      listener: (result, value) {
        print("Native Ad: $result --> $value");
      },
      keepExpandedWhileLoading: true,
      expandAnimationDuraion: 1000,
    );
  }
}
13
likes
0
points
30
downloads

Publisher

unverified uploader

Weekly Downloads

Facebook Audience Network plugin for Flutter apps. You can show Facebook Banner ads, Interstitial ads, Rewarded video ads and Native ads though this plugin.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

characters, flutter, meta

More

Packages that depend on audience_network

Packages that implement audience_network