admin管理员组

文章数量:1290959

I'm trying to implement a collapsible banner ad in my Flutter app using Google AdMob. However, the ad does not collapse as expected, and it behaves like a regular banner ad. I have followed the documentation and set the "collapsible": "bottom" or "top" extra parameter in my ad request, but it doesn't seem to take effect.

My Code:

import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:insta/ads/consent_manager.dart';

class BannerAdWidget extends StatefulWidget {
  final String adUnitId;

  const BannerAdWidget({
    super.key,
    required this.adUnitId,
  });

  @override
  _BannerAdWidgetState createState() => _BannerAdWidgetState();
}

class _BannerAdWidgetState extends State<BannerAdWidget> {
  BannerAd? _bannerAd;
  bool _isAdLoaded = false;
  final ConsentManager _consentManager = ConsentManager.instance;

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

  void _gatherConsentAndLoadAd() {
    _consentManager.gatherConsent((FormError? error) async {
      if (error == null) {
        bool canRequestAds = await _consentManager.canRequestAds();
        if (canRequestAds) {
          _loadAd();
        } else {
          debugPrint('User consent is not sufficient to request ads.');
        }
      } else {
        debugPrint('Error gathering consent: ${error.message}');
      }
    });
  }

  Future<void> _loadAd() async {
    await _bannerAd?.dispose();
    setState(() {
      _bannerAd = null;
      _isAdLoaded = false;
    });

    final AnchoredAdaptiveBannerAdSize? adSize = await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
      MediaQuery.of(context).size.width.truncate(),
    );

    if (adSize == null) {
      debugPrint('Unable to get height of anchored banner.');
      return;
    }

    const adRequest = AdRequest(extras: {
      "collapsible": "bottom",
    });

    _bannerAd = BannerAd(
      adUnitId: widget.adUnitId,
      size: adSize,
      request: adRequest,
      listener: BannerAdListener(
        onAdLoaded: (Ad ad) {
          debugPrint('$ad loaded: ${ad.responseInfo}');
          setState(() {
            _bannerAd = ad as BannerAd;
            _isAdLoaded = true;
          });
        },
        onAdFailedToLoad: (Ad ad, LoadAdError error) {
          debugPrint('Banner ad failed to load: $error');
          ad.dispose();
        },
      ),
    );
    _bannerAd!.load();
  }

  Widget _buildAdWidget() {
    return OrientationBuilder(
      builder: (context, orientation) {
        if (_bannerAd != null && _isAdLoaded) {
          return Container(
            alignment: Alignment.bottomCenter,
            width: _bannerAd!.size.width.toDouble(),
            height: _bannerAd!.size.height.toDouble(),
            child: AdWidget(ad: _bannerAd!),
          );
        }
        return const SizedBox.shrink();
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return _buildAdWidget();
  }

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

What I Expected:

  • The banner ad should appear at the bottom and collapse when the user interacts with the UI.

What Actually Happened:

  • The banner ad loads successfully, but it does not collapse as expected.

Questions:

  1. Does Google AdMob currently support collapsible banners in Flutter?
  2. Is "collapsible": "bottom" the correct way to enable a collapsible banner?
  3. Are there any known workarounds or alternative approaches to achieve this behavior?

Any insights or suggestions would be greatly appreciated! Thanks in advance.

本文标签: Google AdMob Collapsible Banner Not Working in FlutterStack Overflow