Skip to content

Commit c38ee7e

Browse files
committed
address comments
1 parent 40f20f9 commit c38ee7e

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

packages/google_mobile_ads/example/lib/multi_adaptive_inline_with_recycle_example.dart

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import 'package:flutter/material.dart';
33
import 'package:collection/collection.dart';
44
import 'package:google_mobile_ads/google_mobile_ads.dart';
55

6+
/// This example demonstrates inline adaptive ads in a list view, where we
7+
/// recycle banners to improve performance.
68
class MultiInlineAdaptiveWithRecycleExample extends StatefulWidget {
79
const MultiInlineAdaptiveWithRecycleExample({super.key});
810

@@ -23,7 +25,6 @@ class _MultiInlineAdaptiveWithRecycleExampleState extends State<MultiInlineAdapt
2325
final Map<BannerAd, int> _bannerPositions = {};
2426

2527
BannerAd _createBannerAd() {
26-
print("Create a banner ad");
2728
final String bannerId = Platform.isAndroid
2829
? 'ca-app-pub-3940256099942544/6300978111'
2930
: 'ca-app-pub-3940256099942544/2934735716';
@@ -62,18 +63,17 @@ class _MultiInlineAdaptiveWithRecycleExampleState extends State<MultiInlineAdapt
6263
_banners.add(bannerAd);
6364
_bannerPositions[bannerAd] = bannerPosition;
6465
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;
7666
}
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;
7777
}
7878

7979
@override
@@ -91,15 +91,14 @@ class _MultiInlineAdaptiveWithRecycleExampleState extends State<MultiInlineAdapt
9191
final AdSize? adSize = _bannerSizes[bannerAd];
9292
if (adSize == null) {
9393
// 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'));
9895
}
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));
10298
}
99+
100+
// Show your regular non-ad content.
101+
return SizedBox(height: 200, child: ColoredBox(color: Colors.yellow));
103102
}),
104103
);
105104
}

0 commit comments

Comments
 (0)