Flutter广告展示插件ourads的使用

发布于 1周前 作者 sinazl 来自 Flutter

以下是关于“Flutter广告展示插件ourads的使用”的详细内容和完整示例Demo:

Flutter广告展示插件ourads的使用

关于OurAds

OurAds 插件旨在帮助开发者解决编码过程中可能遇到的问题,并且通过减少重复输入来提高开发效率。OurAds 遵循三大原则:性能、生产力和组织性

  • 性能:OurAds 专注于性能优化和资源最小化消耗。
  • 生产力:OurAds 提供简单且愉悦的语法。
  • 组织性:OurAds 可以轻松地组织你的小部件和代码。

此外,为了进一步提高您的生产力,我们还提供了其他包和广告管理设置。

平台支持

Android iOS MacOS Web
✔️ ✔️

添加依赖

pubspec.yaml 文件中添加以下依赖项:

dependencies:
  ourads: 0.1.1

在使用该库的文件中导入:

import 'package:ourads/ourads.dart';

OurAds初始化

main() 函数中初始化 OurAds,确保在创建 MaterialApp 之前执行此操作:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await OurAds(
    iOSAdId: IOSAdId(
      appOpenId: "Place here your iOS AppOpen Ad Id",
      bannerId: "Place here your iOS  Banner Ad Id",
      interstitialId: "Place here your iOS Interstitial Ad Id",
      interstitialVideoId: "Place here your iOS Interstitial Video Ad Id",
      rewardedId: "Place here your iOS Rewarded  Ad Id",
      rewardedInterstitialId: "Place here your iOS Rewarded Interstitial Ad Id",
      nativeId: "Place here your iOS Native Advanced Ad Id",
      nativeAdvancedVideoId: "Place here your iOS Native Advanced Video Ad Id",
    ),
    androidAdId: AndroidAdId(
      appOpenId: "Place here your Android AppOpen Ad Id",
      bannerId: "Place here your Android Banner Ad Id",
      interstitialId: "Place here your Android Interstitial Ad Id",
      interstitialVideoId: "Place here your Android Interstitial Video Ad Id",
      rewardedId: "Place here your Android Rewarded  Ad Id",
      rewardedInterstitialId: "Place here your Android Rewarded Interstitial Ad Id",
      nativeId: "Place here your Android Native Ad Id",
      nativeAdvancedVideoId: "Place here your Android Native Advanced Video Ad Id",
    ),
  ).initAds(
    hasOpenAd: true,
    // 如果你不想使用开屏广告,可以将此选项设为 false
  );
  // 如果你想从高级用户中隐藏开屏广告,可以在下面的命令上添加逻辑。
  OurAds.resumeOpenAppAds();
  runApp(
    ScreenUtilInit(
      designSize: Size(360, 760),
      builder: (_, __) => MyApp(),
    ),
  );
}

iOS平台特定设置

更新 ios/Runner/Info.plist 文件以添加两个键:

  • GADApplicationIdentifier 键,其字符串值为您的 AdMob 应用程序ID(在 AdMob UI 中标识)。
  • SKAdNetworkItems 键,包含 Google 的 SKAdNetworkIdentifiercstr6suwn9.skadnetwork
<key>GADApplicationIdentifier</key>
<string>ca-app-pub-3940256099942544~1458002511</string>
<key>GADIsAdManagerApp</key>
<true/>
<key>NSUserTrackingUsageDescription</key>
<string>This identifier will be used to deliver personalized ads to you.</string>
<key>GADNativeAdValidatorEnabled</key>
<false/>
<key>SKAdNetworkItems</key>
<array>
  <dict>
    <key>SKAdNetworkIdentifier</key>
    <string>cstr6suwn9.skadnetwork</string>
  </dict>
  <!-- 添加更多 SKAdNetworkIdentifier -->
</array>

Android平台特定设置

android/app/src/main/AndroidManifest.xml 文件中添加 AdMob 应用程序ID:

<manifest>
  <application>
    <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
    <meta-data
      android:name="com.google.android.gms.ads.APPLICATION_ID"
      android:value="ca-app-pub-3940256099942544~3347511713"/>
  </application>
</manifest>

示例

开屏广告
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await OurAds().initAds(
    hasOpenAd: true,
  );
  OurAds.resumeOpenAppAds();
  runApp(
    MyApp()
  );
}
插屏广告
Scaffold(
  appBar: AppBar(
    title: Text('Interstitial Ad'),
    centerTitle: true,
  ),
  body: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
      Text(
        'Button Below will take you to the new screen and also shows you \n'
        'Interstitial Ad \n \n',
        style: TextStyle(fontSize: 20, fontWeight: FontWeight.w600),
        textAlign: TextAlign.center,
      ),
      ElevatedButton(
        onPressed: () {
          OurAds.showInterstitialAd();
          Get.toNamed(Routes.SHOW_INTERSTITIAL_AD);
        },
        child: Text('Show Interstitial Ad'),
      )
    ],
  ),
);
横幅广告
Scaffold(
  appBar: AppBar(
    title: Text('Banner Ads'),
    centerTitle: true,
  ),
  body: Column(
    children: [
      StandardBanner(), // 标准横幅广告,大小为 320x50
      LargeBanner(), // 大型横幅广告,大小为 320x100
      MediumRectangle(), // 中型矩形广告,大小为 320x250
      FullSizeBanner(), // 全尺寸横幅广告,大小为 468x60
      LeaderboardBanner(), // 领导板广告,大小为 728x90
    ],
  ),
);

