From 4246b50a24d0306a0589f331fb910dfc2beb21bc Mon Sep 17 00:00:00 2001 From: Inex Code Date: Tue, 30 Jul 2024 16:40:04 +0300 Subject: [PATCH] fix: Handle situation when enum setting from the server has an invalid value --- assets/translations/en.json | 1 + .../basic_enum_config_item.dart | 63 ++++++++++++------- 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/assets/translations/en.json b/assets/translations/en.json index e628c9d2..a66509fd 100644 --- a/assets/translations/en.json +++ b/assets/translations/en.json @@ -373,6 +373,7 @@ }, "settings": "Service settings", "modified": "Modified", + "invalid_value_detected": "Invalid value detected. Please change it.", "invalid_input": "Invalid input", "create_job": "Create job", "update_job": "Update job", diff --git a/lib/ui/pages/services/config_item_fields/basic_enum_config_item.dart b/lib/ui/pages/services/config_item_fields/basic_enum_config_item.dart index 4417db92..439a0b11 100644 --- a/lib/ui/pages/services/config_item_fields/basic_enum_config_item.dart +++ b/lib/ui/pages/services/config_item_fields/basic_enum_config_item.dart @@ -18,29 +18,46 @@ class BasicEnumConfigItem extends StatefulWidget { class _BasicEnumConfigItemState extends State { @override - Widget build(final BuildContext context) => Column( - children: [ - ListTile( - title: Text(widget.configItem.description), - subtitle: (widget.newValue != null && - widget.newValue != widget.configItem.value) - ? Text('service_page.modified'.tr()) - : null, - trailing: DropdownButton( - value: widget.newValue ?? widget.configItem.value, - items: widget.configItem.options - .map>( - (final String option) => DropdownMenuItem( - value: option, - child: Text(option), - ), - ) - .toList(), - onChanged: (final String? value) { - widget.onChanged(value!); - }, - ), + Widget build(final BuildContext context) { + final List> options = widget.configItem.options + .map>( + (final String option) => DropdownMenuItem( + value: option, + child: Text(option), ), - ], + ) + .toList(); + if (!widget.configItem.options.contains(widget.configItem.value)) { + options.add( + DropdownMenuItem( + value: widget.configItem.value, + enabled: false, + child: Text(widget.configItem.value, + style: Theme.of(context).textTheme.titleMedium!.copyWith( + color: Theme.of(context).colorScheme.error.withOpacity(0.7), + )), + ), ); + } + return Column( + children: [ + ListTile( + title: Text(widget.configItem.description), + subtitle: (widget.newValue != null && + widget.newValue != widget.configItem.value) + ? Text('service_page.modified'.tr()) + : (!widget.configItem.options.contains(widget.configItem.value)) + ? Text('service_page.invalid_value_detected'.tr()) + : null, + trailing: DropdownButton( + value: widget.newValue ?? widget.configItem.value, + items: options, + onChanged: (final String? value) { + widget.onChanged(value!); + }, + ), + ), + ], + ); + } }