|
1 | 1 | import 'package:flutter/material.dart'; |
2 | 2 | import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_feed_item.dart'; |
3 | | -import 'package:google_mobile_ads/google_mobile_ads.dart'; |
| 3 | +import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/admob_native_ad_widget.dart'; |
| 4 | +import 'package:google_mobile_ads/google_mobile_ads.dart' as admob; |
4 | 5 | import 'package:ui_kit/ui_kit.dart'; |
5 | 6 |
|
6 | 7 | /// {@template ad_feed_item_widget} |
7 | 8 | /// A widget responsible for rendering a native ad within the feed. |
8 | 9 | /// |
9 | | -/// This widget takes an [AdFeedItem] and uses the `AdWidget` from the |
10 | | -/// `google_mobile_ads` package to display the underlying [NativeAd]. |
11 | | -/// It also handles the disposal of the ad object to prevent memory leaks. |
| 10 | +/// This widget acts as a dispatcher, taking an [AdFeedItem] and delegating |
| 11 | +/// the actual rendering to the appropriate provider-specific ad widget |
| 12 | +/// (e.g., [AdMobNativeAdWidget]). |
12 | 13 | /// {@endtemplate} |
13 | | -class AdFeedItemWidget extends StatefulWidget { |
| 14 | +class AdFeedItemWidget extends StatelessWidget { |
14 | 15 | /// {@macro ad_feed_item_widget} |
15 | 16 | const AdFeedItemWidget({required this.adFeedItem, super.key}); |
16 | 17 |
|
17 | 18 | /// The ad feed item containing the loaded native ad to be displayed. |
18 | 19 | final AdFeedItem adFeedItem; |
19 | 20 |
|
20 | | - @override |
21 | | - State<AdFeedItemWidget> createState() => _AdFeedItemWidgetState(); |
22 | | -} |
23 | | - |
24 | | -class _AdFeedItemWidgetState extends State<AdFeedItemWidget> { |
25 | | - NativeAd? _nativeAd; |
26 | | - bool _isAdLoaded = false; |
27 | | - |
28 | | - @override |
29 | | - void initState() { |
30 | | - super.initState(); |
31 | | - _nativeAd = widget.adFeedItem.nativeAd; |
32 | | - // The ad is typically already loaded by the AdService before being passed here. |
33 | | - // We can set the state to reflect this. |
34 | | - if (_nativeAd != null) { |
35 | | - _isAdLoaded = true; |
36 | | - } |
37 | | - } |
38 | | - |
39 | | - @override |
40 | | - void dispose() { |
41 | | - // Dispose the native ad when the widget is removed from the tree. |
42 | | - _nativeAd?.dispose(); |
43 | | - super.dispose(); |
44 | | - } |
45 | | - |
46 | 21 | @override |
47 | 22 | Widget build(BuildContext context) { |
48 | | - final nativeAd = _nativeAd; |
49 | | - if (!_isAdLoaded || nativeAd == null) { |
50 | | - // If the ad is not loaded for any reason, return an empty container. |
| 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. |
| 26 | + 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 | + ); |
| 37 | + } 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}.', |
| 43 | + ); |
51 | 44 | return const SizedBox.shrink(); |
52 | 45 | } |
53 | | - |
54 | | - return Card( |
55 | | - margin: const EdgeInsets.symmetric( |
56 | | - vertical: AppSpacing.sm, |
57 | | - horizontal: AppSpacing.lg, |
58 | | - ), |
59 | | - child: SizedBox( |
60 | | - height: 120, |
61 | | - child: AdWidget(ad: nativeAd), |
62 | | - ), |
63 | | - ); |
64 | 46 | } |
65 | 47 | } |
0 commit comments