横幅广告尺寸

广告类型 小部件名称 尺寸 (dp) (WxH)
标准横幅广告 StandardBanner() 320x50
大型横幅广告 LargeBanner() 320x100
中型矩形广告 MediumRectangle() 320x250
全尺寸横幅广告 FullSizeBanner() 468x60
领导板广告 LeaderboardBanner() 728x90
SmartBanner32 SmartBanner32() fullx32
SmartBanner50 SmartBanner50() fullx50
SmartBanner90 SmartBanner90() fullx90

实现实时ID

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await OurAds(
    iOSAdId: IOSAdId(
      appOpenId: "Place here your iOS AppOpen Ad Id",
      bannerId: "Place here your iOS  Banner Ad Id",
      interstitialId: "Place here your iOS Interstitial Ad Id",
      interstitialVideoId: "Place here your iOS Interstitial Video Ad Id",
      rewardedId: "Place here your iOS Rewarded  Ad Id",
      rewardedInterstitialId: "Place here your iOS Rewarded Interstitial Ad Id",
      nativeId: "Place here your iOS Native Advanced Ad Id",
      nativeAdvancedVideoId: "Place here your iOS Native Advanced Video Ad Id",
    ),
    androidAdId: AndroidAdId(
      appOpenId: "Place here your Android AppOpen Ad Id",
      bannerId: "Place here your Android Banner Ad Id",
      interstitialId: "Place here your Android Interstitial Ad Id",
      interstitialVideoId: "Place here your Android Interstitial Video Ad Id",
      rewardedId: "Place here your Android Rewarded  Ad Id",
      rewardedInterstitialId: "Place here your Android Rewarded Interstitial Ad Id",
      nativeId: "Place here your Android Native Ad Id",
      nativeAdvancedVideoId: "Place here your Android Native Advanced Video Ad Id",
    ),
  ).initAds(
    hasOpenAd: true,
    // 如果你不想使用开屏广告,可以将此选项设为 false
  );
  // 如果你想从高级用户中隐藏开屏广告,可以在下面的命令上添加逻辑。
  OurAds.resumeOpenAppAds();
  runApp(
    ScreenUtilInit(
      designSize: Size(360, 760),
      builder: (_, __) => MyApp(),
    ),
  );
}

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

1 回复

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


当然,以下是一个关于如何在Flutter项目中集成并使用ourads广告展示插件的示例代码。请注意,ourads插件的具体实现和API可能会随时间变化,因此请参考官方文档以确保准确性。

首先,确保你的Flutter环境已经配置好,并且已经创建了一个新的Flutter项目。

  1. 添加依赖

    pubspec.yaml文件中添加ourads依赖。

    dependencies:
      flutter:
        sdk: flutter
      ourads: ^latest_version  # 请替换为最新的版本号
    

    然后运行flutter pub get来安装依赖。

  2. 配置Android和iOS

    根据ourads的官方文档,你可能需要在AndroidManifest.xmlInfo.plist文件中添加一些配置。这里假设你已经有了ourads的SDK密钥和必要的配置信息。

    Android (AndroidManifest.xml):

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.yourapp">
        <!-- 其他配置 -->
        <meta-data
            android:name="com.ourads.SDK_KEY"
            android:value="YOUR_OURADS_SDK_KEY"/>
    </manifest>
    

    iOS (Info.plist):

    <dict>
        <!-- 其他配置 -->
        <key>OURADS_SDK_KEY</key>
        <string>YOUR_OURADS_SDK_KEY</string>
    </dict>
    
  3. 初始化并使用OurAds

    在你的Flutter项目中,你可以按照以下方式初始化并使用ourads插件来展示广告。

    import 'package:flutter/material.dart';
    import 'package:ourads/ourads.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'OurAds Example',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: OurAdsExample(),
        );
      }
    }
    
    class OurAdsExample extends StatefulWidget {
      @override
      _OurAdsExampleState createState() => _OurAdsExampleState();
    }
    
    class _OurAdsExampleState extends State<OurAdsExample> {
      OurAds? _ourAds;
    
      @override
      void initState() {
        super.initState();
        // 初始化OurAds
        _ourAds = OurAds(
          // 根据需要传递初始化参数
          appId: "YOUR_OURADS_APP_ID",
          adUnitId: "YOUR_OURADS_AD_UNIT_ID",
          // 其他参数...
        );
    
        // 加载广告(假设有一个加载广告的方法)
        _ourAds?.loadAd();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('OurAds Example'),
          ),
          body: Center(
            child: ElevatedButton(
              onPressed: () {
                // 显示广告(假设有一个显示广告的方法)
                _ourAds?.showAd().then((result) {
                  if (result == AdResult.loaded) {
                    print("Ad shown successfully!");
                  } else {
                    print("Failed to show ad.");
                  }
                });
              },
              child: Text('Show Ad'),
            ),
          ),
        );
      }
    
      @override
      void dispose() {
        // 释放资源
        _ourAds?.dispose();
        super.dispose();
      }
    }
    

请注意,上述代码是一个简化的示例,用于说明如何在Flutter项目中集成ourads插件。实际的OurAds类及其方法可能有所不同,因此请参考ourads插件的官方文档以获取最新的API和示例代码。

另外,由于广告SDK通常涉及敏感信息和用户隐私,务必确保你遵循了所有相关的法律法规和最佳实践。

回到顶部