jjsos_autosos_flutter/lib/widget/multi_state_view.dart

75 lines
2.0 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import 'package:flutter/material.dart';
enum MultiState { loading, empty, error, success, offline }
/// 多状态视图(对应 Android MultiStatePage
class MultiStateView extends StatelessWidget {
final MultiState state;
final Widget? child;
final String? emptyText;
final String? errorText;
final VoidCallback? onRetry;
final Widget? loadingWidget;
const MultiStateView({
super.key,
required this.state,
this.child,
this.emptyText,
this.errorText,
this.onRetry,
this.loadingWidget,
});
@override
Widget build(BuildContext context) {
switch (state) {
case MultiState.loading:
return loadingWidget ??
const Center(child: CircularProgressIndicator(strokeWidth: 2));
case MultiState.empty:
return _Empty(text: emptyText ?? '暂无数据', onRetry: onRetry);
case MultiState.error:
return _Empty(
text: errorText ?? '加载失败,点击重试',
icon: Icons.error_outline,
onRetry: onRetry,
);
case MultiState.offline:
return _Empty(
text: '当前无网络,请检查后重试',
icon: Icons.wifi_off,
onRetry: onRetry,
);
case MultiState.success:
return child ?? const SizedBox.shrink();
}
}
}
class _Empty extends StatelessWidget {
final String text;
final IconData? icon;
final VoidCallback? onRetry;
const _Empty({required this.text, this.icon, this.onRetry});
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(icon ?? Icons.inbox_outlined,
size: 64, color: Colors.grey.shade400),
const SizedBox(height: 12),
Text(text, style: TextStyle(color: Colors.grey.shade600)),
if (onRetry != null) ...[
const SizedBox(height: 16),
OutlinedButton(onPressed: onRetry, child: const Text('重试')),
],
],
),
);
}
}