flutter_mintegral 1.5.2 copy "flutter_mintegral: ^1.5.2" to clipboard
flutter_mintegral: ^1.5.2 copied to clipboard

retractedunlisted

Mintegral SDK for Android

example/lib/main.dart

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  final String appId = 'your_app_id';
  final String appKey = 'your_app_key';

  bool sdkInit = false;
  SplashAd? _splashAd;
  RewardVideoAd? _rewardVideoAd;

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

  Future<void> initSdk() async {
    try {
      await Mintegral.instance.initialize(
        appId: appId,
        appKey: appKey,
        onInitSuccess: () {
          debugPrint('[Mintegral] Initialization Complete.');
          setState(() {
            sdkInit = true;
          });
        },
        onInitFail: (error) {
          debugPrint('[Mintegral] Initialization Failed: $error');
        },
      );
    } catch (e) {
      debugPrint('[Mintegral] Initialization Failed: $e');
    }
  }

  void loadSplashAd() {
    SplashAd.load(
      placementId: 'your_splash_placement_id',
      unitId: 'your_splash_unit_id',
      adLoadCallback: SplashAdLoadCallback(
        onAdLoaded: (SplashAd ad) {
          debugPrint('[Mintegral] SplashAd loaded.');
          setState(() {
            _splashAd = ad;
          });
        },
        onAdFailedToLoad: (String error) {
          debugPrint('[Mintegral] SplashAd load failed. $error');
        },
      ),
    );
  }

  void showSplashAd() {
    if (_splashAd != null) {
      _splashAd!.splashContentCallback = SplashContentCallback(
        onAdShowedFullScreenContent: (SplashAd ad) {
          debugPrint('[Mintegral] SplashAd onAdShowedFullScreenContent.');
        },
        onAdDismissedFullScreenContent: (SplashAd ad, DismissType type) {
          debugPrint('[Mintegral] SplashAd onAdDismissedFullScreenContent. type: $type');
          ad.dispose();
          setState(() {
            _splashAd = null;
          });
        },
        onAdFailedToShowFullScreenContent: (SplashAd ad, String error) {
          debugPrint('[Mintegral] SplashAd onAdFailedToShowFullScreenContent. $error');
          ad.dispose();
          setState(() {
            _splashAd = null;
          });
        },
        onAdClicked: (SplashAd ad) {
          debugPrint('[Mintegral] SplashAd onAdClicked.');
        },
      );
      _splashAd!.show();
    }
  }

  void loadRewardVideoAd() {
    RewardVideoAd.load(
      placementId: 'your_rewarded_placement_id',
      unitId: 'your_rewarded_unit_id',
      rewardedAdLoadCallback: RewardedAdLoadCallback(
        onAdLoaded: (RewardVideoAd ad) {
          debugPrint('[Mintegral] RewardVideoAd loaded.');
          setState(() {
            _rewardVideoAd = ad;
          });
        },
        onAdFailedToLoad: (String error) {
          debugPrint('[Mintegral] RewardVideoAd load failed. $error');
        },
      ),
    );
  }

  void showRewardVideoAd() {
    if (_rewardVideoAd != null) {
      _rewardVideoAd!.fullScreenContentCallback = FullScreenContentCallback(
        onAdShowedFullScreenContent: (RewardVideoAd ad) {
          debugPrint('[Mintegral] RewardVideoAd onAdShowedFullScreenContent.');
        },
        onAdDismissedFullScreenContent: (RewardVideoAd ad, RewardInfo rewardInfo) {
          debugPrint('[Mintegral] RewardVideoAd onAdDismissedFullScreenContent. rewardInfo: ${rewardInfo.toString()}');
          ad.dispose();
          setState(() {
            _rewardVideoAd = null;
          });
        },
        onAdFailedToShowFullScreenContent: (RewardVideoAd ad, String error) {
          debugPrint('[Mintegral] RewardVideoAd onAdFailedToShowFullScreenContent. $error');
          ad.dispose();
          setState(() {
            _rewardVideoAd = null;
          });
        },
        onAdClicked: (RewardVideoAd ad) {
          debugPrint('[Mintegral] RewardVideoAd onAdClicked.');
        },
        onAdCompleted: (RewardVideoAd ad) {
          debugPrint('[Mintegral] RewardVideoAd onAdCompleted.');
        },
        onAdEndCardShowed: (RewardVideoAd ad) {
          debugPrint('[Mintegral] RewardVideoAd onAdEndCardShowed.');
        },
      );
      _rewardVideoAd!.show();
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              child: Text(sdkInit
                  ? 'Mintegral SDK initialized - $appId'
                  : 'Initializing Mintegral SDK - $appId'),
              onPressed: sdkInit ? null : initSdk,
            ),
            ElevatedButton(
              child: Text(_splashAd == null
                  ? 'Load Splash Ad'
                  : 'Show Splash Ad'),
              onPressed: !sdkInit ? null
                  : _splashAd == null ? loadSplashAd : showSplashAd,
            ),
            ElevatedButton(
              child: Text(_rewardVideoAd == null
                  ? 'Load Reward Video Ad'
                  : 'Show Reward Video Ad'),
              onPressed: !sdkInit ? null
                  : _rewardVideoAd == null ? loadRewardVideoAd : showRewardVideoAd,
            ),
            Center(child: Text('Initialization status: $sdkInit\n')),
          ],
        ),
      ),
    );
  }
}