flutter_qq_ads 2.0.1 copy "flutter_qq_ads: ^2.0.1" to clipboard
flutter_qq_ads: ^2.0.1 copied to clipboard

outdated

FlutterAds 致力于构建优质的 Flutter 广告插件,这是其中一款基于「腾讯广告、广点通、优量汇」SDK 的插件

example/lib/main.dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_qq_ads/flutter_qq_ads.dart';
import 'ads_config.dart';

// 结果信息
String _result = '';

void main() {
  /// 绑定引擎
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _adEvent = '';

  @override
  void initState() {
    super.initState();
    init().then((value) {
      if (value) {
        showSplashAd(AdsConfig.logo);
      }
    });
    setAdEvent();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('FlutterAds QQ plugin'),
        ),
        body: Center(
          child: Column(
            children: [
              SizedBox(height: 10),
              Text('Result: $_result'),
              SizedBox(height: 10),
              Text('onAdEvent: $_adEvent'),
              SizedBox(height: 20),
              ElevatedButton(
                child: Text('初始化'),
                onPressed: () {
                  init();
                },
              ),
              SizedBox(height: 20),
              ElevatedButton(
                child: Text('请求应用跟踪透明度授权(仅 iOS)'),
                onPressed: () {
                  requestIDFA();
                },
              ),
              SizedBox(height: 20),
              ElevatedButton(
                child: Text('展示开屏广告(Logo2)'),
                onPressed: () {
                  showSplashAd(AdsConfig.logo2);
                },
              ),
              SizedBox(height: 20),
              ElevatedButton(
                child: Text('展示开屏广告(全屏)'),
                onPressed: () {
                  showSplashAd();
                  setState(() {});
                },
              ),
              SizedBox(height: 20),
              ElevatedButton(
                child: Text('展示插屏广告'),
                onPressed: () {
                  showInterstitialAd();
                },
              ),
              SizedBox(height: 20),
              ElevatedButton(
                child: Text('展示激励视频广告'),
                onPressed: () {
                  showRewardVideoAd();
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// 设置广告监听
  Future<void> setAdEvent() async {
    setState(() {
      _adEvent = '设置成功';
    });
    FlutterQqAds.onEventListener((event) {
      _adEvent = 'adId:${event.adId} action:${event.action}';
      if (event is AdErrorEvent) {
        // 错误事件
        _adEvent += ' errCode:${event.errCode} errMsg:${event.errMsg}';
      } else if (event is AdRewardEvent) {
        // 激励事件
        _adEvent +=
            ' transId:${event.transId} customData:${event.customData} userId:${event.userId}';
      }
      print('onEventListener:$_adEvent');
      setState(() {});
    });
  }

  /// 请求应用跟踪透明度授权
  Future<void> requestIDFA() async {
    bool result = await FlutterQqAds.requestIDFA;
    _adEvent = '请求广告标识符:$result';
    setState(() {});
  }

  /// 展示插屏广告
  Future<void> showInterstitialAd() async {
    try {
      bool result = await FlutterQqAds.showInterstitialAd(
        AdsConfig.interstitialId,
        showPopup: false,
        autoPlayMuted: false,
        autoPlayOnWifi: false,
        detailPageMuted: false,
      );
      _result = "展示插屏广告${result ? '成功' : '失败'}";
    } on PlatformException catch (e) {
      _result = "展示插屏广告失败 code:${e.code} msg:${e.message} details:${e.details}";
    }
    setState(() {});
  }

  /// 展示激励视频广告
  Future<void> showRewardVideoAd() async {
    try {
      bool result = await FlutterQqAds.showRewardVideoAd(
        AdsConfig.rewardVideoId,
        playMuted: false,
        customData: 'customData',
        userId: 'userId',
      );
      _result = "展示激励视频广告${result ? '成功' : '失败'}";
    } on PlatformException catch (e) {
      _result =
          "展示激励视频广告失败 code:${e.code} msg:${e.message} details:${e.details}";
    }
    setState(() {});
  }
}

/// 初始化广告 SDK
Future<bool> init() async {
  try {
    bool result = await FlutterQqAds.initAd(AdsConfig.appId);
    _result = "广告SDK 初始化${result ? '成功' : '失败'}";
    return result;
  } on PlatformException catch (e) {
    _result =
        "广告SDK 初始化失败 code:${e.code} msg:${e.message} details:${e.details}";
  }
  return false;
}

/// 展示开屏广告
/// [logo] 展示如果传递则展示logo,不传递不展示
Future<void> showSplashAd([String? logo]) async {
  try {
    bool result = await FlutterQqAds.showSplashAd(AdsConfig.splashId, logo);
    _result = "展示开屏广告${result ? '成功' : '失败'}";
  } on PlatformException catch (e) {
    _result = "展示开屏广告失败 code:${e.code} msg:${e.message} details:${e.details}";
  }
}
21
likes
0
pub points
65%
popularity

Publisher

unverified uploader

FlutterAds 致力于构建优质的 Flutter 广告插件,这是其中一款基于「腾讯广告、广点通、优量汇」SDK 的插件

Homepage

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_qq_ads