75 lines
1.5 KiB
Dart
75 lines
1.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
/// 四种视图状态
|
|
enum ViewState { normal, error, loading, empty }
|
|
|
|
class TViewStateWidget extends StatelessWidget {
|
|
const TViewStateWidget({
|
|
super.key,
|
|
required this.viewState,
|
|
required this.child,
|
|
});
|
|
|
|
final ViewState viewState;
|
|
final Widget child;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
switch (viewState) {
|
|
case ViewState.normal:
|
|
return child;
|
|
case ViewState.loading:
|
|
return loadingView();
|
|
case ViewState.empty:
|
|
return emptyView();
|
|
case ViewState.error:
|
|
return errorView();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 加载中视图
|
|
Widget loadingView({
|
|
Color? color,
|
|
Animation<Color?>? valueColor,
|
|
Color? backgroundColor,
|
|
double? value,
|
|
}) {
|
|
return Center(
|
|
child: CircularProgressIndicator(
|
|
color: const Color(0xff4ECA8C),
|
|
valueColor: valueColor,
|
|
backgroundColor: backgroundColor ?? Colors.white,
|
|
value: value,
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 空视图
|
|
Widget emptyView({String? msg, Color? textColor}) {
|
|
return Center(
|
|
child: Text(
|
|
msg ?? 'No data',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
color: textColor ?? Colors.white,
|
|
fontSize: 15,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 错误视图
|
|
Widget errorView({String? msg, Color? textColor}) {
|
|
return Center(
|
|
child: Text(
|
|
msg ?? 'An error occurred, please try again later',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
color: textColor ?? Colors.white,
|
|
fontSize: 15,
|
|
),
|
|
),
|
|
);
|
|
}
|