error_view.dart 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import 'package:flutter/material.dart';
  2. import 'package:sino_med_cloud/l10n/app_localizations.dart';
  3. /// 错误状态视图组件
  4. class ErrorView extends StatelessWidget {
  5. final String? message;
  6. final String? error;
  7. final VoidCallback? onRetry;
  8. final IconData? icon;
  9. final Color? iconColor;
  10. final EdgeInsets? padding;
  11. const ErrorView({
  12. super.key,
  13. this.message,
  14. this.error,
  15. this.onRetry,
  16. this.icon,
  17. this.iconColor,
  18. this.padding,
  19. });
  20. @override
  21. Widget build(BuildContext context) {
  22. final l10n = AppLocalizations.of(context)!;
  23. return Padding(
  24. padding: padding ?? const EdgeInsets.all(24.0),
  25. child: Center(
  26. child: Column(
  27. mainAxisAlignment: MainAxisAlignment.center,
  28. children: [
  29. // 错误图标
  30. Icon(
  31. icon ?? Icons.error_outline,
  32. size: 64,
  33. color: iconColor ?? Theme.of(context).colorScheme.error,
  34. ),
  35. const SizedBox(height: 16),
  36. // 错误提示
  37. Text(
  38. message ?? l10n.loadFailed,
  39. style: Theme.of(context).textTheme.titleMedium?.copyWith(
  40. color: Theme.of(context).colorScheme.onSurface,
  41. ),
  42. textAlign: TextAlign.center,
  43. ),
  44. // 详细错误信息(可选)
  45. if (error != null) ...[
  46. const SizedBox(height: 8),
  47. Text(
  48. error!,
  49. style: Theme.of(context).textTheme.bodySmall?.copyWith(
  50. color: Theme.of(context).colorScheme.onSurface.withOpacity(0.6),
  51. ),
  52. textAlign: TextAlign.center,
  53. maxLines: 3,
  54. overflow: TextOverflow.ellipsis,
  55. ),
  56. ],
  57. // 重试按钮
  58. if (onRetry != null) ...[
  59. const SizedBox(height: 24),
  60. ElevatedButton.icon(
  61. onPressed: onRetry,
  62. icon: const Icon(Icons.refresh),
  63. label: Text(l10n.retry),
  64. style: ElevatedButton.styleFrom(
  65. padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
  66. ),
  67. ),
  68. ],
  69. ],
  70. ),
  71. ),
  72. );
  73. }
  74. }