fnmobi_flutter_sdk 0.0.1 copy "fnmobi_flutter_sdk: ^0.0.1" to clipboard
fnmobi_flutter_sdk: ^0.0.1 copied to clipboard

Fnmobi Flutter SDK. Simple to get

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fnmobi_flutter_sdk/event/ad_error_event.dart';
import 'package:fnmobi_flutter_sdk/event/ad_reward_event.dart';
import 'package:fnmobi_flutter_sdk/fnmobi_flutter_sdk.dart';
import 'package:fnmobi_flutter_sdk/view/ad_banner_widget.dart';
import 'package:fnmobi_flutter_sdk/view/ad_flow_widget.dart';

import 'ads_config.dart';

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

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

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          // This is the theme of your application.
          //
          // TRY THIS: Try running your application with "flutter run". You'll see
          // the application has a blue toolbar. Then, without quitting the app,
          // try changing the seedColor in the colorScheme below to Colors.green
          // and then invoke "hot reload" (save your changes or press the "hot
          // reload" button in a Flutter-supported IDE, or press "r" if you used
          // the command line to start the app).
          //
          // Notice that the counter didn't reset back to zero; the application
          // state is not lost during the reload. To reset the state, use hot
          // restart instead.
          //
          // This works for code too, not just values: Most code changes can be
          // tested with just a hot reload.
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
          useMaterial3: true,
        ),
        home: const AdsDemoPage());
  }
}

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

  @override
  State<AdsDemoPage> createState() => _AdsDemoPageState();
}

class _AdsDemoPageState extends State<AdsDemoPage> {
  String _adEvent = '';
  bool showFlow = false;

  @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('Flutter Sdk plugin'),
        ),
        body: Center(
          child: SingleChildScrollView(
            child: Column(
              children: [
                const SizedBox(height: 10),
                Text('Result: $_result'),
                const SizedBox(height: 10),
                Text('onAdEvent: $_adEvent'),
                const SizedBox(height: 20),
                ElevatedButton(
                  child: const Text('初始化'),
                  onPressed: () {
                    init();
                  },
                ),
                const SizedBox(height: 20),
                ElevatedButton(
                  child: const Text('展示信息流'),
                  onPressed: () {
                    setState(() {
                      _result = "展示信息流广告";
                      showFlow = !showFlow;
                    });
                  },
                ),
                Visibility(
                  visible: showFlow,
                  child: AdFlowWidget(
                    posId: AdsConfig.flowId,
                    show: showFlow,
                  ),
                ),
                const SizedBox(height: 20),
                ElevatedButton(
                  child: const Text('展示开屏广告'),
                  onPressed: () {
                    showSplashAd();
                    setState(() {});
                  },
                ),
                const SizedBox(height: 20),
                ElevatedButton(
                  child: const Text('展示插屏广告'),
                  onPressed: () {
                    showInterstitialAd(AdsConfig.interstitialId);
                  },
                ),
                const SizedBox(height: 20),
                ElevatedButton(
                  child: const Text('激励视频-加载并展示'),
                  onPressed: () {
                    showRewardVideoAd();
                  },
                ),
                const SizedBox(height: 20),
                ElevatedButton(
                  child: const Text('激励视频-仅加载'),
                  onPressed: () {
                    loadOnlyVideoAd();
                  },
                ),
                const SizedBox(height: 20),
                ElevatedButton(
                  child: const Text('激励视频-仅展示'),
                  onPressed: () {
                    showOnlyVideoAd();
                  },
                ),
                const SizedBox(height: 20),
                ElevatedButton(
                  child: const Text('展示全屏插屏'),
                  onPressed: () {
                    showScreenVideoAd();
                  },
                ),
                const SizedBox(height: 20),
                ElevatedButton(
                  child: const Text('内容联盟'),
                  onPressed: () {
                    showContentAlliance();
                    setState(() {});
                  },
                ),
                const SizedBox(height: 20),
                SizedBox(
                  width: 375,
                  height: 100,
                  child: AdBannerWidget(
                    posId: AdsConfig.bannerId,
                    show: true,
                  ),
                ),
                // SizedBox(height: 10),
                // Container(
                //   width: double.infinity,
                //   height: 80,
                //   child: AdBannerWidget(
                //     posId: AdsConfig.bannerId,
                //     show: true,
                //   ),
                // ),
                // Container(
                //   width: double.infinity,
                //   // height: 100,
                //   margin: EdgeInsets.all(10),
                //   // 设置比例为 6.4:1 的官方推荐比例
                //   child: AspectRatio(
                //     aspectRatio: 6.4 / 1,
                //     child: AdBannerWidget(
                //       posId: AdsConfig.bannerId,
                //       interval: 120,
                //     ),
                //   ),
                // ),
                // SizedBox(height: 20),
              ],
            ),
          ),
        ),
      ),
    );
  }

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

  /// 展示插屏广告
  Future<void> showInterstitialAd(String posId) async {
    try {
      bool result = await FnmobiFlutterSdk.showInterstitialAd(posId);
      _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 FnmobiFlutterSdk.showRewardVideoAd(
        AdsConfig.rewardVideoId,
        customData: 'showRewardVideoAd customData',
        userId: 'userId',
      );
      _result = "展示激励视频广告${result ? '成功' : '失败'}";
    } on PlatformException catch (e) {
      _result =
          "展示激励视频广告失败 code:${e.code} msg:${e.message} details:${e.details}";
    }
    setState(() {});
  }

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

  /// 仅显示激励视频广告
  Future<void> showOnlyVideoAd() async {
    try {
      bool result = await FnmobiFlutterSdk.showOnlyVideoAd();
      _result = "仅展示激励视频广告${result ? '成功' : '失败'}";
    } on PlatformException catch (e) {
      _result =
          "仅展示激励视频广告失败 code:${e.code} msg:${e.message} details:${e.details}";
    }
    setState(() {});
  }

  /// 展示全屏插屏广告
  Future<void> showScreenVideoAd() async {
    try {
      bool result =
          await FnmobiFlutterSdk.showScreenVideoAd(AdsConfig.screenVideoId);
      _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 FnmobiFlutterSdk.initAd(AdsConfig.appId, test: true, debug: true);
    _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 FnmobiFlutterSdk.showSplashAd(
      AdsConfig.splashId,
      logo: logo,
    );
    _result = "展示开屏广告${result ? '成功' : '失败'}";
  } on PlatformException catch (e) {
    _result = "展示开屏广告失败 code:${e.code} msg:${e.message} details:${e.details}";
  }
}

/// 展示内容联盟
Future<void> showContentAlliance() async {
  try {
    bool result = await FnmobiFlutterSdk.showContentAlliance(
      AdsConfig.contentAllianceId,
    );
    _result = "展示内容联盟${result ? '成功' : '失败'}";
  } on PlatformException catch (e) {
    _result = "展示内容联盟失败 code:${e.code} msg:${e.message} details:${e.details}";
  }
}
1
likes
120
points
--
downloads

Publisher

unverified uploader

Weekly Downloads

Fnmobi Flutter SDK. Simple to get

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on fnmobi_flutter_sdk

Packages that implement fnmobi_flutter_sdk