@@ -2,13 +2,15 @@ import 'dart:async';
22
33import 'package:core/core.dart' ;
44import 'package:flutter/material.dart' ;
5+ import 'package:flutter/scheduler.dart' ;
56import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_service.dart' ;
67import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart' ;
78import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/interstitial_ad.dart' ;
89import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/widgets.dart' ;
910import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart' ;
1011import 'package:google_mobile_ads/google_mobile_ads.dart' as admob;
1112import 'package:logging/logging.dart' ;
13+ import 'package:ui_kit/ui_kit.dart' ;
1214
1315/// {@template interstitial_ad_manager}
1416/// A service that manages the lifecycle of interstitial ads.
@@ -71,12 +73,15 @@ class InterstitialAdManager {
7173 _adConfig = newAdConfig;
7274 _userRole = newUserRole;
7375 // A config change might mean we need to load an ad now.
74- _maybePreloadAd ();
76+ _maybePreloadAd (state );
7577 }
7678 }
7779
7880 /// Pre-loads an interstitial ad if one is not already loaded and conditions are met.
79- Future <void > _maybePreloadAd () async {
81+ ///
82+ /// This method now takes the current [AppState] to derive theme information
83+ /// without needing a [BuildContext] .
84+ Future <void > _maybePreloadAd (AppState appState) async {
8085 if (_preloadedAd != null ) {
8186 _logger.info ('An interstitial ad is already pre-loaded. Skipping.' );
8287 return ;
@@ -92,16 +97,31 @@ class InterstitialAdManager {
9297
9398 _logger.info ('Attempting to pre-load an interstitial ad...' );
9499 try {
95- // We need a BuildContext to get the theme for AdThemeStyle.
96- // Since this is a service, we get it from the AppBloc's navigatorKey.
97- final context = _appBloc.navigatorKey.currentContext;
98- if (context == null ) {
99- _logger.warning (
100- 'BuildContext not available from navigatorKey. Cannot create AdThemeStyle.' ,
101- );
102- return ;
103- }
104- final adThemeStyle = AdThemeStyle .fromTheme (Theme .of (context));
100+ // Determine the brightness for theme creation.
101+ // If themeMode is system, use platform brightness.
102+ final brightness = appState.themeMode == ThemeMode .system
103+ ? SchedulerBinding .instance.window.platformBrightness
104+ : (appState.themeMode == ThemeMode .dark
105+ ? Brightness .dark
106+ : Brightness .light);
107+
108+ // Create a ThemeData instance from the AppState's settings.
109+ // This allows us to derive AdThemeStyle without a BuildContext.
110+ final themeData = brightness == Brightness .light
111+ ? lightTheme (
112+ scheme: appState.flexScheme,
113+ appTextScaleFactor: appState.settings.displaySettings.textScaleFactor,
114+ appFontWeight: appState.settings.displaySettings.fontWeight,
115+ fontFamily: appState.settings.displaySettings.fontFamily,
116+ )
117+ : darkTheme (
118+ scheme: appState.flexScheme,
119+ appTextScaleFactor: appState.settings.displaySettings.textScaleFactor,
120+ appFontWeight: appState.settings.displaySettings.fontWeight,
121+ fontFamily: appState.settings.displaySettings.fontFamily,
122+ );
123+
124+ final adThemeStyle = AdThemeStyle .fromTheme (themeData);
105125
106126 final ad = await _adService.getInterstitialAd (
107127 adConfig: adConfig,
@@ -163,7 +183,7 @@ class InterstitialAdManager {
163183 if (_preloadedAd == null ) {
164184 _logger.warning ('Show ad called, but no ad is pre-loaded. Pre-loading now.' );
165185 // Attempt a last-minute load if no ad is ready.
166- await _maybePreloadAd ();
186+ await _maybePreloadAd (_appBloc.state );
167187 if (_preloadedAd == null ) {
168188 _logger.severe ('Last-minute ad load failed. Cannot show ad.' );
169189 return ;
@@ -188,7 +208,7 @@ class InterstitialAdManager {
188208 // After the ad is shown or fails to show, dispose of it and
189209 // start pre-loading the next one for the next opportunity.
190210 _disposePreloadedAd (); // Ensure the ad object is disposed
191- _maybePreloadAd ();
211+ _maybePreloadAd (_appBloc.state );
192212 }
193213 }
194214
0 commit comments