I am looking to implement Google Admanager native and normal banners in my app and cannot figure out a way to get the height to adapt to the different banners, without having to specify it beforehand.
This is my current code:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
class NativeAdBanner extends HookWidget {
const NativeAdBanner({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final bannerAd = useState<NativeAd?>(null);
final bannerAdIsLoaded = useState<bool>(false);
// load the banner ad
useEffect(
() {
bannerAd.value = NativeAd(
adUnitId: Platform.isAndroid
? 'ca-app-pub-3940256099942544/2247696110'
: 'ca-app-pub-3940256099942544/2934735716',
nativeTemplateStyle: NativeTemplateStyle(
templateType: TemplateType.medium,
),
listener: NativeAdListener(
onAdLoaded: (Ad ad) {
print('$BannerAd loaded.');
bannerAdIsLoaded.value = true;
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
print('$BannerAd failedToLoad: $error');
ad.dispose();
},
onAdOpened: (Ad ad) => print('$BannerAd onAdOpened.'),
onAdClosed: (Ad ad) => print('$BannerAd onAdClosed.'),
),
request: const AdRequest(),
)..load();
return () => bannerAd.value!.dispose();
},
[],
);
// if we have an ad to show -> show it
if (bannerAdIsLoaded.value && bannerAd.value != null) {
return SizedBox(
width: double.infinity,
child: Align(
alignment: Alignment.center,
child: ConstrainedBox(
constraints: const BoxConstraints(
minWidth: 300,
minHeight: 350,
maxHeight: 800,
maxWidth: 450,
),
child: AdWidget(ad: bannerAd.value!),
),
),
);
}
return const SizedBox();
}
}
I really need to be able to pass an adUnitId and the native ad to adapt natively to the height of it.
I have found multiple issues, but no solution yet. For example: https://github.com/googleads/googleads-mobile-flutter/issues/442
Did anyone figure this out?