| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import 'package:flutter/material.dart';
- /// 错误状态视图组件
- class ErrorView extends StatelessWidget {
- final String? message;
- final String? error;
- final VoidCallback? onRetry;
- final IconData? icon;
- final Color? iconColor;
- final EdgeInsets? padding;
- const ErrorView({
- super.key,
- this.message,
- this.error,
- this.onRetry,
- this.icon,
- this.iconColor,
- this.padding,
- });
- @override
- Widget build(BuildContext context) {
- return Padding(
- padding: padding ?? const EdgeInsets.all(24.0),
- child: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- // 错误图标
- Icon(
- icon ?? Icons.error_outline,
- size: 64,
- color: iconColor ?? Theme.of(context).colorScheme.error,
- ),
-
- const SizedBox(height: 16),
-
- // 错误提示
- Text(
- message ?? '加载失败',
- style: Theme.of(context).textTheme.titleMedium?.copyWith(
- color: Theme.of(context).colorScheme.onSurface,
- ),
- textAlign: TextAlign.center,
- ),
-
- // 详细错误信息(可选)
- if (error != null) ...[
- const SizedBox(height: 8),
- Text(
- error!,
- style: Theme.of(context).textTheme.bodySmall?.copyWith(
- color: Theme.of(context).colorScheme.onSurface.withOpacity(0.6),
- ),
- textAlign: TextAlign.center,
- maxLines: 3,
- overflow: TextOverflow.ellipsis,
- ),
- ],
-
- // 重试按钮
- if (onRetry != null) ...[
- const SizedBox(height: 24),
- ElevatedButton.icon(
- onPressed: onRetry,
- icon: const Icon(Icons.refresh),
- label: const Text('重试'),
- style: ElevatedButton.styleFrom(
- padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
- ),
- ),
- ],
- ],
- ),
- ),
- );
- }
- }
|