Skip to content

Commit 9ba72b6

Browse files
committed
refactor(ads): enhance InArticleAdLoaderWidget for demo environment
- Add support for displaying placeholder ads in demo environment - Integrate DemoBannerAdWidget and DemoNativeAdWidget for demo mode - Update ad loading logic for different environments - Improve error handling and fallback to SizedBox.shrink()
1 parent acc01e7 commit 9ba72b6

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

lib/ads/widgets/in_article_ad_loader_widget.dart

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/banne
1010
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/inline_ad.dart';
1111
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/native_ad.dart';
1212
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/admob_inline_ad_widget.dart';
13+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/demo_banner_ad_widget.dart';
14+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/demo_native_ad_widget.dart';
1315
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/local_banner_ad_widget.dart';
1416
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/local_native_ad_widget.dart';
15-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/placeholder_ad_widget.dart';
1617
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
18+
import 'package:flutter_news_app_mobile_client_full_source_code/app/config/app_environment.dart';
1719
import 'package:logging/logging.dart';
1820
import 'package:ui_kit/ui_kit.dart';
1921

@@ -208,6 +210,33 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
208210

209211
@override
210212
Widget build(BuildContext context) {
213+
final appEnvironment = context.read<AppBloc>().state.environment;
214+
final headlineImageStyle = context
215+
.read<AppBloc>()
216+
.state
217+
.settings
218+
.feedPreferences
219+
.headlineImageStyle;
220+
221+
// In demo environment, display placeholder ads directly.
222+
if (appEnvironment == AppEnvironment.demo) {
223+
// Determine the ad type from the adConfig's articleAdConfiguration
224+
final adType =
225+
widget.adConfig.articleAdConfiguration.defaultInArticleAdType;
226+
switch (adType) {
227+
case AdType.native:
228+
return DemoNativeAdWidget(headlineImageStyle: headlineImageStyle);
229+
case AdType.banner:
230+
return DemoBannerAdWidget(headlineImageStyle: headlineImageStyle);
231+
case AdType.interstitial:
232+
case AdType.video:
233+
// Interstitial and video ads are not inline, so they won't be
234+
// handled by InArticleAdLoaderWidget. Fallback to a generic placeholder.
235+
return const SizedBox.shrink();
236+
}
237+
}
238+
239+
// For other environments (development, production), proceed with real ad loading.
211240
if (_isLoading) {
212241
return const Padding(
213242
padding: EdgeInsets.symmetric(
@@ -222,26 +251,31 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
222251
),
223252
);
224253
} else if (_hasError || _loadedAd == null) {
225-
return const PlaceholderAdWidget();
254+
return const SizedBox.shrink();
226255
} else {
227256
// If an ad is successfully loaded, dispatch to the appropriate
228257
// provider-specific widget for rendering.
229258
switch (_loadedAd!.provider) {
230259
case AdPlatformType.admob:
231-
return AdmobInlineAdWidget(inlineAd: _loadedAd!);
260+
return AdmobInlineAdWidget(
261+
inlineAd: _loadedAd!,
262+
headlineImageStyle: headlineImageStyle,
263+
);
232264
case AdPlatformType.local:
233265
if (_loadedAd is NativeAd && _loadedAd!.adObject is LocalNativeAd) {
234266
return LocalNativeAdWidget(
235267
localNativeAd: _loadedAd!.adObject as LocalNativeAd,
268+
headlineImageStyle: headlineImageStyle,
236269
);
237270
} else if (_loadedAd is BannerAd &&
238271
_loadedAd!.adObject is LocalBannerAd) {
239272
return LocalBannerAdWidget(
240273
localBannerAd: _loadedAd!.adObject as LocalBannerAd,
274+
headlineImageStyle: headlineImageStyle,
241275
);
242276
}
243277
// Fallback for unsupported local ad types or errors
244-
return const PlaceholderAdWidget();
278+
return const SizedBox.shrink();
245279
}
246280
}
247281
}

0 commit comments

Comments
 (0)