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,
content: BlocBuilder<ProductsBloc, ProductsState>(
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,
),
),
),
);
}
}