empty_view.dart 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'package:flutter/material.dart';
  2. /// 空状态视图组件
  3. class EmptyView extends StatelessWidget {
  4. final String? message;
  5. final String? imagePath;
  6. final IconData? icon;
  7. final Color? iconColor;
  8. final double? iconSize;
  9. final Widget? action;
  10. final EdgeInsets? padding;
  11. const EmptyView({
  12. super.key,
  13. this.message,
  14. this.imagePath,
  15. this.icon,
  16. this.iconColor,
  17. this.iconSize,
  18. this.action,
  19. this.padding,
  20. });
  21. @override
  22. Widget build(BuildContext 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. if (imagePath != null)
  31. Image.asset(
  32. imagePath!,
  33. width: 120,
  34. height: 120,
  35. )
  36. else if (icon != null)
  37. Icon(
  38. icon,
  39. size: iconSize ?? 64,
  40. color: iconColor ?? Theme.of(context).colorScheme.onSurface.withOpacity(0.4),
  41. )
  42. else
  43. Icon(
  44. Icons.inbox_outlined,
  45. size: iconSize ?? 64,
  46. color: Theme.of(context).colorScheme.onSurface.withOpacity(0.4),
  47. ),
  48. const SizedBox(height: 16),
  49. // 提示文字
  50. if (message != null)
  51. Text(
  52. message!,
  53. style: Theme.of(context).textTheme.bodyMedium?.copyWith(
  54. color: Theme.of(context).colorScheme.onSurface.withOpacity(0.6),
  55. ),
  56. textAlign: TextAlign.center,
  57. )
  58. else
  59. Text(
  60. '暂无数据',
  61. style: Theme.of(context).textTheme.bodyMedium?.copyWith(
  62. color: Theme.of(context).colorScheme.onSurface.withOpacity(0.6),
  63. ),
  64. textAlign: TextAlign.center,
  65. ),
  66. // 操作按钮
  67. if (action != null) ...[
  68. const SizedBox(height: 24),
  69. action!,
  70. ],
  71. ],
  72. ),
  73. ),
  74. );
  75. }
  76. }