@@ -3,6 +3,8 @@ import 'package:flutter/material.dart';
3
3
import 'package:collection/collection.dart' ;
4
4
import 'package:google_mobile_ads/google_mobile_ads.dart' ;
5
5
6
+ /// This example demonstrates inline adaptive ads in a list view, where we
7
+ /// recycle banners to improve performance.
6
8
class MultiInlineAdaptiveWithRecycleExample extends StatefulWidget {
7
9
const MultiInlineAdaptiveWithRecycleExample ({super .key});
8
10
@@ -23,7 +25,6 @@ class _MultiInlineAdaptiveWithRecycleExampleState extends State<MultiInlineAdapt
23
25
final Map <BannerAd , int > _bannerPositions = {};
24
26
25
27
BannerAd _createBannerAd () {
26
- print ("Create a banner ad" );
27
28
final String bannerId = Platform .isAndroid
28
29
? 'ca-app-pub-3940256099942544/6300978111'
29
30
: 'ca-app-pub-3940256099942544/2934735716' ;
@@ -62,18 +63,17 @@ class _MultiInlineAdaptiveWithRecycleExampleState extends State<MultiInlineAdapt
62
63
_banners.add (bannerAd);
63
64
_bannerPositions[bannerAd] = bannerPosition;
64
65
return bannerAd;
65
- } else {
66
- // If cache is full, we should recycle the banner (if possible).
67
- BannerAd bannerAd = _banners[bannerPosition % _cacheSize];
68
- if (bannerAd.isMounted) {
69
- // Create a new banner if it's not possible to recycle the banner
70
- // e.g. show 15 banners on screen, but _cacheSize is only 10.
71
- // This should be a corner case indicating _cacheSize should be increased.
72
- bannerAd = _createBannerAd ();
73
- }
74
- _bannerPositions[bannerAd] = bannerPosition;
75
- return bannerAd;
76
66
}
67
+ // If cache is full, we should recycle the banner (if possible).
68
+ BannerAd bannerAd = _banners[bannerPosition % _cacheSize];
69
+ if (bannerAd.isMounted) {
70
+ // Create a new banner if it's not possible to recycle the banner
71
+ // e.g. show 15 banners on screen, but _cacheSize is only 10.
72
+ // This should be a corner case indicating _cacheSize should be increased.
73
+ bannerAd = _createBannerAd ();
74
+ }
75
+ _bannerPositions[bannerAd] = bannerPosition;
76
+ return bannerAd;
77
77
}
78
78
79
79
@override
@@ -91,15 +91,14 @@ class _MultiInlineAdaptiveWithRecycleExampleState extends State<MultiInlineAdapt
91
91
final AdSize ? adSize = _bannerSizes[bannerAd];
92
92
if (adSize == null ) {
93
93
// Null adSize means the banner's content is not fetched yet.
94
- return SizedBox (height: 50 , child: Text ("banner is loading" ));
95
- } else {
96
- // Now this banner is loaded with ad content and corresponding ad size.
97
- return SizedBox (width: adSize.width.toDouble (), height: adSize.height.toDouble (), child: AdWidget (ad: bannerAd));
94
+ return SizedBox (height: 50 , child: Text ('banner is loading' ));
98
95
}
99
- } else {
100
- // Show your regular non-ad content.
101
- return SizedBox (height: 200 , child: ColoredBox (color: Colors .yellow));
96
+ // Now this banner is loaded with ad content and corresponding ad size.
97
+ return SizedBox (width: adSize.width.toDouble (), height: adSize.height.toDouble (), child: AdWidget (ad: bannerAd));
102
98
}
99
+
100
+ // Show your regular non-ad content.
101
+ return SizedBox (height: 200 , child: ColoredBox (color: Colors .yellow));
103
102
}),
104
103
);
105
104
}
0 commit comments