Skip to content

Commit 431e919

Browse files
committed
refactor(ads): replace direct AdWidget usage with dispatcher widget
- Convert AdFeedItemWidget from StatefulWidget to StatelessWidget - Remove direct usage of google_mobile_ads NativeAd and AdWidget - Introduce support for AdMob native ads via AdMobNativeAdWidget - Add
1 parent 086e7e8 commit 431e919

File tree

1 file changed

+27
-45
lines changed

1 file changed

+27
-45
lines changed
Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,47 @@
11
import 'package:flutter/material.dart';
22
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;
45
import 'package:ui_kit/ui_kit.dart';
56

67
/// {@template ad_feed_item_widget}
78
/// A widget responsible for rendering a native ad within the feed.
89
///
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]).
1213
/// {@endtemplate}
13-
class AdFeedItemWidget extends StatefulWidget {
14+
class AdFeedItemWidget extends StatelessWidget {
1415
/// {@macro ad_feed_item_widget}
1516
const AdFeedItemWidget({required this.adFeedItem, super.key});
1617

1718
/// The ad feed item containing the loaded native ad to be displayed.
1819
final AdFeedItem adFeedItem;
1920

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-
4621
@override
4722
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+
);
5144
return const SizedBox.shrink();
5245
}
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-
);
6446
}
6547
}

0 commit comments

Comments
 (0)