我有一个flutter的ElevatedButton
来显示rewardedAd
,它一开始显示string:'show ad'
,但是看完广告后,我想在里面显示int:'+10'
,而且我想每次点击它(看广告)时增加+10。
我的代码:
RewardedAd? _rewardedAd;
int _rewardedScore = 0;
@override
void initState() {
// TODO: implement initState
super.initState();
_createRewardedAd();
}
void _createRewardedAd() {
RewardedAd.load(
adUnitId: 'ca-app-pub-3940256099942544/5224354917',
request: AdRequest(),
rewardedAdLoadCallback: RewardedAdLoadCallback(
onAdLoaded: (RewardedAd ad) {
print('$ad loaded.');
// Keep a reference to the ad so you can show it later.
setState(() => this._rewardedAd = ad);
},
onAdFailedToLoad: (LoadAdError error) {
print('RewardedAd failed to load: $error');
setState(() {
_rewardedAd = null;
});
},
),
);
}
void _showRewardedAd() {
_rewardedAd?.fullScreenContentCallback = FullScreenContentCallback(
onAdShowedFullScreenContent: (RewardedAd ad) =>
print('$ad onAdShowedFullScreenContent.'),
onAdDismissedFullScreenContent: (RewardedAd ad) {
print('$ad onAdDismissedFullScreenContent.');
ad.dispose(); // dispose ad and
_createRewardedAd(); // then, create a new one
},
onAdFailedToShowFullScreenContent: (RewardedAd ad, AdError error) {
print('$ad onAdFailedToShowFullScreenContent: $error');
ad.dispose(); // dispose ad and
_createRewardedAd(); // then, create a new one
},
onAdImpression: (RewardedAd ad) => print('$ad impression occurred.'),
);
_rewardedAd?.show(
onUserEarnedReward: (AdWithoutView ad, RewardItem reward) {
setState(() {
_rewardedScore = _rewardedScore + 10;
});
});
_rewardedAd = null;
}
Scaffold(
appBar: AppBar(
title: Text('Rewarded: $_rewardedScore'),
centerTitle: true,
),
body: Center(
child: ElevatedButton(
child: Text(
"show ad",
style: TextStyle(fontSize: 30),
),
onPressed: () {
print('clicked rewarded');
_showRewardedAd();
},
),
),
);
第一节第一节第一节第一节第一次
3条答案
按热度按时间tez616oj1#
您可以将Text(reward.toString())与int reward
或(int.parse(reward)+10).toString()与字符串reward
mzsu5hc02#
“我想通了!时间旅行!我想通了”。我们回到时间,带回Text()石头,并将String和int都弹回现实。
创建一个布尔变量。
然后,使用三元运算符存储以下值之一:
1.或字符串:“显示广告”(如果为false,则默认为)
将文本()构件编辑为:
egmofgnx3#
创建一个可为空的String类型变量,如
把它当作
要递增该值,请编写如下函数