|
| 1 | +import 'package:core/core.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/l10n.dart'; |
| 4 | +import 'package:flutter_news_app_web_dashboard_full_source_code/shared/extensions/engagement_mode_l10n.dart'; |
| 5 | +import 'package:ui_kit/ui_kit.dart'; |
| 6 | + |
| 7 | +/// {@template engagement_settings_form} |
| 8 | +/// A form widget for configuring user engagement settings. |
| 9 | +/// {@endtemplate} |
| 10 | +class EngagementSettingsForm extends StatelessWidget { |
| 11 | + /// {@macro engagement_settings_form} |
| 12 | + const EngagementSettingsForm({ |
| 13 | + required this.remoteConfig, |
| 14 | + required this.onConfigChanged, |
| 15 | + super.key, |
| 16 | + }); |
| 17 | + |
| 18 | + /// The current [RemoteConfig] object. |
| 19 | + final RemoteConfig remoteConfig; |
| 20 | + |
| 21 | + /// Callback to notify parent of changes to the [RemoteConfig]. |
| 22 | + final ValueChanged<RemoteConfig> onConfigChanged; |
| 23 | + |
| 24 | + @override |
| 25 | + Widget build(BuildContext context) { |
| 26 | + final l10n = AppLocalizationsX(context).l10n; |
| 27 | + final communityConfig = remoteConfig.features.community; |
| 28 | + final engagementConfig = communityConfig.engagement; |
| 29 | + |
| 30 | + return Column( |
| 31 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 32 | + children: [ |
| 33 | + SwitchListTile( |
| 34 | + title: Text(l10n.enableEngagementFeaturesLabel), |
| 35 | + subtitle: Text(l10n.enableEngagementFeaturesDescription), |
| 36 | + value: engagementConfig.enabled, |
| 37 | + onChanged: (value) { |
| 38 | + final newConfig = communityConfig.copyWith( |
| 39 | + engagement: engagementConfig.copyWith(enabled: value), |
| 40 | + ); |
| 41 | + onConfigChanged( |
| 42 | + remoteConfig.copyWith( |
| 43 | + features: remoteConfig.features.copyWith(community: newConfig), |
| 44 | + ), |
| 45 | + ); |
| 46 | + }, |
| 47 | + ), |
| 48 | + const SizedBox(height: AppSpacing.lg), |
| 49 | + Padding( |
| 50 | + padding: const EdgeInsets.symmetric(horizontal: AppSpacing.lg), |
| 51 | + child: Text( |
| 52 | + l10n.engagementModeDescription, |
| 53 | + style: Theme.of(context).textTheme.bodySmall, |
| 54 | + ), |
| 55 | + ), |
| 56 | + const SizedBox(height: AppSpacing.md), |
| 57 | + Padding( |
| 58 | + padding: const EdgeInsets.symmetric(horizontal: AppSpacing.lg), |
| 59 | + child: SegmentedButton<EngagementMode>( |
| 60 | + segments: EngagementMode.values |
| 61 | + .map( |
| 62 | + (mode) => ButtonSegment<EngagementMode>( |
| 63 | + value: mode, |
| 64 | + label: Text(mode.l10n(context)), |
| 65 | + ), |
| 66 | + ) |
| 67 | + .toList(), |
| 68 | + selected: {engagementConfig.engagementMode}, |
| 69 | + onSelectionChanged: (newSelection) { |
| 70 | + final newConfig = communityConfig.copyWith( |
| 71 | + engagement: engagementConfig.copyWith( |
| 72 | + engagementMode: newSelection.first, |
| 73 | + ), |
| 74 | + ); |
| 75 | + onConfigChanged( |
| 76 | + remoteConfig.copyWith( |
| 77 | + features: remoteConfig.features.copyWith( |
| 78 | + community: newConfig, |
| 79 | + ), |
| 80 | + ), |
| 81 | + ); |
| 82 | + }, |
| 83 | + ), |
| 84 | + ), |
| 85 | + ], |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
0 commit comments