Skip to content

Commit 9ec434e

Browse files
authored
Merge pull request #87 from flutter-news-app-full-source-code/Fix-Native-Ad-Rendering
Fix native ad rendering
2 parents 09f7e82 + b9ba612 commit 9ec434e

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

lib/ads/admob_ad_provider.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ class AdMobAdProvider implements AdProvider {
156156
id: _uuid.v4(), // Generate a unique ID for our internal model
157157
provider: app_native_ad.AdProviderType.admob, // Set the provider
158158
adObject: googleNativeAd, // Store the original AdMob object
159+
templateType: switch (templateType) {
160+
admob.TemplateType.small => app_native_ad.NativeAdTemplateType.small,
161+
admob.TemplateType.medium => app_native_ad.NativeAdTemplateType.medium,
162+
},
159163
);
160164
}
161165

lib/ads/models/native_ad.dart

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
import 'package:equatable/equatable.dart';
22
import 'package:flutter/foundation.dart';
33

4+
/// {@template native_ad_template_type}
5+
/// Defines the visual template type for a native ad.
6+
///
7+
/// This is used to determine the expected size and layout of the native ad
8+
/// when rendering it in the UI.
9+
/// {@endtemplate}
10+
enum NativeAdTemplateType {
11+
/// A small native ad template, typically used for compact layouts.
12+
small,
13+
14+
/// A medium native ad template, typically used for more prominent layouts.
15+
medium,
16+
}
17+
418
/// {@template ad_provider_type}
519
/// Defines the supported ad network providers.
620
///
@@ -11,7 +25,7 @@ enum AdProviderType {
1125
/// Google AdMob provider.
1226
admob,
1327

14-
/// A placeholder provider for platforms where native ads are not supported.
28+
/// A placeholder provider for platforms where native ad SDKs are not supported.
1529
///
1630
/// This is primarily used for the web demo environment to maintain UI
1731
/// consistency without relying on native SDKs.
@@ -33,6 +47,7 @@ class NativeAd extends Equatable {
3347
required this.id,
3448
required this.provider,
3549
required this.adObject,
50+
required this.templateType,
3651
});
3752

3853
/// A unique identifier for this specific native ad instance.
@@ -50,6 +65,9 @@ class NativeAd extends Equatable {
5065
/// only within the dedicated ad rendering widget for that provider.
5166
final Object adObject;
5267

68+
/// The template type of the native ad, indicating its expected size and layout.
69+
final NativeAdTemplateType templateType;
70+
5371
@override
54-
List<Object?> get props => [id, provider, adObject];
72+
List<Object?> get props => [id, provider, adObject, templateType];
5573
}

lib/ads/no_op_ad_provider.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class NoOpAdProvider implements AdProvider {
5252
id: _uuid.v4(),
5353
provider: AdProviderType.placeholder,
5454
adObject: Object(), // Dummy object
55+
templateType: switch (imageStyle) {
56+
HeadlineImageStyle.largeThumbnail => NativeAdTemplateType.medium,
57+
_ => NativeAdTemplateType.small,
58+
},
5559
);
5660
}
5761
}

lib/ads/widgets/admob_native_ad_widget.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ class AdmobNativeAdWidget extends StatelessWidget {
3636

3737
// The AdWidget from the google_mobile_ads package handles the rendering
3838
// of the pre-loaded native ad.
39-
return admob.AdWidget(ad: adObject);
39+
// We wrap it in a SizedBox to provide explicit height constraints,
40+
// which is crucial for platform views (like native ads) within scrollable
41+
// lists to prevent "unbounded height" errors.
42+
final adHeight = switch (nativeAd.templateType) {
43+
app_ad_models.NativeAdTemplateType.small => 120, // Example height for small template
44+
app_ad_models.NativeAdTemplateType.medium => 340, // Example height for medium template
45+
};
46+
47+
return SizedBox(
48+
height: adHeight.toDouble(),
49+
child: admob.AdWidget(ad: adObject),
50+
);
4051
}
4152
}

0 commit comments

Comments
 (0)