Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Banner ads cut off in production #1097

Closed
patolax opened this issue May 20, 2024 · 1 comment
Closed

Banner ads cut off in production #1097

patolax opened this issue May 20, 2024 · 1 comment

Comments

@patolax
Copy link

patolax commented May 20, 2024

We put a lot of effort into creating a nice UI, but AdMob ads come and destroy it.

This is from the production version. Some ads are misaligned and get cut off and the entire UI looks bad. How to fix this? my code is shown below.

image

`class AdBanner extends StatefulWidget {
  const AdBanner({super.key});

  @override
  State<AdBanner> createState() => _AdBannerState();
}

class _AdBannerState extends State<AdBanner> {
  BannerAd? _inlineAdaptiveAd;
  bool _isLoaded = false;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      var windowWidth = MediaQuery.of(context).size.width.truncate();
      _loadAd(windowWidth);
    });
  }

  void _loadAd(int windowWidth) async {
    await _inlineAdaptiveAd?.dispose();
    // Get an inline adaptive size for the current orientation.
    final AnchoredAdaptiveBannerAdSize? size =
        await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
            windowWidth);
    if (size == null) {
      debugPrint('Unable to get height of anchored banner.');
      return;
    }

    _inlineAdaptiveAd = BannerAd(
      adUnitId: AdHelper.bannerAdUnitId,
      size: size,
      request: const AdRequest(),
      listener: BannerAdListener(
        onAdLoaded: (Ad ad) async {
          debugPrint('Inline BannerAdListener onAdLoaded');
          if (mounted) {
            setState(() {
              _isLoaded = true;
            });
          }
        },
        onAdFailedToLoad: (Ad ad, LoadAdError error) {
          debugPrint('Inline adaptive banner failedToLoad: $error');
          _isLoaded = false;
          ad.dispose();
        },
      ),
    );
    await _inlineAdaptiveAd!.load();
  }

  @override
  Widget build(BuildContext context) {
    return (_isLoaded && _inlineAdaptiveAd != null)
        ? Align(
            alignment: Alignment.bottomCenter,
            child: SizedBox(
                width: _inlineAdaptiveAd!.size.width.toDouble(),
                height: _inlineAdaptiveAd!.size.height.toDouble(),
                child: AdWidget(
                  ad: _inlineAdaptiveAd!,
                )))
        : const SizedBox(
            width: 0,
            height: 0,
          );
  }

  @override
  void dispose() {
    _inlineAdaptiveAd?.dispose();

    super.dispose();
  }
}`
@malandr2
Copy link
Collaborator

Hi @patolax, the flutter plugin does not control how the content in the AdWidget is displayed. As long as you have set the widget's bounds to the your desired size, it is working as intended. The content that is displayed in the widget is most likely the source of the issue. As a next step I recommend to debug your ad unit and any other questions/debugging concerns you may have can be directed to the AdMob Developer forum to properly triage.

Closing this out. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants