adster_flutter_sdk 1.0.3 copy "adster_flutter_sdk: ^1.0.3" to clipboard
adster_flutter_sdk: ^1.0.3 copied to clipboard

SDK for flutter consumers

example/lib/main.dart

import 'package:adster_flutter_sdk/adster_flutter_sdk.dart';
import 'package:adster_flutter_sdk/core/adster_constants.dart';
import 'package:adster_flutter_sdk/native/adster_native_ad_callback.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:toastification/toastification.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());

  ///ToastificationWrapper (optional)
  ///Just to toast the click
  runApp(ToastificationWrapper(child: const MyApp()));
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  ///keeping it global to prevent it from reload again & again
  AdsterRewardedAds rewardedAds = AdsterRewardedAds();
  late Future rewardAdFuture;

  ///keeping it global to prevent it from reload again & again
  AdsterInterstitialAds interstitialAds = AdsterInterstitialAds();
  late Future interstitialAdFuture;

  @override
  void initState() {
    super.initState();
    rewardAdFuture = rewardedAds.loadRewardedAd(
      adPlacementName: "adster_rewarded_test",
      callback: getRewardedAdCallback(),
    );
    interstitialAdFuture = interstitialAds.loadInterstitialAd(
      adPlacementName: "adster_interstitial_test",
      callback: getInterstitialAdCallback(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Plugin example app')),
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              AdsterBannerAd(
                adPlacementName: "adster_banner_300x250",
                adSize: AdsterAdSize.medium,
                loadingWidget: SizedBox(
                  width: AdsterAdSize.medium.width,
                  height: AdsterAdSize.medium.height,
                  child: Card(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        CircularProgressIndicator(),
                        SizedBox(height: 10),
                        Text("Loading...."),
                      ],
                    ),
                  ),
                ),
                onFailure: (AdsterAdsException error) {
                  return Text("Banner not loaded: ${error.message}");
                },
              ),
              SizedBox(height: 10),
              AdsterBannerAd(
                adPlacementName: "adster_banner_320x50",
                adSize: AdsterAdSize.small,
                loadingWidget: SizedBox(
                  width: AdsterAdSize.small.width,
                  height: AdsterAdSize.small.height,
                  child: Card(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        CircularProgressIndicator(),
                        SizedBox(width: 10),
                        Text("Loading...."),
                      ],
                    ),
                  ),
                ),
                onFailure: (AdsterAdsException error) {
                  return Text("Banner not loaded: ${error.message}");
                },
              ),
              Card(
                margin: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                child: AdsterNativeAd(
                  adPlacementName: "adster_native_test",
                  onAdLoaded: (value, widget, clickHandler) {
                    return SizedBox(
                      height: 200,
                      child: Row(
                        children: [
                          SizedBox(width: 10),
                          Expanded(child: widget),
                          Expanded(
                            child: Padding(
                              padding: const EdgeInsets.symmetric(
                                horizontal: 10,
                              ),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: [
                                  SizedBox(height: 10),
                                  Row(
                                    children: [
                                      CachedNetworkImage(
                                        imageUrl: value.imageUrl ?? "",
                                        placeholder:
                                            (context, url) =>
                                                CircularProgressIndicator(),
                                        errorWidget:
                                            (context, url, error) =>
                                                Icon(Icons.error),
                                      ),
                                      SizedBox(width: 10),
                                      Flexible(
                                        child: InkWell(
                                          onTap: () {
                                            clickHandler.call(
                                              AdsterNativeAdClickComponent
                                                  .headline,
                                            );
                                          },
                                          child: Text(
                                            value.headLine ?? "",
                                            maxLines: 2,
                                            style: TextStyle(
                                              fontWeight: FontWeight.bold,
                                            ),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                  SizedBox(height: 5),
                                  InkWell(
                                    onTap: () {
                                      clickHandler.call(
                                        AdsterNativeAdClickComponent.body,
                                      );
                                    },
                                    child: Text(value.body ?? ""),
                                  ),
                                  SizedBox(height: 5),
                                  MaterialButton(
                                    onPressed: () {
                                      clickHandler.call(
                                        AdsterNativeAdClickComponent
                                            .callToAction,
                                      );
                                    },
                                    color: Colors.grey,
                                    child: Text(value.callToAction ?? ""),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    );
                  },
                  onFailure: (AdsterAdsException error) {
                    return Text(error.message ?? "");
                  },
                  clickCallback: getNativeAdCallback(),
                ),
              ),
              FutureBuilder(
                future: rewardAdFuture,
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    if (!(snapshot.data ?? true)) {
                      return Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text("Ads not loaded"),
                      );
                    } else {
                      return ElevatedButton(
                        onPressed: () {
                          rewardedAds.showRewardedAd();
                        },
                        child: Text("Show Rewarded Ad"),
                      );
                    }
                  } else if (snapshot.hasError) {
                    if (snapshot.error is Exception) {
                      return Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          "Rewarded Ads not loaded: ${(snapshot.error as PlatformException).message}",
                        ),
                      );
                    }
                    return Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Text("${snapshot.error}"),
                    );
                  }
                  return CircularProgressIndicator();
                },
              ),
              SizedBox(height: 5),
              FutureBuilder(
                future: interstitialAdFuture,
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    if (!(snapshot.data ?? true)) {
                      return Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text("Ads not loaded"),
                      );
                    } else {
                      return ElevatedButton(
                        onPressed: () {
                          interstitialAds.showInterstitialAd();
                        },
                        child: Text("Show Interstitial Ad"),
                      );
                    }
                  } else if (snapshot.hasError) {
                    if (snapshot.error is Exception) {
                      return Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          "Interstitial Ads not loaded: ${(snapshot.error as PlatformException).message}",
                        ),
                      );
                    }
                    return Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Text("${snapshot.error}"),
                    );
                  }
                  return CircularProgressIndicator();
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  AdsterNativeAdCallback getNativeAdCallback() {
    return AdsterNativeAdCallback(
      onAdClicked: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("NativeAd:onAdClicked"),
        );
      },
      onAdImpression: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("NativeAd:onAdImpression"),
        );
      },
      onFailure: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("NativeAd:onFailure"),
        );
      },
      onNativeAdLoaded: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("NativeAd:onNativeAdLoaded"),
        );
      },
    );
  }

  AdsterInterstitialAdsCallback getInterstitialAdCallback() {
    return AdsterInterstitialAdsCallback(
      onInterstitialAdLoaded: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("InterstitialAd:onInterstitialAdLoaded"),
        );
      },
      onAdClicked: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("InterstitialAd:onAdClicked"),
        );
      },
      onAdImpression: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("InterstitialAd:onAdImpression"),
        );
      },
      onAdOpened: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("InterstitialAd:onAdOpened"),
        );
      },
      onAdClosed: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("InterstitialAd:onAdClosed"),
        );
      },
      onFailure: () {
        toastification.show(
          type: ToastificationType.error,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("InterstitialAd:onFailure"),
        );
      },
    );
  }

  AdsterRewardedAdCallback getRewardedAdCallback() {
    return AdsterRewardedAdCallback(
      onRewardedAdLoaded: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("RewardedAd:onRewardedAdLoaded"),
        );
      },
      onAdClicked: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("RewardedAd:onAdClicked"),
        );
      },
      onAdImpression: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("RewardedAd:onAdImpression"),
        );
      },
      onUserEarnedReward: (rewardAmount) {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("RewardedAd:onUserEarnedReward: \$$rewardAmount"),
        );
      },
      onVideoComplete: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("RewardedAd:onVideoComplete"),
        );
      },
      onVideoClosed: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("RewardedAd:onVideoClosed"),
        );
      },
      onFailure: () {
        toastification.show(
          type: ToastificationType.error,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("RewardedAd:onFailure"),
        );
      },
      onVideoStart: () {
        toastification.show(
          type: ToastificationType.success,
          alignment: Alignment.bottomCenter,
          style: ToastificationStyle.fillColored,
          title: Text("RewardedAd:onVideoStart"),
        );
      },
    );
  }
}