From 47bd6ff89ed3f07e10236bb9d72c0ded7f1be9de Mon Sep 17 00:00:00 2001 From: Faris Armoush Date: Mon, 7 Jul 2025 10:20:51 +0300 Subject: [PATCH] Refactor AddDeviceTypeWidget for Improved Error Handling and Loading States: - Extracted loading and error handling logic into separate methods for better readability and maintainability. - Updated UI to utilize centralized loading and failure widgets, enhancing user experience during data fetching. --- .../widgets/add_device_type_widget.dart | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/pages/space_management_v2/modules/tags/presentation/widgets/add_device_type_widget.dart b/lib/pages/space_management_v2/modules/tags/presentation/widgets/add_device_type_widget.dart index 5cfceea8..fa2af925 100644 --- a/lib/pages/space_management_v2/modules/tags/presentation/widgets/add_device_type_widget.dart +++ b/lib/pages/space_management_v2/modules/tags/presentation/widgets/add_device_type_widget.dart @@ -21,22 +21,14 @@ class AddDeviceTypeWidget extends StatelessWidget { backgroundColor: ColorsManager.whiteColors, content: BlocBuilder( builder: (context, state) => switch (state) { - ProductsInitial() => const Center( - child: CircularProgressIndicator(), - ), - ProductsLoading() => const Center( - child: CircularProgressIndicator(), - ), + ProductsInitial() => _buildLoading(context), + ProductsLoading() => _buildLoading(context), ProductsLoaded(:final products) => ProductsGrid( products: products, ), - ProductsFailure(:final errorMessage) => Center( - child: Text( - errorMessage, - style: context.textTheme.bodyMedium?.copyWith( - color: context.theme.colorScheme.error, - ), - ), + ProductsFailure(:final errorMessage) => _buildFailure( + context, + errorMessage, ), }, ), @@ -44,4 +36,25 @@ class AddDeviceTypeWidget extends StatelessWidget { ), ); } + + Widget _buildLoading(BuildContext context) => SizedBox( + width: context.screenWidth * 0.9, + height: context.screenHeight * 0.65, + child: const Center(child: CircularProgressIndicator()), + ); + + Widget _buildFailure(BuildContext context, String errorMessage) { + return SizedBox( + width: context.screenWidth * 0.9, + height: context.screenHeight * 0.65, + child: Center( + child: SelectableText( + errorMessage, + style: context.textTheme.bodyMedium?.copyWith( + color: context.theme.colorScheme.error, + ), + ), + ), + ); + } }