@@ -9,7 +9,7 @@ import 'package:ui_kit/ui_kit.dart';
99/// {@template ad_config_form}
1010/// A form widget for configuring ad settings based on user role.
1111///
12- /// This widget uses a [SegmentedButton ] to allow selection of an [AppUserRole]
12+ /// This widget uses a [TabBar ] to allow selection of an [AppUserRole]
1313/// and then conditionally renders the relevant input fields for that role.
1414/// {@endtemplate}
1515class AdConfigForm extends StatefulWidget {
@@ -30,17 +30,19 @@ class AdConfigForm extends StatefulWidget {
3030 State <AdConfigForm > createState () => _AdConfigFormState ();
3131}
3232
33- class _AdConfigFormState extends State <AdConfigForm > {
34- AppUserRole _selectedUserRole = AppUserRole .guestUser;
33+ class _AdConfigFormState extends State <AdConfigForm >
34+ with SingleTickerProviderStateMixin {
35+ late TabController _tabController;
3536 late final Map <AppUserRole , TextEditingController > _adFrequencyControllers;
3637 late final Map <AppUserRole , TextEditingController >
37- _adPlacementIntervalControllers;
38+ _adPlacementIntervalControllers;
3839 late final Map <AppUserRole , TextEditingController >
39- _articlesToReadBeforeShowingInterstitialAdsControllers;
40+ _articlesToReadBeforeShowingInterstitialAdsControllers;
4041
4142 @override
4243 void initState () {
4344 super .initState ();
45+ _tabController = TabController (length: AppUserRole .values.length, vsync: this );
4446 _initializeControllers ();
4547 }
4648
@@ -56,39 +58,33 @@ class _AdConfigFormState extends State<AdConfigForm> {
5658 final adConfig = widget.remoteConfig.adConfig;
5759 _adFrequencyControllers = {
5860 for (final role in AppUserRole .values)
59- role:
60- TextEditingController (
61- text: _getAdFrequency (adConfig, role).toString (),
62- )
63- ..selection = TextSelection .collapsed (
64- offset: _getAdFrequency (adConfig, role).toString ().length,
65- ),
61+ role: TextEditingController (
62+ text: _getAdFrequency (adConfig, role).toString (),
63+ )..selection = TextSelection .collapsed (
64+ offset: _getAdFrequency (adConfig, role).toString ().length,
65+ ),
6666 };
6767 _adPlacementIntervalControllers = {
6868 for (final role in AppUserRole .values)
69- role:
70- TextEditingController (
71- text: _getAdPlacementInterval (adConfig, role).toString (),
72- )
73- ..selection = TextSelection .collapsed (
74- offset: _getAdPlacementInterval (
75- adConfig,
76- role,
77- ).toString ().length,
78- ),
69+ role: TextEditingController (
70+ text: _getAdPlacementInterval (adConfig, role).toString (),
71+ )..selection = TextSelection .collapsed (
72+ offset: _getAdPlacementInterval (
73+ adConfig,
74+ role,
75+ ).toString ().length,
76+ ),
7977 };
8078 _articlesToReadBeforeShowingInterstitialAdsControllers = {
8179 for (final role in AppUserRole .values)
82- role:
83- TextEditingController (
84- text: _getArticlesBeforeInterstitial (adConfig, role).toString (),
85- )
86- ..selection = TextSelection .collapsed (
87- offset: _getArticlesBeforeInterstitial (
88- adConfig,
89- role,
90- ).toString ().length,
91- ),
80+ role: TextEditingController (
81+ text: _getArticlesBeforeInterstitial (adConfig, role).toString (),
82+ )..selection = TextSelection .collapsed (
83+ offset: _getArticlesBeforeInterstitial (
84+ adConfig,
85+ role,
86+ ).toString ().length,
87+ ),
9288 };
9389 }
9490
@@ -121,15 +117,15 @@ class _AdConfigFormState extends State<AdConfigForm> {
121117 _articlesToReadBeforeShowingInterstitialAdsControllers[role]? .text =
122118 newInterstitialValue;
123119 _articlesToReadBeforeShowingInterstitialAdsControllers[role]
124- ? .selection = TextSelection .collapsed (
125- offset: newInterstitialValue.length,
126- );
120+ ? .selection =
121+ TextSelection .collapsed (offset: newInterstitialValue.length);
127122 }
128123 }
129124 }
130125
131126 @override
132127 void dispose () {
128+ _tabController.dispose ();
133129 for (final controller in _adFrequencyControllers.values) {
134130 controller.dispose ();
135131 }
@@ -158,53 +154,39 @@ class _AdConfigFormState extends State<AdConfigForm> {
158154 ),
159155 ),
160156 const SizedBox (height: AppSpacing .lg),
157+ // Replaced SegmentedButton with TabBar for role selection
161158 Align (
162159 alignment: AlignmentDirectional .centerStart,
163- child: SegmentedButton <AppUserRole >(
164- style: SegmentedButton .styleFrom (
165- shape: const RoundedRectangleBorder (
166- borderRadius: BorderRadius .zero,
167- ),
160+ child: SizedBox (
161+ height: kTextTabBarHeight,
162+ child: TabBar (
163+ controller: _tabController,
164+ tabAlignment: TabAlignment .start,
165+ isScrollable: true ,
166+ tabs: AppUserRole .values
167+ .map ((role) => Tab (text: role.l10n (context)))
168+ .toList (),
168169 ),
169- segments: AppUserRole .values
170+ ),
171+ ),
172+ const SizedBox (height: AppSpacing .lg),
173+ // TabBarView to display role-specific fields
174+ SizedBox (
175+ height: 400 , // Fixed height for TabBarView within a ListView
176+ child: TabBarView (
177+ controller: _tabController,
178+ children: AppUserRole .values
170179 .map (
171- (role) => ButtonSegment <AppUserRole >(
172- value: role,
173- label: Text (role.l10n (context)),
180+ (role) => _buildRoleSpecificFields (
181+ context,
182+ l10n,
183+ role,
184+ adConfig,
174185 ),
175186 )
176187 .toList (),
177- selected: {_selectedUserRole},
178- onSelectionChanged: (newSelection) {
179- setState (() {
180- _selectedUserRole = newSelection.first;
181- });
182- },
183188 ),
184189 ),
185- const SizedBox (height: AppSpacing .lg),
186- // Conditionally render fields based on selected user role
187- if (_selectedUserRole == AppUserRole .guestUser)
188- _buildRoleSpecificFields (
189- context,
190- l10n,
191- AppUserRole .guestUser,
192- adConfig,
193- ),
194- if (_selectedUserRole == AppUserRole .standardUser)
195- _buildRoleSpecificFields (
196- context,
197- l10n,
198- AppUserRole .standardUser,
199- adConfig,
200- ),
201- if (_selectedUserRole == AppUserRole .premiumUser)
202- _buildRoleSpecificFields (
203- context,
204- l10n,
205- AppUserRole .premiumUser,
206- adConfig,
207- ),
208190 ],
209191 );
210192 }
0 commit comments