Flutter广告集成插件ironsource_ads_module的使用
ironsource_ads_module #
一个用于Flutter项目的插件。
开始使用 #
该项目是一个用于Flutter的插件包, 这是一个专门的包,包含针对Android和/或iOS平台的特定实现代码。
要开始使用Flutter开发,请参阅 在线文档,其中提供了教程、示例、移动开发指南和完整的API参考。
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:ironsource_ads_module/ironsource_ads_module.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _isInterstitialAdAvailable = false;
@override
void initState() {
super.initState();
// 初始化IronSource插件
IronSourceAdsModule.init();
// 监听插件事件
IronSourceAdsModule.setListener((event, args) {
if (event == ‘interstitialAdAvailabilityChanged’) {
setState(() {
_isInterstitialAdAvailable = args[‘available’];
});
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text(‘IronSource Ads Example’),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
‘Interstitial Ad Available: $_isInterstitialAdAvailable’,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
// 请求插屏广告
IronSourceAdsModule.requestInterstitialAd();
},
child: Text(‘Request Interstitial Ad’),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
// 显示插屏广告
IronSourceAdsModule.showInterstitialAd();
},
child: Text(‘Show Interstitial Ad’),
),
],
),
),
),
);
}
}
更多关于Flutter广告集成插件ironsource_ads_module的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter广告集成插件ironsource_ads_module的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
ironsource_ads_module
是一个用于在 Flutter 应用中集成 IronSource 广告的插件。它允许你在应用中展示横幅广告、插页广告、激励视频广告等。以下是如何在 Flutter 项目中使用 ironsource_ads_module
的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 ironsource_ads_module
插件的依赖:
dependencies:
flutter:
sdk: flutter
ironsource_ads_module: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 初始化 IronSource
在你的应用启动时(例如在 main.dart
中),你需要初始化 IronSource。你需要在 initState
方法中调用 IronSource.init
并传入你的 App Key。
import 'package:ironsource_ads_module/ironsource_ads_module.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
IronSource.init(appKey: 'YOUR_APP_KEY');
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('IronSource Ads Example'),
),
body: Center(
child: Text('Hello, IronSource Ads!'),
),
),
);
}
}
3. 展示广告
激励视频广告
激励视频广告通常用于奖励用户观看广告。你可以使用 IronSource.showRewardedVideo
方法来展示激励视频广告。
FlatButton(
onPressed: () async {
bool isAvailable = await IronSource.isRewardedVideoAvailable();
if (isAvailable) {
IronSource.showRewardedVideo();
} else {
print('Rewarded video not available');
}
},
child: Text('Show Rewarded Video'),
);
插页广告
插页广告通常在全屏展示,你可以使用 IronSource.showInterstitial
方法来展示插页广告。
FlatButton(
onPressed: () async {
bool isAvailable = await IronSource.isInterstitialReady();
if (isAvailable) {
IronSource.showInterstitial();
} else {
print('Interstitial not ready');
}
},
child: Text('Show Interstitial'),
);
横幅广告
横幅广告通常展示在屏幕的顶部或底部。你可以使用 IronSourceBannerAd
小部件来展示横幅广告。
IronSourceBannerAd(
placementName: 'DefaultBanner',
size: IronSourceBannerSize.BANNER,
listener: (event) {
print('Banner Ad event: $event');
},
);
4. 处理广告事件
你可以通过设置监听器来处理广告事件,例如广告加载成功、广告展示、广告关闭等。
IronSource.setRewardedVideoListener(
onRewardedVideoAdOpened: () {
print('Rewarded Video Ad Opened');
},
onRewardedVideoAdClosed: () {
print('Rewarded Video Ad Closed');
},
onRewardedVideoAvailabilityChanged: (bool available) {
print('Rewarded Video Available: $available');
},
onRewardedVideoAdRewarded: (IronSourceReward reward) {
print('Rewarded Video Ad Rewarded: ${reward.amount} ${reward.placementName}');
},
);
5. 调试和测试
在开发和测试阶段,你可以使用 IronSource 提供的测试模式来确保广告正常工作。你可以在初始化时启用测试模式:
IronSource.init(appKey: 'YOUR_APP_KEY', isTestMode: true);