flutter 如何对int(最初是字符串)进行算术运算(增量)

wgmfuz8q  于 2022-12-19  发布在  Flutter
关注(0)|答案(3)|浏览(151)

我有一个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();
          },
        ),
      ),
    );

第一节第一节第一节第一节第一次

tez616oj

tez616oj1#

您可以将Text(reward.toString())与int reward
或(int.parse(reward)+10).toString()与字符串reward

mzsu5hc0

mzsu5hc02#

“我想通了!时间旅行!我想通了”。我们回到时间,带回Text()石头,并将String和int都弹回现实。
创建一个布尔变量。

bool clickedButton = false;

然后,使用三元运算符存储以下值之一:

  1. int变量:+10(如果为真)
    1.或字符串:“显示广告”(如果为false,则默认为)
    将文本()构件编辑为:
Text(
                clickedButton? '$_rewardedScore' : 'Show Ad',
                style: TextStyle(fontSize: 30),
              )

egmofgnx

egmofgnx3#

创建一个可为空的String类型变量,如

String? showAd;

把它当作

Text(showAd ?? "show ad");

要递增该值,请编写如下函数

showAd = watchAd(showAd)

String watchAd(String? ad){
    int _newValue = int.parse(ad ?? '0');
    _newValue+=10;
    return _newValue.toString();
}

相关问题