@@ -3,10 +3,9 @@ import 'package:flutter/material.dart';
33import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/l10n.dart' ;
44
55/// {@template ad_config_form}
6- /// A form widget for configuring ad settings based on user role .
6+ /// A form widget for configuring global ad settings.
77///
8- /// This widget uses a [TabBar] to allow selection of an [AppUserRole]
9- /// and then conditionally renders the relevant input fields for that role.
8+ /// This widget primarily controls the global enable/disable switch for ads.
109/// {@endtemplate}
1110class AdConfigForm extends StatefulWidget {
1211 /// {@macro ad_config_form}
@@ -26,96 +25,7 @@ class AdConfigForm extends StatefulWidget {
2625 State <AdConfigForm > createState () => _AdConfigFormState ();
2726}
2827
29- class _AdConfigFormState extends State <AdConfigForm >
30- with SingleTickerProviderStateMixin {
31- late TabController _tabController;
32- late final Map <AppUserRole , TextEditingController > _adFrequencyControllers;
33- late final Map <AppUserRole , TextEditingController >
34- _adPlacementIntervalControllers;
35-
36- @override
37- void initState () {
38- super .initState ();
39- _tabController = TabController (
40- length: AppUserRole .values.length,
41- vsync: this ,
42- );
43- _initializeControllers ();
44- }
45-
46- @override
47- void didUpdateWidget (covariant AdConfigForm oldWidget) {
48- super .didUpdateWidget (oldWidget);
49- if (widget.remoteConfig.adConfig != oldWidget.remoteConfig.adConfig) {
50- _updateControllers ();
51- }
52- }
53-
54- void _initializeControllers () {
55- final adConfig = widget.remoteConfig.adConfig;
56- _adFrequencyControllers = {
57- for (final role in AppUserRole .values)
58- role:
59- TextEditingController (
60- text: _getAdFrequency (adConfig, role).toString (),
61- )
62- ..selection = TextSelection .collapsed (
63- offset: _getAdFrequency (adConfig, role).toString ().length,
64- ),
65- };
66- _adPlacementIntervalControllers = {
67- for (final role in AppUserRole .values)
68- role:
69- TextEditingController (
70- text: _getAdPlacementInterval (adConfig, role).toString (),
71- )
72- ..selection = TextSelection .collapsed (
73- offset: _getAdPlacementInterval (
74- adConfig,
75- role,
76- ).toString ().length,
77- ),
78- };
79- }
80-
81- void _updateControllers () {
82- final adConfig = widget.remoteConfig.adConfig;
83- for (final role in AppUserRole .values) {
84- final newFrequencyValue = _getAdFrequency (adConfig, role).toString ();
85- if (_adFrequencyControllers[role]? .text != newFrequencyValue) {
86- _adFrequencyControllers[role]? .text = newFrequencyValue;
87- _adFrequencyControllers[role]? .selection = TextSelection .collapsed (
88- offset: newFrequencyValue.length,
89- );
90- }
91-
92- final newPlacementIntervalValue = _getAdPlacementInterval (
93- adConfig,
94- role,
95- ).toString ();
96- if (_adPlacementIntervalControllers[role]? .text !=
97- newPlacementIntervalValue) {
98- _adPlacementIntervalControllers[role]? .text = newPlacementIntervalValue;
99- _adPlacementIntervalControllers[role]? .selection =
100- TextSelection .collapsed (
101- offset: newPlacementIntervalValue.length,
102- );
103- }
104- }
105- }
106-
107- @override
108- void dispose () {
109- _tabController.dispose ();
110- for (final controller in _adFrequencyControllers.values) {
111- controller.dispose ();
112- }
113- for (final controller in _adPlacementIntervalControllers.values) {
114- controller.dispose ();
115- }
116- super .dispose ();
117- }
118-
28+ class _AdConfigFormState extends State <AdConfigForm > {
11929 @override
12030 Widget build (BuildContext context) {
12131 final adConfig = widget.remoteConfig.adConfig;
@@ -138,38 +48,4 @@ class _AdConfigFormState extends State<AdConfigForm>
13848 ],
13949 );
14050 }
141-
142- int _getAdFrequency (AdConfig config, AppUserRole role) {
143- switch (role) {
144- case AppUserRole .guestUser:
145- return config.feedAdConfiguration.frequencyConfig.guestAdFrequency;
146- case AppUserRole .standardUser:
147- return config
148- .feedAdConfiguration
149- .frequencyConfig
150- .authenticatedAdFrequency;
151- case AppUserRole .premiumUser:
152- return config.feedAdConfiguration.frequencyConfig.premiumAdFrequency;
153- }
154- }
155-
156- int _getAdPlacementInterval (AdConfig config, AppUserRole role) {
157- switch (role) {
158- case AppUserRole .guestUser:
159- return config
160- .feedAdConfiguration
161- .frequencyConfig
162- .guestAdPlacementInterval;
163- case AppUserRole .standardUser:
164- return config
165- .feedAdConfiguration
166- .frequencyConfig
167- .authenticatedAdPlacementInterval;
168- case AppUserRole .premiumUser:
169- return config
170- .feedAdConfiguration
171- .frequencyConfig
172- .premiumAdPlacementInterval;
173- }
174- }
17551}
0 commit comments