Flutter广告集成插件yodomas_ads_module的使用
Flutter广告集成插件yodomas_ads_module的使用
本项目是一个新的Flutter插件包起点,该插件包包括Android和/或iOS平台特定实现代码。
开始使用
此项目是Flutter插件包的起点,包含Android和/或iOS平台的特定实现代码。若要开始使用Flutter开发,可查看以下资源:
- 在线文档,其中包含教程、示例、移动开发指南以及完整的API参考。
yodomas_ads_module_example
演示如何使用yodomas_ads_module
插件。
开始使用
此项目是Flutter应用的起点。如果你是第一次使用Flutter项目,这里有一些资源可以帮你起步:
若要开始使用Flutter开发,可查看以下资源:
- 在线文档,其中包含教程、示例、移动开发指南以及完整的API参考。
完整示例代码
下面是使用yodomas_ads_module
插件的完整示例代码。
import 'package:flutter/material.dart';
import 'package:yodomas_ads_module/yodomas_ads_module.dart'; // 导入插件包
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('广告集成示例'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
// 初始化广告模块
YodomasAdsModule.initAd();
// 显示横幅广告
YodomasAdsModule.showBannerAd();
},
child: Text('显示横幅广告'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
// 显示插屏广告
YodomasAdsModule.showInterstitialAd();
},
child: Text('显示插屏广告'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
// 显示激励视频广告
YodomasAdsModule.showRewardedVideoAd();
},
child: Text('显示激励视频广告'),
),
],
),
),
),
);
}
}
代码说明
-
导入插件:
import 'package:yodomas_ads_module/yodomas_ads_module.dart';
-
初始化广告模块:
YodomasAdsModule.initAd();
在调用任何广告显示方法之前,需要先调用
initAd()
来初始化广告模块。 -
显示横幅广告:
YodomasAdsModule.showBannerAd();
调用
showBannerAd()
方法来显示横幅广告。 -
显示插屏广告:
YodomasAdsModule.showInterstitialAd();
调用
showInterstitialAd()
方法来显示插屏广告。 -
显示激励视频广告:
YodomasAdsModule.showRewardedVideoAd();
更多关于Flutter广告集成插件yodomas_ads_module的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter广告集成插件yodomas_ads_module的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
yodomas_ads_module
是一个用于在 Flutter 应用中集成广告的插件。它支持多种广告格式,如横幅广告、插页式广告、奖励视频广告等。以下是使用 yodomas_ads_module
的基本步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 yodomas_ads_module
依赖:
dependencies:
flutter:
sdk: flutter
yodomas_ads_module: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 初始化广告模块
在 main.dart
或其他合适的地方初始化广告模块。通常,你需要在应用启动时进行初始化。
import 'package:flutter/material.dart';
import 'package:yodomas_ads_module/yodomas_ads_module.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化广告模块
await YodomasAdsModule.initialize(
appId: 'YOUR_APP_ID', // 替换为你的应用ID
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Ads Demo',
home: HomeScreen(),
);
}
}
3. 加载和显示广告
根据你需要的广告类型,加载和显示广告。以下是几种常见广告类型的示例:
横幅广告
import 'package:flutter/material.dart';
import 'package:yodomas_ads_module/yodomas_ads_module.dart';
class HomeScreen extends StatefulWidget {
[@override](/user/override)
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
BannerAd? _bannerAd;
[@override](/user/override)
void initState() {
super.initState();
_loadBannerAd();
}
void _loadBannerAd() {
_bannerAd = BannerAd(
adUnitId: 'YOUR_BANNER_AD_UNIT_ID', // 替换为你的横幅广告单元ID
size: AdSize.banner,
listener: BannerAdListener(
onAdLoaded: (Ad ad) {
print('Banner Ad Loaded');
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
print('Banner Ad Failed to Load: $error');
},
),
)..load();
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Banner Ad Example'),
),
body: Column(
children: [
Expanded(
child: Center(
child: Text('Content'),
),
),
if (_bannerAd != null)
Container(
alignment: Alignment.center,
child: AdWidget(ad: _bannerAd!),
width: _bannerAd!.size.width.toDouble(),
height: _bannerAd!.size.height.toDouble(),
),
],
),
);
}
[@override](/user/override)
void dispose() {
_bannerAd?.dispose();
super.dispose();
}
}
插页式广告
import 'package:flutter/material.dart';
import 'package:yodomas_ads_module/yodomas_ads_module.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', // 替换为你的插页式广告单元ID
request: AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (InterstitialAd ad) {
_interstitialAd = ad;
print('Interstitial Ad Loaded');
},
onAdFailedToLoad: (LoadAdError 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();
}
}
奖励视频广告
import 'package:flutter/material.dart';
import 'package:yodomas_ads_module/yodomas_ads_module.dart';
class RewardedAdExample extends StatefulWidget {
[@override](/user/override)
_RewardedAdExampleState createState() => _RewardedAdExampleState();
}
class _RewardedAdExampleState extends State<RewardedAdExample> {
RewardedAd? _rewardedAd;
[@override](/user/override)
void initState() {
super.initState();
_loadRewardedAd();
}
void _loadRewardedAd() {
RewardedAd.load(
adUnitId: 'YOUR_REWARDED_AD_UNIT_ID', // 替换为你的奖励视频广告单元ID
request: AdRequest(),
rewardedAdLoadCallback: RewardedAdLoadCallback(
onAdLoaded: (RewardedAd ad) {
_rewardedAd = ad;
print('Rewarded Ad Loaded');
},
onAdFailedToLoad: (LoadAdError error) {
print('Rewarded Ad Failed to Load: $error');
},
),
);
}
void _showRewardedAd() {
if (_rewardedAd != null) {
_rewardedAd!.show(
onUserEarnedReward: (AdWithoutView ad, RewardItem reward) {
print('User earned reward: ${reward.amount} ${reward.type}');
},
);
} else {
print('Rewarded Ad not loaded yet');
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Rewarded Ad Example'),
),
body: Center(
child: ElevatedButton(
onPressed: _showRewardedAd,
child: Text('Show Rewarded Ad'),
),
),
);
}
[@override](/user/override)
void dispose() {
_rewardedAd?.dispose();
super.dispose();
}
}