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

Admob Banner ad is cut off on flutter #854

Closed
patolax opened this issue May 30, 2023 · 1 comment
Closed

Admob Banner ad is cut off on flutter #854

patolax opened this issue May 30, 2023 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@patolax
Copy link

patolax commented May 30, 2023

As shown in the image admob banner ad is cut off or stretched.

If I use AdSize.banner widget does not take the entire width but shows the ad correctly. If I use full-banner the ad is stretched.

How do I get the banner to fit the full width of the screen and show the content correctly (without cut off or streached)?

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

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _loadAd();
  }

  void _loadAd() async {
    await _inlineAdaptiveAd?.dispose();
    setState(() {
      _inlineAdaptiveAd = null;
      _isLoaded = false;
    });

    // Get an inline adaptive size for the current orientation.
    AdSize size = AdSize.fullBanner;

    _inlineAdaptiveAd = BannerAd(
      adUnitId: AdHelper.bannerAdUnitId,
      size: size,
      request: const AdRequest(),
      listener: BannerAdListener(
        onAdLoaded: (Ad ad) async {
          debugPrint('Inline adaptive banner loaded: ${ad.responseInfo}');

          // After the ad is loaded, get the platform ad size and use it to
          // update the height of the container. This is necessary because the
          // height can change after the ad is loaded.
          BannerAd bannerAd = (ad as BannerAd);
          _finalSize = await bannerAd.getPlatformAdSize();
          // if (size == null) {
          //   debugPrint(
          //       'Error: getPlatformAdSize() returned null for $bannerAd');
          //   return;
          // }

          setState(() {
            _inlineAdaptiveAd = bannerAd;
            _isLoaded = true;
          });
        },
        onAdFailedToLoad: (Ad ad, LoadAdError error) {
          debugPrint('Inline adaptive banner failedToLoad: $error');
          ad.dispose();
        },
      ),
    );
    await _inlineAdaptiveAd!.load();
  }

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

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

    super.dispose();
  }
}

image

Flutter 3.10.0 • channel stable • https://github.com/flutter/flutter.git Framework • revision 84a1e904f4 (3 weeks ago) • 2023-05-09 07:41:44 -0700 Engine • revision d44b5a94c9 Tools • Dart 3.0.0 • DevTools 2.23.1

@huycozy huycozy added the in triage Issue currently being evaluated label May 30, 2023
@huycozy
Copy link
Collaborator

huycozy commented May 30, 2023

Thanks for the report! This is a known issue being tracked at #261. And you may wanna take a look at #261 (comment) for the workaround.

Closing this issue as a duplicate, so please follow up on it for further updates.

@huycozy huycozy closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2023
@huycozy huycozy added duplicate This issue or pull request already exists and removed in triage Issue currently being evaluated labels May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants