如何在 flutter dart 中每 20 秒显示一次插页式广告
How to show interstitial ads evry 20 seconds in flutter dart
我在使用 flutter 实现插页式定时广告时遇到了问题。我已经实施了一个选项,该选项在单击按钮时显示广告,但我想实施它每 20 次显示一次广告的位置 seconds.please 帮助
我正在使用我在网上找到的下面的代码
import 'package:flutter/material.dart';
import 'package:firebase_admob/firebase_admob.dart';
const String testDevice = 'MobileId';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
testDevices: testDevice != null ? <String>[testDevice] : null,
nonPersonalizedAds: true,
keywords: <String>['Game', 'Mario'],
);
BannerAd _bannerAd;
InterstitialAd _interstitialAd;
BannerAd createBannerAd() {
return BannerAd(
adUnitId: BannerAd.testAdUnitId,
//Change BannerAd adUnitId with Admob ID
size: AdSize.banner,
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
print("BannerAd $event");
});
}
InterstitialAd createInterstitialAd() {
return InterstitialAd(
adUnitId: InterstitialAd.testAdUnitId,
//Change Interstitial AdUnitId with Admob ID
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
print("IntersttialAd $event");
});
}
@override
void initState() {
FirebaseAdMob.instance.initialize(appId: BannerAd.testAdUnitId);
//Change appId With Admob Id
_bannerAd = createBannerAd()
..load()
..show();
super.initState();
}
@override
void dispose() {
_bannerAd.dispose();
_interstitialAd.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(),
home: Scaffold(
appBar: AppBar(
title: Text("Demo App"),
),
body: Center(
child: RaisedButton(
child: Text('Click on Ads'),
onPressed: () {
createInterstitialAd()
..load()
..show();
},
)),
),
);
}
}
我正在使用 flutter 语言,在线教程还不多,因为社区仍在发展
尝试将以下内容添加到您的代码中:
import 'dart:async'; // <-- put it on very top of your file
Timer _timerForInter; // <- Put this line on top of _MyAppState class
@override
void initState() {
// Add these lines to launch timer on start of the app
_timerForInter = Timer.periodic(Duration(seconds: 20), (result) {
_interstitialAd = createInterstitialAd()..load();
});
super.initState();
}
@override
void dispose() {
// Add these to dispose to cancel timer when user leaves the app
_timerForInter.cancel();
_interstitialAd.dispose();
super.dispose();
}
我在使用 flutter 实现插页式定时广告时遇到了问题。我已经实施了一个选项,该选项在单击按钮时显示广告,但我想实施它每 20 次显示一次广告的位置 seconds.please 帮助
我正在使用我在网上找到的下面的代码
import 'package:flutter/material.dart';
import 'package:firebase_admob/firebase_admob.dart';
const String testDevice = 'MobileId';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
testDevices: testDevice != null ? <String>[testDevice] : null,
nonPersonalizedAds: true,
keywords: <String>['Game', 'Mario'],
);
BannerAd _bannerAd;
InterstitialAd _interstitialAd;
BannerAd createBannerAd() {
return BannerAd(
adUnitId: BannerAd.testAdUnitId,
//Change BannerAd adUnitId with Admob ID
size: AdSize.banner,
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
print("BannerAd $event");
});
}
InterstitialAd createInterstitialAd() {
return InterstitialAd(
adUnitId: InterstitialAd.testAdUnitId,
//Change Interstitial AdUnitId with Admob ID
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
print("IntersttialAd $event");
});
}
@override
void initState() {
FirebaseAdMob.instance.initialize(appId: BannerAd.testAdUnitId);
//Change appId With Admob Id
_bannerAd = createBannerAd()
..load()
..show();
super.initState();
}
@override
void dispose() {
_bannerAd.dispose();
_interstitialAd.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(),
home: Scaffold(
appBar: AppBar(
title: Text("Demo App"),
),
body: Center(
child: RaisedButton(
child: Text('Click on Ads'),
onPressed: () {
createInterstitialAd()
..load()
..show();
},
)),
),
);
}
}
我正在使用 flutter 语言,在线教程还不多,因为社区仍在发展
尝试将以下内容添加到您的代码中:
import 'dart:async'; // <-- put it on very top of your file
Timer _timerForInter; // <- Put this line on top of _MyAppState class
@override
void initState() {
// Add these lines to launch timer on start of the app
_timerForInter = Timer.periodic(Duration(seconds: 20), (result) {
_interstitialAd = createInterstitialAd()..load();
});
super.initState();
}
@override
void dispose() {
// Add these to dispose to cancel timer when user leaves the app
_timerForInter.cancel();
_interstitialAd.dispose();
super.dispose();
}