nefta_sdk_flutter 4.4.0 copy "nefta_sdk_flutter: ^4.4.0" to clipboard
nefta_sdk_flutter: ^4.4.0 copied to clipboard

Nefta SDK for flutter projects.

example/lib/main.dart

import 'dart:io';
import 'dart:math';

import 'package:flutter/material.dart';

import 'package:applovin_max/applovin_max.dart';
import 'package:nefta_sdk_flutter/nefta.dart';

void main() {
  runApp(IntegrationDemo());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MainPage(),
    );
  }
}

class MainPage extends StatefulWidget {
  @override
  MainState createState() => MainState();
}

class _AdRequest {
  double? revenue;
}

class MainState extends State<MainPage> {
  final _bannerViewId = ValueKey(1);

  static final String _neftaAppId = Platform.isAndroid ? "5678631082786816" : "5727602702548992";
  static final String _defaultBannerAdUnitId = Platform.isAndroid ? "d3d61616c344d2b4" : "78161f678bc0c46f";
  static final String _dynamicInterstitialAdUnitId = Platform.isAndroid ? "084edff9524b52ec" : "fbd50dc3d655933c";
  static final String _defaultInterstitialAdUnitId = Platform.isAndroid ? "0822634ec9c39d78" : "5e11b1838778c517";
  static final String _dynamicRewardedAdUnitId = Platform.isAndroid ? "c0c516310b8c7c04" : "c068edf12c4282a6";
  static final String _defaultRewardedAdUnitId = Platform.isAndroid ? "3d7ef05a78cf8615" : "ad9b024164e61c00";

  String _bannerAdUnitId = _defaultBannerAdUnitId;

  String _statusText = "Status";
  bool _isBannerMounted = false;

  bool _isInterstitialLoadingOn = false;
  AdInsight? _dynamicInterstitialInsight;
  _AdRequest? _dynamicInterstitialRequest;
  _AdRequest? _defaultInterstitialRequest;
  int _dynamicInterstitialFails = 0;

  bool _isRewardedLoadingOn = false;
  AdInsight? _dynamicRewardedInsight;
  _AdRequest? _dynamicRewardedRequest;
  _AdRequest? _defaultRewardedRequest;
  int _dynamicRewardedFails = 0;

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

    Nefta.enableLogging(true);
    Nefta.setExtraParameter(Nefta.extParamTestGroup, "split-f");
    Nefta.init(_neftaAppId);

