根据 chdown/super_core
仓库中的 lib/super_core.dart
文件及其引用的相关代码文件,生成以下详细的 README 使用文档:
SuperCore
是一个 Dart 库,提供了一系列核心组件和扩展功能,旨在简化应用程序开发中的常见任务,如加载状态管理、错误处理、网络请求等。
- 加载状态管理
- 错误处理
- 网络请求拦截器
- 常用工具类和扩展方法
请确保在 pubspec.yaml
文件中添加了 super_core
依赖:
dependencies:
super_core:
git:
url: https://github.com/chdown/super_core.git
然后运行 flutter pub get
安装依赖。
在您的 Dart 文件中导入 super_core
:
import 'package:super_core/super_core.dart';
SuperCore
是一个混入,提供了加载状态管理和错误处理的基本功能。
mixin SuperCore {
void showState(LoadConfig loadConfig, LoadEnum loadEnum, LoadState loadState, String errorMsg);
void showToast(String? message);
Future<bool> consumptionError(Object e, StackTrace? trace) async => false;
Future request<T>({
required Future<dynamic> Function() request,
LoadEnum loadEnum = LoadEnum.loading,
LoadConfig? loadConfig,
}) async;
}
方法 | 参数 | 类型 | 说明 |
---|---|---|---|
showState |
loadConfig loadEnum loadState errorMsg |
LoadConfig LoadEnum LoadState String |
展示加载状态 |
showToast |
message |
String? |
显示 toast 消息 |
consumptionError |
e trace |
Object StackTrace? |
处理错误并防止显示 toast 或默认加载状态 |
request |
request loadEnum loadConfig |
Future<dynamic> Function() LoadEnum LoadConfig? |
处理数据请求并自动管理加载状态 |
处理数据错误。
class AppDataError extends Error {
static String errorDataMsg = "数据错误,请稍后再试!";
String toString() {
return errorDataMsg;
}
}
处理网络连接错误。
class AppNetError extends Error {
static String errorNetUnConnectionMsg = "无法连接服务器,请检查您的网络环境!";
String toString() {
return errorNetUnConnectionMsg;
}
}
处理网络请求中的错误。
class SuperErrorInterceptor extends Interceptor {
@override
void onError(DioError err, ErrorInterceptorHandler handler) {
// 处理错误逻辑
}
}
添加请求头拦截器。
class SuperHeaderInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
// 添加请求头逻辑
}
}
日志拦截器,用于记录请求和响应日志。
class SuperLogInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
// 记录请求日志
}
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
// 记录响应日志
}
}
处理请求中的 token。
class SuperTokenInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
// 处理 token 逻辑
}
}
日期工具类,提供常用日期处理方法。
class DateUtil {
static String format(DateTime date, {String format = 'yyyy-MM-dd'}) {
// 格式化日期
}
}
日志工具类,提供日志记录方法。
class LogUtil {
static void d(String message) {
// 记录调试日志
}
static void e(String message) {
// 记录错误日志
}
}
数字工具类,提供常用数字处理方法。
class NumUtil {
static double add(double a, double b) {
// 加法运算
}
}
对象工具类,提供常用对象处理方法。
class ObjUtil {
static bool isEmpty(Object? obj) {
// 判断对象是否为空
}
}
为 bool
类型添加扩展方法。
extension BoolExtension on bool {
bool toggle() {
return !this;
}
}
为 BuildContext
添加扩展方法。
extension ContextExtension on BuildContext {
void showDialog(String message) {
// 显示对话框
}
}
为 Function
类型添加扩展方法。
extension FunctionExtension on Function {
void callWithDelay(Duration delay) {
Future.delayed(delay, () => this());
}
}
为 int
类型添加扩展方法。
extension IntExtension on int {
int increment() {
return this + 1;
}
}
为 List
类型添加扩展方法。
extension ListExtension<T> on List<T> {
void addIfNotExist(T value) {
if (!contains(value)) add(value);
}
}
为 Map
类型添加扩展方法。
extension MapExtension<K, V> on Map<K, V> {
V? getOrDefault(K key, V defaultValue) {
return this[key] ?? defaultValue;
}
}
为 Map
类型添加空值处理扩展方法。
extension MapNullExtension<K, V> on Map<K, V?> {
void removeNullValues() {
removeWhere((key, value) => value == null);
}
}
为 String
类型添加扩展方法。
extension StringExtension on String {
bool isNullOrEmpty() {
return this == null || this.isEmpty;
}
}
为任意类型添加扩展方法。
extension GenericExtension<T> on T {
void let(Function(T it) block) {
block(this);
}
}
为 Padding
小部件添加扩展方法。
extension PaddingExtension on Padding {
Padding copyWith(EdgeInsetsGeometry padding) {
return Padding(padding: padding, child: child);
}
}
为 SizedBox
小部件添加扩展方法。
extension SizedBoxExtension on SizedBox {
SizedBox copyWith({double? width, double? height}) {
return SizedBox(width: width ?? this.width, height: height ?? this.height);
}
}
为 Align
小部件添加扩展方法。
extension AlignExtension on Align {
Align copyWith({AlignmentGeometry? alignment, double? widthFactor, double? heightFactor}) {
return Align(alignment: alignment ?? this.alignment, widthFactor: widthFactor ?? this.widthFactor, heightFactor: heightFactor ?? this.heightFactor);
}
}
为 Clip
小部件添加扩展方法。
extension ClipExtension on Clip {
Clip copyWith({Clip? clipBehavior}) {
return Clip(clipBehavior: clipBehavior ?? this.clipBehavior);
}
}
为 GestureDetector
小部件添加扩展方法。
extension GestureExtension on GestureDetector {
GestureDetector copyWith({VoidCallback? onTap}) {
return GestureDetector(onTap: onTap ?? this.onTap);
}
}
为 Padding
小部件添加扩展方法。
extension PaddingExtension on Padding {
Padding copyWith(EdgeInsetsGeometry padding) {
return Padding(padding: padding, child: child);
}
}
为 Sliver
小部件添加扩展方法。
extension SliverExtension on Sliver {
Sliver copyWith({Widget? sliver}) {
return Sliver(sliver: sliver ?? this.sliver);
}
}