Skip to content

Commit 4b64292

Browse files
committed
refactor(ads): replace ThemeData with AdThemeStyle in native ad implementation
- Introduce AdThemeStyle model for better encapsulation and testability - Update native ad styling logic to use new AdThemeStyle class - Remove direct dependency on Flutter Material package
1 parent a080791 commit 4b64292

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

lib/ads/admob_ad_provider.dart

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import 'dart:async';
22

33
import 'package:core/core.dart';
44
import 'package:flutter/foundation.dart';
5-
import 'package:flutter/material.dart';
65
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_provider.dart';
6+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart';
77
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/native_ad.dart'
88
as app_native_ad;
99
import 'package:google_mobile_ads/google_mobile_ads.dart' as admob;
@@ -79,7 +79,7 @@ class AdMobAdProvider implements AdProvider {
7979
@override
8080
Future<app_native_ad.NativeAd?> loadNativeAd({
8181
required HeadlineImageStyle imageStyle,
82-
required ThemeData theme,
82+
required AdThemeStyle adThemeStyle,
8383
}) async {
8484
if (_nativeAdUnitId.isEmpty) {
8585
_logger.warning('No native ad unit ID configured for this platform.');
@@ -100,7 +100,7 @@ class AdMobAdProvider implements AdProvider {
100100
request: const admob.AdRequest(),
101101
nativeTemplateStyle: _createNativeTemplateStyle(
102102
templateType: templateType,
103-
theme: theme,
103+
adThemeStyle: adThemeStyle,
104104
),
105105
listener: admob.NativeAdListener(
106106
onAdLoaded: (ad) {
@@ -166,38 +166,35 @@ class AdMobAdProvider implements AdProvider {
166166
/// to the AdMob native ad styling options, ensuring a consistent look and feel.
167167
admob.NativeTemplateStyle _createNativeTemplateStyle({
168168
required admob.TemplateType templateType,
169-
required ThemeData theme,
169+
required AdThemeStyle adThemeStyle,
170170
}) {
171-
final colorScheme = theme.colorScheme;
172-
final textTheme = theme.textTheme;
173-
174171
return admob.NativeTemplateStyle(
175172
templateType: templateType,
176-
mainBackgroundColor: colorScheme.surface,
177-
cornerRadius: AppSpacing.sm, // 8.0
173+
mainBackgroundColor: adThemeStyle.mainBackgroundColor,
174+
cornerRadius: adThemeStyle.cornerRadius,
178175
callToActionTextStyle: admob.NativeTemplateTextStyle(
179-
textColor: colorScheme.onPrimary,
180-
backgroundColor: colorScheme.primary,
176+
textColor: adThemeStyle.callToActionTextColor,
177+
backgroundColor: adThemeStyle.callToActionBackgroundColor,
181178
style: admob.NativeTemplateFontStyle.normal,
182-
size: textTheme.labelLarge?.fontSize,
179+
size: adThemeStyle.callToActionTextSize,
183180
),
184181
primaryTextStyle: admob.NativeTemplateTextStyle(
185-
textColor: colorScheme.onSurface,
186-
backgroundColor: colorScheme.surface,
182+
textColor: adThemeStyle.primaryTextColor,
183+
backgroundColor: adThemeStyle.primaryBackgroundColor,
187184
style: admob.NativeTemplateFontStyle.bold,
188-
size: textTheme.titleMedium?.fontSize,
185+
size: adThemeStyle.primaryTextSize,
189186
),
190187
secondaryTextStyle: admob.NativeTemplateTextStyle(
191-
textColor: colorScheme.onSurfaceVariant,
192-
backgroundColor: colorScheme.surface,
188+
textColor: adThemeStyle.secondaryTextColor,
189+
backgroundColor: adThemeStyle.secondaryBackgroundColor,
193190
style: admob.NativeTemplateFontStyle.normal,
194-
size: textTheme.bodyMedium?.fontSize,
191+
size: adThemeStyle.secondaryTextSize,
195192
),
196193
tertiaryTextStyle: admob.NativeTemplateTextStyle(
197-
textColor: colorScheme.onSurfaceVariant,
198-
backgroundColor: colorScheme.surface,
194+
textColor: adThemeStyle.tertiaryTextColor,
195+
backgroundColor: adThemeStyle.tertiaryBackgroundColor,
199196
style: admob.NativeTemplateFontStyle.normal,
200-
size: textTheme.labelSmall?.fontSize,
197+
size: adThemeStyle.tertiaryTextSize,
201198
),
202199
);
203200
}

0 commit comments

Comments
 (0)