flutter 我如何设置按钮播放间隙广告第一,而不是它与其他行动同步

hl0ma9xz  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(165)

我如何设置按钮的第一次按下,它显示的广告,关闭广告后,它仍然回到上一个菜单和第二次点击,它加载其他行动。
先谢谢你了。

void showPopUpButton(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => Dialog(
        backgroundColor: Colors.transparent,
        child: IconButton(
          icon: Image.asset('lib/images/last.png'),
          iconSize: 260,
          onPressed: () async {
            _showInterstitialAd();
            setState(() {});
            initRandomImage();
            Navigator.pop(context);

            final player = AudioPlayer();
            await player.play(AssetSource('Sound1.wav'),
                volume: 1.0);
          },
        ),
      ),
    );
  }
0sgqnhkj

0sgqnhkj1#

尝试将IconButton包含在StatefulWidget中,并在StatefulWidget中记录广告是否已显示,然后导航到实际操作,如下所示:您的职能:

void showPopUpButton(BuildContext context) {
  showDialog(
    context: context,
    builder: (context) => Dialog(
      backgroundColor: Colors.transparent,
      child: DialogButtonWidget(),
    ),
  );
}

对话框按钮小部件:

class DialogButtonWidget extends StatefulWidget {
  const DialogButtonWidget({Key? key}) : super(key: key);

  @override
  State<DialogButtonWidget> createState() => _DialogButtonWidgetState();
}

class _DialogButtonWidgetState extends State<DialogButtonWidget> {
  bool _interstitialAdDisplayed = false;

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: Image.asset('lib/images/last.png'),
      iconSize: 260,
      onPressed: () async {
        if (!_interstitialAdDisplayed) {
          _showInterstitialAd();
          setState(() {
            _interstitialAdDisplayed = true;
          });
        } else {
          initRandomImage();
          Navigator.pop(context);
          final player = AudioPlayer();
          await player.play(AssetSource('Sound1.wav'), volume: 1.0);
        }
      },
    );
  }
}

相关问题