Skip to content

Commit 5ecd4d8

Browse files
committed
refactor(ads): enhance native ad rendering and adaptability
- Refactor AdFeedItemWidget to use a more flexible rendering approach - Introduce support for different headline image styles in native ads - Improve error handling for unsupported ad types - Update imports and logging for better maintainability
1 parent f835fb3 commit 5ecd4d8

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed
Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,63 @@
1+
import 'package:core/core.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_feed_item.dart';
34
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/admob_native_ad_widget.dart';
5+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/native_ad_view.dart';
6+
import 'package:flutter_news_app_mobile_client_full_source_code/shared/widgets/ads/ads.dart';
47
import 'package:google_mobile_ads/google_mobile_ads.dart' as admob;
5-
import 'package:ui_kit/ui_kit.dart';
8+
import 'package:logging/logging.dart';
69

710
/// {@template ad_feed_item_widget}
8-
/// A widget responsible for rendering a native ad within the feed.
11+
/// A widget responsible for rendering a native ad within the feed,
12+
/// adapting its appearance based on the [HeadlineImageStyle] setting.
913
///
1014
/// This widget acts as a dispatcher, taking an [AdFeedItem] and delegating
1115
/// the actual rendering to the appropriate provider-specific ad widget
12-
/// (e.g., [AdMobNativeAdWidget]).
16+
/// (e.g., [AdMobNativeAdWidget]), which is then wrapped by a style-matching
17+
/// ad card.
1318
/// {@endtemplate}
1419
class AdFeedItemWidget extends StatelessWidget {
1520
/// {@macro ad_feed_item_widget}
16-
const AdFeedItemWidget({required this.adFeedItem, super.key});
21+
const AdFeedItemWidget({
22+
required this.adFeedItem,
23+
required this.headlineImageStyle,
24+
super.key,
25+
});
1726

1827
/// The ad feed item containing the loaded native ad to be displayed.
1928
final AdFeedItem adFeedItem;
2029

30+
/// The preferred image style for headlines, used to match the ad's appearance.
31+
final HeadlineImageStyle headlineImageStyle;
32+
2133
@override
2234
Widget build(BuildContext context) {
23-
// Determine the type of the underlying ad object to dispatch to the
24-
// correct rendering widget.
25-
// For now, we only support AdMob, but this can be extended.
35+
// Determine the type of the underlying ad object to instantiate the
36+
// correct provider-specific NativeAdView.
37+
final NativeAdView? nativeAdView;
2638
if (adFeedItem.nativeAd.adObject is admob.NativeAd) {
27-
return Card(
28-
margin: const EdgeInsets.symmetric(
29-
vertical: AppSpacing.sm,
30-
horizontal: AppSpacing.lg,
31-
),
32-
child: SizedBox(
33-
height: 120, // Fixed height for the ad card
34-
child: AdMobNativeAdWidget(nativeAd: adFeedItem.nativeAd),
35-
),
36-
);
39+
nativeAdView = AdMobNativeAdWidget(nativeAd: adFeedItem.nativeAd);
3740
} else {
38-
// Fallback for unsupported ad types or if adObject is null/unexpected.
39-
// In a production app, you might log this or show a generic error ad.
40-
debugPrint(
41-
'AdFeedItemWidget: Unsupported native ad type: '
42-
'${adFeedItem.nativeAd.adObject.runtimeType}.',
41+
// Log an error for unsupported ad types.
42+
Logger('AdFeedItemWidget').warning(
43+
'Unsupported native ad type: ${adFeedItem.nativeAd.adObject.runtimeType}. '
44+
'Ad will not be displayed.',
4345
);
46+
nativeAdView = null;
47+
}
48+
49+
if (nativeAdView == null) {
4450
return const SizedBox.shrink();
4551
}
52+
53+
// Select the appropriate ad card widget based on the headline image style.
54+
switch (headlineImageStyle) {
55+
case HeadlineImageStyle.hidden:
56+
return NativeAdCardTextOnly(adView: nativeAdView);
57+
case HeadlineImageStyle.smallThumbnail:
58+
return NativeAdCardImageStart(adView: nativeAdView);
59+
case HeadlineImageStyle.largeThumbnail:
60+
return NativeAdCardImageTop(adView: nativeAdView);
61+
}
4662
}
4763
}

0 commit comments

Comments
 (0)