Skip to content

Commit acc01e7

Browse files
committed
refactor(ads): improve ad loading logic and support demo environment
- Add support for demo environment by displaying placeholder ads directly - Remove usage of PlaceholderAdWidget and replace with SizedBox.shrink() - Optimize code by reading AppBloc state once per build method - Update imports to include demo ad widgets - Improve code readability and maintainability
1 parent 1a7b605 commit acc01e7

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

lib/ads/widgets/feed_ad_loader_widget.dart

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/banne
1111
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/inline_ad.dart';
1212
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/native_ad.dart';
1313
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/admob_inline_ad_widget.dart';
14+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/demo_banner_ad_widget.dart';
15+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/demo_native_ad_widget.dart';
1416
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/local_banner_ad_widget.dart';
1517
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/local_native_ad_widget.dart';
16-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/placeholder_ad_widget.dart';
1718
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
19+
import 'package:flutter_news_app_mobile_client_full_source_code/app/config/app_environment.dart';
1820
import 'package:logging/logging.dart';
1921
import 'package:ui_kit/ui_kit.dart';
2022

@@ -261,6 +263,30 @@ class _FeedAdLoaderWidgetState extends State<FeedAdLoaderWidget> {
261263

262264
@override
263265
Widget build(BuildContext context) {
266+
final appEnvironment = context.read<AppBloc>().state.environment;
267+
final headlineImageStyle = context
268+
.read<AppBloc>()
269+
.state
270+
.settings
271+
.feedPreferences
272+
.headlineImageStyle;
273+
274+
// In demo environment, display placeholder ads directly.
275+
if (appEnvironment == AppEnvironment.demo) {
276+
switch (widget.adPlaceholder.adType) {
277+
case AdType.native:
278+
return DemoNativeAdWidget(headlineImageStyle: headlineImageStyle);
279+
case AdType.banner:
280+
return DemoBannerAdWidget(headlineImageStyle: headlineImageStyle);
281+
case AdType.interstitial:
282+
case AdType.video:
283+
// Interstitial and video ads are not inline, so they won't be
284+
// handled by FeedAdLoaderWidget. Fallback to a generic placeholder.
285+
return const SizedBox.shrink();
286+
}
287+
}
288+
289+
// For other environments (development, production), proceed with real ad loading.
264290
if (_isLoading) {
265291
// Show a shimmer or loading indicator while the ad is being loaded.
266292
return const Padding(
@@ -276,47 +302,32 @@ class _FeedAdLoaderWidgetState extends State<FeedAdLoaderWidget> {
276302
),
277303
);
278304
} else if (_hasError || _loadedAd == null) {
279-
// Show a placeholder or error message if ad loading failed.
280-
return const PlaceholderAdWidget();
305+
// Fallback for unsupported local ad types or errors
306+
return const SizedBox.shrink();
281307
} else {
282308
// If an ad is successfully loaded, dispatch to the appropriate
283309
// provider-specific widget for rendering.
284310
switch (_loadedAd!.provider) {
285311
case AdPlatformType.admob:
286312
return AdmobInlineAdWidget(
287313
inlineAd: _loadedAd!,
288-
headlineImageStyle: context
289-
.read<AppBloc>()
290-
.state
291-
.settings
292-
.feedPreferences
293-
.headlineImageStyle,
314+
headlineImageStyle: headlineImageStyle,
294315
);
295316
case AdPlatformType.local:
296317
if (_loadedAd is NativeAd && _loadedAd!.adObject is LocalNativeAd) {
297318
return LocalNativeAdWidget(
298319
localNativeAd: _loadedAd!.adObject as LocalNativeAd,
299-
headlineImageStyle: context
300-
.read<AppBloc>()
301-
.state
302-
.settings
303-
.feedPreferences
304-
.headlineImageStyle,
320+
headlineImageStyle: headlineImageStyle,
305321
);
306322
} else if (_loadedAd is BannerAd &&
307323
_loadedAd!.adObject is LocalBannerAd) {
308324
return LocalBannerAdWidget(
309325
localBannerAd: _loadedAd!.adObject as LocalBannerAd,
310-
headlineImageStyle: context
311-
.read<AppBloc>()
312-
.state
313-
.settings
314-
.feedPreferences
315-
.headlineImageStyle,
326+
headlineImageStyle: headlineImageStyle,
316327
);
317328
}
318329
// Fallback for unsupported local ad types or errors
319-
return const PlaceholderAdWidget();
330+
return const SizedBox.shrink();
320331
}
321332
}
322333
}

0 commit comments

Comments
 (0)