    initializeAds();
  }

  Future<void> initializeAds() async {
    try {
      List<String> defaultAdUnitsForPreloading = Platform.isAndroid ?
      ["084edff9524b52ec", "c0c516310b8c7c04" ] : ["fbd50dc3d655933c", "c068edf12c4282a6" ];
      AppLovinMAX.setExtraParameter("disable_b2b_ad_unit_ids", defaultAdUnitsForPreloading.join(","));
      await AppLovinMAX.initialize("IAhBswbDpMg9GhQ8NEKffzNrXQP1H4ABNFvUA7ePIz2xmarVFcy_VB8UfGnC9IPMOgpQ3p8G5hBMebJiTHv3P9");
      setState(() {
        _statusText = "Ads Initialized Successfully";
      });

      initializeBanner();
      initializeInterstitial();
      initializeRewarded();
    } catch (e) {
      setState(() {
        _statusText = "Initialization Failed: $e";
      });
    }
  }

  void onCloseBannerClick() {
    AppLovinMAX.destroyBanner(_bannerAdUnitId);
    setState(() {
      _isBannerMounted = false;
    });
  }

  void initializeBanner() {
    AppLovinMAX.setBannerListener(
        AdViewAdListener(
            onAdLoadedCallback: (ad) {
              log("Banner loaded: ${ad.networkName}");
              setState(() {
                _isBannerMounted = true;
              });
            },
            onAdLoadFailedCallback: (adUnitId, error) {
              log("Banner failed to load: ${error.message}");
            },
            onAdClickedCallback: (ad) {
              log("Banner clicked");
            },
            onAdExpandedCallback: (ad) {
              log("Banner expanded");
            },
            onAdCollapsedCallback: (ad) {
              log("Banner collapsed");
            },
            onAdRevenuePaidCallback: (ad) {
              log("Banner revenue paid: ${ad.revenue}");
            }
        )
    );
  }

  void initializeInterstitial() {
    AppLovinMAX.setInterstitialListener(InterstitialListener(
      onAdLoadedCallback: (ad) {
        Nefta.onExternalMediationRequestLoaded(ad);

        if (ad.adUnitId == _dynamicInterstitialAdUnitId) {
          log("Loaded Dynamic Interstitial ${ad.adUnitId} at ${ad.revenue}");
          _dynamicInterstitialFails = 0;
          setState(() {
            _dynamicInterstitialRequest!.revenue = ad.revenue;
          });
        } else {
          log("Loaded Default Interstitial ${ad.adUnitId} at ${ad.revenue}");
          setState(() {
            _defaultInterstitialRequest!.revenue = ad.revenue;
          });
        }
      },
      onAdLoadFailedCallback: (adUnitId, error) {
        Nefta.onExternalMediationRequestFailed(adUnitId, error);

        if (adUnitId == _dynamicInterstitialAdUnitId) {
          _dynamicInterstitialFails = _dynamicInterstitialFails + 1;
          int retryDelay = pow(2, min(6, _dynamicInterstitialFails)).toInt();

          log("Load failed Dynamic Interstitial ${error.code.toString()} - retrying in ${retryDelay.toString()}s");

          Future.delayed(Duration(milliseconds: retryDelay * 1000), () {
            if (_isInterstitialLoadingOn) {
              getInterstitialInsightAndLoad(_dynamicInterstitialInsight);
            } else {
              setState(() {
                _dynamicInterstitialRequest = null;
              });
            }
          });
        } else {
          log("Load failed Default Interstitial code ${error.code.toString()}");

          if (_isInterstitialLoadingOn) {
            loadDefaultInterstitial();
          } else {
            setState(() {
              _defaultInterstitialRequest = null;
            });
          }
        }
      },
      onAdDisplayedCallback: (ad) {
        log("onAdDisplayedCallback");
      },
      onAdDisplayFailedCallback: (ad, error) {
        log("onAdDisplayFailedCallback");
      },
      onAdClickedCallback: (ad) {
        Nefta.onExternalMediationClick(ad);

        log("onAdClickedCallback");
      },
      onAdHiddenCallback: (ad) {
        log("onAdHiddenCallback");

        // start new cycle
        if (_isInterstitialLoadingOn) {
          startInterstitialLoading();
        }
      },
      onAdRevenuePaidCallback: (ad) {
        Nefta.onExternalMediationImpression(ad);

        log("onAdRevenuePaidCallback: ${ad.adFormat} ${ad.revenue}");
      }
    ));
  }

  void initializeRewarded() {
    AppLovinMAX.setRewardedAdListener(RewardedAdListener(
        onAdLoadedCallback: (ad) {
          Nefta.onExternalMediationRequestLoaded(ad);

          if (ad.adUnitId == _dynamicRewardedAdUnitId) {
            log("Loaded Dynamic Rewarded ${ad.adUnitId} at ${ad.revenue}");
            _dynamicRewardedFails = 0;
            setState(() {
              _dynamicRewardedRequest!.revenue = ad.revenue;
            });
          } else {
            log("Loaded Default Rewarded ${ad.adUnitId} at ${ad.revenue}");
            setState(() {
              _defaultRewardedRequest!.revenue = ad.revenue;
            });
          }
        },
        onAdLoadFailedCallback: (adUnitId, error) {
          Nefta.onExternalMediationRequestFailed(adUnitId, error);

          if (adUnitId == _dynamicRewardedAdUnitId) {
            _dynamicRewardedFails = _dynamicRewardedFails + 1;
            int retryDelay = pow(2, min(6, _dynamicRewardedFails)).toInt();

            log("Load failed Dynamic Rewarded code ${error.code.toString()} - retrying in ${retryDelay.toString()}s");

            Future.delayed(Duration(milliseconds: retryDelay * 1000), () {
              if (_isRewardedLoadingOn) {
                getRewardedInsightAndLoad(_dynamicRewardedInsight);
              } else {
                setState(() {
                  _dynamicRewardedRequest = null;
                });
              }
            });
          } else {
            log("Load failed Default Rewarded code ${error.code.toString()}");

            if (_isRewardedLoadingOn) {
              loadDefaultRewarded();
            } else {
              setState(() {
                _defaultRewardedRequest = null;
              });
            }
          }
        },
        onAdDisplayedCallback: (ad) {
          log("onAdDisplayedCallback");
        },
        onAdDisplayFailedCallback: (ad, error) {
          log("onAdDisplayFailedCallback");
        },
        onAdClickedCallback: (ad) {
          Nefta.onExternalMediationClick(ad);

          log("onAdClickedCallback");
        },
        onAdHiddenCallback: (ad) {
          log("onAdClickedCallback");

          // start new cycle
          if (_isRewardedLoadingOn) {
            startRewardedLoading();
          }
        },
        onAdRevenuePaidCallback: (ad) {
          Nefta.onExternalMediationImpression(ad);

          log("onAdRevenuePaidCallback: ${ad.revenue}");
        },
        onAdReceivedRewardCallback: (ad, reward) {
          log("onAdClickedCallback");
        }
    ));
  }

  void log(String log) {
    print("NeftaPluginF ${log}");
    setState(() {
      _statusText = log;
    });
  }

  void onShowBannerClick() {
    _bannerAdUnitId = _defaultBannerAdUnitId;

    AppLovinMAX.createBanner(_bannerAdUnitId, AdViewPosition.topCenter, false);
    AppLovinMAX.stopBannerAutoRefresh(_bannerAdUnitId);

    AppLovinMAX.loadBanner(_bannerAdUnitId);

    log("load banner");
    addDemoIntegrationExampleEvent(1);
  }

  void getInterstitialInsightAndLoad(AdInsight? previousInsight) {
    setState(() {
      _dynamicInterstitialRequest = _AdRequest();
    });
    Nefta.getInsights(Insights.INTERSTITIAL, previousInsight, loadDynamicInterstitial, 5);
  }

  void loadDynamicInterstitial(Insights insights) {
    _dynamicInterstitialInsight = insights.interstitial;
    if (_dynamicInterstitialInsight != null) {
      String bidFloor = _dynamicInterstitialInsight!.floorPrice.toStringAsFixed(10);

      log("Loading Dynamic Interstitial with floor ${bidFloor}");
      AppLovinMAX.setInterstitialExtraParameter(_dynamicInterstitialAdUnitId, "disable_auto_retries", "true");
      AppLovinMAX.setInterstitialExtraParameter(_dynamicInterstitialAdUnitId, "jC7Fp", bidFloor);
      AppLovinMAX.loadInterstitial(_dynamicInterstitialAdUnitId);

      Nefta.onExternalMediationRequest(AdType.Interstitial, _dynamicInterstitialAdUnitId, _dynamicInterstitialInsight);
    }
  }

  void loadDefaultInterstitial() {
    setState(() {
      _defaultInterstitialRequest = _AdRequest();
    });
    AppLovinMAX.loadInterstitial(_defaultInterstitialAdUnitId);

    Nefta.onExternalMediationRequest(AdType.Interstitial, _defaultInterstitialAdUnitId);
  }

  void startInterstitialLoading() {
    if (_dynamicInterstitialRequest == null) {
      getInterstitialInsightAndLoad(null);
    }
    if (_defaultInterstitialRequest == null) {
      loadDefaultInterstitial();
    }
  }

  Future<void> onInterstitialShowClick() async {
    bool isShown = false;
    if (_dynamicInterstitialRequest != null && _dynamicInterstitialRequest!.revenue != null) {
      if (_defaultInterstitialRequest != null && _defaultInterstitialRequest!.revenue != null &&
          _defaultInterstitialRequest!.revenue! > _dynamicInterstitialRequest!.revenue!) {
        isShown = await tryShowDefaultInterstitial();
      }
      if (!isShown) {
        isShown = await tryShowDynamicInterstitial();
      }
    }
    if (!isShown && _defaultInterstitialRequest != null && _defaultInterstitialRequest!.revenue != null) {
      tryShowDefaultInterstitial();
    }
  }

  Future<bool> tryShowDynamicInterstitial() async {
    bool isShown = false;
    bool isReady = (await AppLovinMAX.isInterstitialReady(_dynamicInterstitialAdUnitId))!;
    if (isReady) {
      AppLovinMAX.showInterstitial(_dynamicInterstitialAdUnitId);
      isShown = true;
    }
    setState(() {
      _dynamicInterstitialRequest = null;
    });
    return isShown;
  }

  Future<bool> tryShowDefaultInterstitial() async {
    bool isShown = false;
    bool isReady = (await AppLovinMAX.isInterstitialReady(_defaultInterstitialAdUnitId))!;
    if (isReady) {
      AppLovinMAX.showInterstitial(_defaultInterstitialAdUnitId);
      isShown = true;
    }
    setState(() {
      _defaultInterstitialRequest = null;
    });
    return isShown;
  }

  void startRewardedLoading() {
    if (_dynamicRewardedRequest == null) {
      getRewardedInsightAndLoad(null);
    }
    if (_defaultRewardedRequest == null) {
      loadDefaultRewarded();
    }
  }

  void getRewardedInsightAndLoad(AdInsight? previousInsight) {
    setState(() {
      _dynamicRewardedRequest = _AdRequest();
    });
    Nefta.getInsights(Insights.REWARDED, previousInsight, loadDynamicRewarded, 5);
  }

  void loadDynamicRewarded(Insights insights) {
    _dynamicRewardedInsight = insights.rewarded;
    if (insights.rewarded != null) {
      String bidFloor = _dynamicRewardedInsight!.floorPrice.toStringAsFixed(10);

      log("Loading Dynamic Rewarded with floor ${bidFloor}");
      AppLovinMAX.setRewardedAdExtraParameter(_dynamicRewardedAdUnitId, "disable_auto_retries", "true");
      AppLovinMAX.setRewardedAdExtraParameter(_dynamicRewardedAdUnitId, "jC7Fp", bidFloor);
      AppLovinMAX.loadRewardedAd(_dynamicRewardedAdUnitId);

      Nefta.onExternalMediationRequest(AdType.Rewarded, _dynamicRewardedAdUnitId, _dynamicRewardedInsight);
    }
  }

  void loadDefaultRewarded() {
    setState(() {
      _defaultRewardedRequest = _AdRequest();
    });
    AppLovinMAX.loadRewardedAd(_defaultRewardedAdUnitId);

    Nefta.onExternalMediationRequest(AdType.Rewarded, _defaultRewardedAdUnitId);
  }

  Future<void> onRewardedShowClick() async {
    bool isShown = false;
    if (_dynamicRewardedRequest != null && _dynamicRewardedRequest!.revenue != null) {
      if (_defaultRewardedRequest != null && _defaultRewardedRequest!.revenue != null &&
      _defaultRewardedRequest!.revenue! > _dynamicRewardedRequest!.revenue!) {
        isShown = await tryShowDefaultRewarded();
      }
      if (!isShown) {
        isShown = await tryShowDynamicRewarded();
      }
    }
    if (!isShown && _defaultRewardedRequest != null && _defaultRewardedRequest!.revenue != null) {
      tryShowDefaultRewarded();
    }
  }

  Future<bool> tryShowDynamicRewarded() async {
    bool isShown = false;
    bool isReady = (await AppLovinMAX.isRewardedAdReady(_dynamicRewardedAdUnitId))!;
    if (isReady) {
      AppLovinMAX.showRewardedAd(_dynamicRewardedAdUnitId);
      isShown = true;
    }
    setState(() {
      _dynamicRewardedRequest = null;
    });
    return isShown;
  }

  Future<bool> tryShowDefaultRewarded() async {
    bool isShown = false;
    bool isReady = (await AppLovinMAX.isRewardedAdReady(_defaultRewardedAdUnitId))!;
    if (isReady) {
      AppLovinMAX.showRewardedAd(_defaultRewardedAdUnitId);
      isShown = true;
    }
    setState(() {
      _defaultRewardedRequest = null;
    });
    return isShown;
  }

  void addDemoIntegrationExampleEvent(int type) {
    final random = Random();
    String name = "example event";
    int value = random.nextInt(101);
    if (type == 0) {
      int progressionTypeInt = random.nextInt(7);
      int statusInt = random.nextInt(3);
      int sourceInt = random.nextInt(7);
      String custom = "progression type:$progressionTypeInt status:$statusInt source:$sourceInt v:$value";

      ProgressionType progressionType = ProgressionType.fromInt(progressionTypeInt);
      ProgressionStatus status = ProgressionStatus.fromInt(statusInt);
      ProgressionSource source = ProgressionSource.fromInt(sourceInt);
      ProgressionEvent(progressionType, status, source: source, name: name, value: value, customString: custom).record();
    } else if (type == 1) {
      int categoryInt = random.nextInt(9);
      int methodInt = random.nextInt(7);
      String custom = "receive category:$categoryInt method:$methodInt v:$value";

      ResourceCategory resourceCategory = ResourceCategory.fromInt(categoryInt);
      ReceiveMethod receiveMethod = ReceiveMethod.fromInt(methodInt);
      ReceiveEvent(resourceCategory, method: receiveMethod, name: name, value: value, customString: custom).record();
    } else {
      int categoryInt = random.nextInt(9);
      int methodInt = random.nextInt(7);
      String custom = "spend category:$categoryInt method:$methodInt v:$value";

      ResourceCategory resourceCategory = ResourceCategory.fromInt(categoryInt);
      SpendMethod spendMethod = SpendMethod.fromInt(methodInt);
      SpendEvent(resourceCategory, method: spendMethod, name: name, value: value, customString: custom).record();
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 40.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                // BannerView
                if (_isBannerMounted)
                  Container(
                    key: _bannerViewId,
                    width: 320,
                    height: 50,
                    color: Colors.blue,
                    alignment: Alignment.center,
                    child: MaxAdView(
                      adUnitId: _bannerAdUnitId,
                      adFormat: AdFormat.banner
                    )
                  )
                else
                  Container(
                    key: _bannerViewId,
                    width: 320,
                    height: 50,
                    color: Colors.blue,
                    alignment: Alignment.center,
                  )
                ,
                // LeaderView (hidden by default)
                Visibility(
                  visible: false,
                  child: Container(
                    width: 728,
                    height: 90,
                    color: Colors.blue,
                    alignment: Alignment.center,
                  ),
                ),
                // TableLayout equivalent
                Transform.translate(
                  offset: Offset(0, 100),
                  child: Column(
                    children: [
                      // TableRow 1
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          _buildButton("Show Banner", onShowBannerClick),
                          SizedBox(width: 10),
                          _buildButton("Close Banner", _isBannerMounted ? onCloseBannerClick : null),
                        ],
                      ),
                      // TableRow 2
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text("Load Interstitial"),
                          Switch(
                              value: _isInterstitialLoadingOn,
                              onChanged: (value) {
                                if (value) {
                                  startInterstitialLoading();
                                }

                                setState(() {
                                  _isInterstitialLoadingOn = value;
                                });

                                addDemoIntegrationExampleEvent(2);
                              }),
                          SizedBox(width: 10),
                          _buildButton("Show Interstitial", _dynamicInterstitialRequest != null && _dynamicInterstitialRequest!.revenue != null ||
                              _defaultInterstitialRequest != null && _defaultInterstitialRequest!.revenue != null ? onInterstitialShowClick : null),
                        ],
                      ),
                      // TableRow 3
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text("Load Rewarded"),
                          Switch(
                            value: _isRewardedLoadingOn,
                              onChanged: (value) {
                                if (value) {
                                  startRewardedLoading();
                                }

                                setState(() {
                                  _isRewardedLoadingOn = value;
                                });

                                addDemoIntegrationExampleEvent(2);
                              }),
                          SizedBox(width: 10),
                          _buildButton("Show Rewarded", (_dynamicRewardedRequest != null && _dynamicRewardedRequest!.revenue != null ||
                              _defaultRewardedRequest != null && _defaultRewardedRequest!.revenue != null) ? onRewardedShowClick : null),
                        ],
                      ),
                      // TableRow 4
                      Container(
                        width: double.infinity,
                        padding: EdgeInsets.all(5),
                        child: Text(
                          _statusText,
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 16,
                          ),
                          textAlign: TextAlign.center,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildButton(String text, VoidCallback? onPress) {
    return Padding(
      padding: const EdgeInsets.all(5.0),
      child: ElevatedButton(
        onPressed: onPress,
        style: ElevatedButton.styleFrom(padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          backgroundColor: Color(0xff7d3ba2)),
        child: Text(text, style: TextStyle(color: Colors.white),
        ),
      ),
    );
  }
}
0
likes
130
points
2
downloads

Publisher

unverified uploader

Weekly Downloads

Nefta SDK for flutter projects.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

applovin_max, flutter

More

Packages that depend on nefta_sdk_flutter

Packages that implement nefta_sdk_flutter