error_view.dart 2.2 KB

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