|
| 1 | +import 'package:core/core.dart'; |
1 | 2 | import 'package:flutter/material.dart'; |
2 | 3 | import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_feed_item.dart'; |
3 | 4 | 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'; |
4 | 7 | 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'; |
6 | 9 |
|
7 | 10 | /// {@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. |
9 | 13 | /// |
10 | 14 | /// This widget acts as a dispatcher, taking an [AdFeedItem] and delegating |
11 | 15 | /// 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. |
13 | 18 | /// {@endtemplate} |
14 | 19 | class AdFeedItemWidget extends StatelessWidget { |
15 | 20 | /// {@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 | + }); |
17 | 26 |
|
18 | 27 | /// The ad feed item containing the loaded native ad to be displayed. |
19 | 28 | final AdFeedItem adFeedItem; |
20 | 29 |
|
| 30 | + /// The preferred image style for headlines, used to match the ad's appearance. |
| 31 | + final HeadlineImageStyle headlineImageStyle; |
| 32 | + |
21 | 33 | @override |
22 | 34 | 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; |
26 | 38 | 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); |
37 | 40 | } 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.', |
43 | 45 | ); |
| 46 | + nativeAdView = null; |
| 47 | + } |
| 48 | + |
| 49 | + if (nativeAdView == null) { |
44 | 50 | return const SizedBox.shrink(); |
45 | 51 | } |
| 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 | + } |
46 | 62 | } |
47 | 63 | } |
0 commit comments