Flutter广告管理插件one_admax_flutter的使用

Flutter广告管理插件one_admax_flutter的使用

概览

GitHub Release Pub Version Pub Points

one_admax_flutter 是一个用于 ONE store AdMax 广告 的 Flutter 插件。

有关更多信息,请参阅 ONE AdMax 开发者指南

开始使用

准备工作

在开始使用 ONE AdMax SDK 之前,请确保完成以下步骤:

安装
1. 添加包

运行以下命令以将包添加到项目中:

flutter pub add one_admax_flutter

或者,你也可以手动添加依赖到你的 pubspec.yaml 文件中:

dependencies:
  one_admax_flutter: ^1.0.2

更新完 pubspec.yaml 文件后,运行 flutter pub get 来安装该包。

2. 修改 build.gradle

在项目的根目录 build.gradle 文件中,添加以下 Maven 仓库:

allprojects {
  repositories {
    maven { url 'https://repo.onestore.net/repository/onestore-sdk-public' }
  }
}
3. 修改 AndroidManifest.xml

如果你的应用目标 SDK 版本为 30(OS 11)或更高版本,必须添加以下内容:

<manifest>
  ...
  <queries>
    <intent>
      <action android:name="com.onestore.iaa.intent.action.REWARD" />
    </intent>
  </queries>
  ...
  <application>
    ...
  </application>
</manifest>

使用

导入插件到 Dart 代码中:

import 'package:one_admax_flutter/one_admax_flutter.dart';

Android Proguard 规则

尽管插件已经混淆并打包为 AAR 文件,但你需要添加以下 Proguard 规则以确保正确的混淆:

-keep packagenames com.oneadmax.**
-keep class com.oneadmax.global.** { public *; }
-keep enum com.oneadmax.internal.* { *; }

注意事项

此插件使用 json_serializable 处理在底层平台层和 Dart 之间传递的许多数据结构。编辑任何序列化数据结构后,通过运行以下命令重新构建序列化器:

flutter packages pub run build_runner build --delete-conflicting-outputs

使用以下命令监视文件系统更改:

flutter packages pub run build_runner watch --delete-conflicting-outputs

完整示例

下面是一个完整的示例代码,展示了如何使用 one_admax_flutter 插件。

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:one_admax_flutter_example/router/oam_router.dart';

void main() {
  // 初始化 ProviderScope
  runApp(const ProviderScope(child: MyApp()));
}

/// 主应用类
class MyApp extends StatelessWidget {
  /// 构造函数
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    // 配置路由
    return MaterialApp.router(
      routerConfig: admaxRouter,
    );
  }
}

更多关于Flutter广告管理插件one_admax_flutter的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter广告管理插件one_admax_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


one_admax_flutter 是一个用于在 Flutter 应用中集成和管理广告的插件。它支持多种广告格式,如横幅广告、插页广告、激励视频广告等。以下是如何使用 one_admax_flutter 插件的基本步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 one_admax_flutter 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  one_admax_flutter: ^1.0.0 # 请使用最新版本

然后运行 flutter pub get 来获取依赖。

2. 初始化插件

在你的 Flutter 应用启动时,初始化 one_admax_flutter 插件。通常可以在 main.dart 文件中的 main 函数中进行初始化:

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化 OneAdMax
  await OneAdMax.init(
    appKey: 'YOUR_APP_KEY', // 替换为你的 App Key
  );

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

3. 加载和显示广告

one_admax_flutter 支持多种广告类型,以下是加载和显示横幅广告和插页广告的示例。

横幅广告

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

class BannerAdExample extends StatefulWidget {
  [@override](/user/override)
  _BannerAdExampleState createState() => _BannerAdExampleState();
}

class _BannerAdExampleState extends State<BannerAdExample> {
  BannerAd? _bannerAd;

  [@override](/user/override)
  void initState() {
    super.initState();
    _loadBannerAd();
  }

  void _loadBannerAd() {
    _bannerAd = BannerAd(
      adUnitId: 'YOUR_BANNER_AD_UNIT_ID', // 替换为你的 Banner Ad Unit ID
      size: BannerSize.BANNER,
      listener: BannerAdListener(
        onAdLoaded: (ad) {
          print('Banner Ad loaded');
        },
        onAdFailedToLoad: (ad, error) {
          print('Banner Ad failed to load: $error');
        },
        onAdClicked: (ad) {
          print('Banner Ad clicked');
        },
      ),
    );
    _bannerAd?.load();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Banner Ad Example'),
      ),
      body: Center(
        child: _bannerAd != null
            ? AdWidget(ad: _bannerAd!)
            : Text('Loading Banner Ad...'),
      ),
    );
  }

  [@override](/user/override)
  void dispose() {
    _bannerAd?.dispose();
    super.dispose();
  }
}

插页广告

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

class InterstitialAdExample extends StatefulWidget {
  [@override](/user/override)
  _InterstitialAdExampleState createState() => _InterstitialAdExampleState();
}

class _InterstitialAdExampleState extends State<InterstitialAdExample> {
  InterstitialAd? _interstitialAd;

  [@override](/user/override)
  void initState() {
    super.initState();
    _loadInterstitialAd();
  }

  void _loadInterstitialAd() {
    InterstitialAd.load(
      adUnitId: 'YOUR_INTERSTITIAL_AD_UNIT_ID', // 替换为你的 Interstitial Ad Unit ID
      request: AdRequest(),
      adLoadCallback: InterstitialAdLoadCallback(
        onAdLoaded: (ad) {
          setState(() {
            _interstitialAd = ad;
          });
          print('Interstitial Ad loaded');
        },
        onAdFailedToLoad: (error) {
          print('Interstitial Ad failed to load: $error');
        },
      ),
    );
  }

  void _showInterstitialAd() {
    if (_interstitialAd != null) {
      _interstitialAd?.show();
    } else {
      print('Interstitial Ad not loaded yet');
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Interstitial Ad Example'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: _showInterstitialAd,
          child: Text('Show Interstitial Ad'),
        ),
      ),
    );
  }

  [@override](/user/override)
  void dispose() {
    _interstitialAd?.dispose();
    super.dispose();
  }
}
回到顶部