【发布时间】:2020-07-27 11:42:22
【问题描述】:
这是 myclass 的完整代码我在这里尝试做的是我正在运行一个计时器,当计时器达到 0 时它会显示一个广告,但是当应用程序关闭时,计时器也停止了无论如何计时器运行在后台以及不要停止。我试过把String _ab = '45'; 保存它的状态,但它没有工作,它只是节省了剩余的时间,但是当应用程序关闭时,计时器也停在那里,谷歌了很多但没有找到任何解决方案。在这方面的帮助会非常好
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:firebase_admob/firebase_admob.dart';
class Showads extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: CountDownTimer(),
debugShowCheckedModeBanner: false,
theme: ThemeData(
iconTheme: IconThemeData(
color: Colors.white,
),
accentColor: Colors.red,
),
);
}
}
const String testDevice = 'MobileId';
class CountDownTimer extends StatefulWidget {
@override
_CountDownTimerState createState() => _CountDownTimerState();
}
String _ab = '45';
class _CountDownTimerState extends State<CountDownTimer>
with TickerProviderStateMixin {
static const MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
testDevices: testDevice != null ? <String>[testDevice] : null,
nonPersonalizedAds: true,
keywords: <String>['Game', 'Mario'],
);
InterstitialAd _interstitialAd;
RewardedVideoAd videoAd = RewardedVideoAd.instance;
bool _interstialadValue = false;
InterstitialAd createInterstitialAd() {
return InterstitialAd(
adUnitId: InterstitialAd.testAdUnitId,
//Change Interstitial AdUnitId with Admob ID
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
print("IntersttialAd $event");
});
}
int _coins =0;
@override
void initState() {
FirebaseAdMob.instance.initialize(appId: FirebaseAdMob.testAppId);
//Change appId With Admob Id
videoAd.listener =
(RewardedVideoAdEvent event, {String rewardType, int rewardAmount}) {
print("REWARDED VIDEO AD $event");
if (event == RewardedVideoAdEvent.rewarded) {
setState(() {
_coins += rewardAmount;
});
}
};
videoAd.load(
adUnitId: RewardedVideoAd.testAdUnitId,
targetingInfo: targetingInfo);
super.initState();
if(_ab == '00'){
setState(() {
_ab ='40';
});
}
controller = AnimationController(
vsync: this,
duration: Duration(seconds: int.parse(_ab)),
);
}
@override
void dispose() {
if(_ab == '00'){
setState(() {
_ab ='40';
});
}
print(timerString);
print(timerString.substring(2, 4));
setState(() => _ab = timerString.substring(2, 4));
print(_ab);
setState(() => controller);
_CountDownTimerState().dispose();
controller.dispose();
if(_interstialadValue){
_interstitialAd.dispose();
}
super.dispose();
}
AnimationController controller;
String get timerString {
Duration duration = controller.duration * controller.value;
return '${duration.inMinutes}:${(duration.inSeconds % 60).toString().padLeft(2, '0')}';
}
@override
Widget build(BuildContext context) {
ThemeData themeData = Theme.of(context);
return Scaffold(
backgroundColor: Colors.black,
body: AnimatedBuilder(
animation: controller,
builder: (context, child) {
return Stack(
children: <Widget>[
Align(
alignment: Alignment.bottomCenter,
child:
Container(
color: Colors.amber,
height:
controller.value * MediaQuery.of(context).size.height,
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: Align(
alignment: FractionalOffset.center,
child: AspectRatio(
aspectRatio: 1.0,
child: Stack(
children: <Widget>[
Positioned.fill(
child: CustomPaint(
painter: CustomTimerPainter(
animation: controller,
backgroundColor: Colors.white,
color: themeData.indicatorColor,
)),
),
Align(
alignment: FractionalOffset.center,
child: Column(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
crossAxisAlignment:
CrossAxisAlignment.center,
children: <Widget>[
Text(
"Next AD Timer ",
style: TextStyle(
fontSize: 20.0,
color: Colors.white),
),
Text(
"Minimize App \n Do not close",
style: TextStyle(
fontSize: 20.0,
color: Colors.white),
),
Text(
timerString,
style: TextStyle(
fontSize: 112.0,
color: Colors.white),
),
],
),
),
],
),
),
),
),
AnimatedBuilder(
animation: controller,
builder: (context, child) {
return FloatingActionButton.extended(
onPressed: () {
if (controller.isAnimating) {
//controller.stop();
createInterstitialAd()
..load()
..show();
setState(() {
_interstialadValue = true;
print(controller.value);
print(timerString);
});
}
else {
// videoAd.show();
showRewardedAd();
// Checkout();
controller.reverse(
from: controller.value == 0.0
? 1.0
: controller.value);
}
},
icon: Icon(controller.isAnimating
? Icons.pause
: Icons.play_arrow),
label: Text(
controller.isAnimating ? "WAIT ADS LOADING" : "WATCH AD"));
}),
],
),
),
],
);
}),
);
}
void showRewardedAd() async {
try {
await videoAd.load(
adUnitId: RewardedVideoAd.testAdUnitId,
targetingInfo: targetingInfo);;
await videoAd.show();
}
catch(e){
print(e);
}
}
}
class CustomTimerPainter extends CustomPainter {
CustomTimerPainter({
this.animation,
this.backgroundColor,
this.color,
}) : super(repaint: animation);
final Animation<double> animation;
final Color backgroundColor, color;
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..color = backgroundColor
..strokeWidth = 10.0
..strokeCap = StrokeCap.butt
..style = PaintingStyle.stroke;
canvas.drawCircle(size.center(Offset.zero), size.width / 2.0, paint);
paint.color = color;
double progress = (1.0 - animation.value) * 2 * math.pi;
canvas.drawArc(Offset.zero & size, math.pi * 1.5, -progress, false, paint);
}
@override
bool shouldRepaint(CustomTimerPainter old) {
return animation.value != old.animation.value ||
color != old.color ||
backgroundColor != old.backgroundColor;
}
}
【问题讨论】:
标签: flutter