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.
This commit is contained in:
Faris Armoush
2025-07-07 10:20:51 +03:00
parent df87e41d61
commit 47bd6ff89e

View File

@ -21,22 +21,14 @@ class AddDeviceTypeWidget extends StatelessWidget {
backgroundColor: ColorsManager.whiteColors, backgroundColor: ColorsManager.whiteColors,
content: BlocBuilder<ProductsBloc, ProductsState>( content: BlocBuilder<ProductsBloc, ProductsState>(
builder: (context, state) => switch (state) { builder: (context, state) => switch (state) {
ProductsInitial() => const Center( ProductsInitial() => _buildLoading(context),
child: CircularProgressIndicator(), ProductsLoading() => _buildLoading(context),
),
ProductsLoading() => const Center(
child: CircularProgressIndicator(),
),
ProductsLoaded(:final products) => ProductsGrid( ProductsLoaded(:final products) => ProductsGrid(
products: products, products: products,
), ),
ProductsFailure(:final errorMessage) => Center( ProductsFailure(:final errorMessage) => _buildFailure(
child: Text( context,
errorMessage, errorMessage,
style: context.textTheme.bodyMedium?.copyWith(
color: context.theme.colorScheme.error,
),
),
), ),
}, },
), ),
@ -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,
),
),
),
);
}
} }