how do i set the button on the first press, it shown the ad, after closing the ad its still back to the previous menu and the second tap, it load other action.
thanks in advance.
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);
},
),
),
);
}
CodePudding user response:
Try including your IconButton inside a StatefulWidget and inside the StatefulWidget, keep the record of whether the ad has been already displayed or not, before navigating to the actual action as below: Your function:
void showPopUpButton(BuildContext context) {
showDialog(
context: context,
builder: (context) => Dialog(
backgroundColor: Colors.transparent,
child: DialogButtonWidget(),
),
);
}
The 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);
}
},
);
}
}