googleads-mobile-flutter icon indicating copy to clipboard operation
googleads-mobile-flutter copied to clipboard

[Android]The WebView flickers when the advertisement is refreshed.

Open yo1106 opened this issue 11 months ago • 2 comments

Plugin Version

flutter: 3.19.2 google_mobile_ads: 4.0.0 webview_flutter: 4.7.0

platform is android.

Steps to Reproduce

  1. WebView and AdMobBanner setup same widget.
  2. Reload AdMobBanner.

admob

class WebViewWithAd extends StatefulWidget {
  @override
  _WebViewWithAdState createState() => _WebViewWithAdState();
}

class _WebViewWithAdState extends State<WebViewWithAd> {
  late BannerAd _bannerAd;
  bool _isBannerAdReady = false;
  final WebViewController _controllerWeb1 = WebViewController();
  final WebViewController _controllerWeb2 = WebViewController();

  @override
  void initState() {
    super.initState();
    _reloadAd();
  }

  @override
  void dispose() {
    _bannerAd.dispose();
    super.dispose();
  }

  void _reloadAd() {
    setState(() {
      _isBannerAdReady = false;
      _bannerAd = BannerAd(
        adUnitId: 'ca-app-pub-3940256099942544/6300978111',
        size: AdSize.banner,
        request: AdRequest(),
        listener: BannerAdListener(
          onAdLoaded: (_) {
            setState(() {
              _isBannerAdReady = true;
            });
          },
          onAdFailedToLoad: (ad, error) {
            print('Failed to load a banner ad: ${error.message}');
            _isBannerAdReady = false;
            ad.dispose();
          },
        ),
      )..load();
    });
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          title: Text('WebView + BannerAd'),
        ),
        body: Column(
          children: <Widget>[
            Expanded(
              child: WebViewWidget(
                controller: _controllerWeb1,
              ),
            ),
            // It doesn't happen if you comment in here
            // Expanded(
            //   child: WebViewWidget(
            //     controller: _controllerWeb2,
            //   ),
            // ),
            if (_isBannerAdReady)
              Container(
                height: _bannerAd.size.height.toDouble(),
                width: _bannerAd.size.width.toDouble(),
                child: AdWidget(ad: _bannerAd),
              ),
            ElevatedButton(
              onPressed: _reloadAd,
              child: Text('Reload Ad'),
            ),
          ],
        ),
      ),
    );
  }
}

yo1106 avatar Feb 29 '24 11:02 yo1106

same here, in my case it's not webview but google map, but it behaves the same - after the banner ad at the bottom is loaded, for tiny period of time google map widget is replaced with super-stretched ad banner as shown in the video above. Sometimes whole ad is displayed like this, sometimes only the "test ad" label, and sometimes it just flickers without anything displayed

flutter: 3.19.1 google_mobile_ads: 4.0.0 google_maps_flutter: 2.5.3 affected platform: android only

Screenshot 2024-02-29 at 18 09 18

palicka avatar Feb 29 '24 17:02 palicka

Hey @yo1106, thanks for sharing the sample code. I can also reproduce this. I have escalated this to the Flutter engineering team to take a closer look.

malandr2 avatar Mar 04 '24 15:03 malandr2

any updates ?

palicka avatar May 14 '24 13:05 palicka

Hi @palicka no update to share at this time as the team has been addressing p1 issues and the triage process is still ongoing.

malandr2 avatar May 14 '24 14:05 malandr2

Hello @yo1106, @palicka and anyone affected by this.

After taking a look and some debugging, it seems that the problem is related to the way Web Views are populated by Flutter. I modified the sample code given by @yo1106 to remove all ads related code and only use WebViewWidgets and I get the same results.

I opened an issue on the Flutter GitHub. You can follow progress and get more details on what I did there: https://github.com/flutter/flutter/issues/148991

LTPhantom avatar May 23 '24 19:05 LTPhantom

Closing this issue out as confirmed in https://github.com/flutter/flutter/issues/148991 it can be replicated without ads. Will reopen if necessary but please follow the Flutter issue for next steps.

malandr2 avatar May 28 '24 15:05 malandr2