123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- /// 自动导入ValueNotifier拓展
- export 'package:lszlgl/ext/value_notifier_ext.dart';
- ///
- /// VM基类
- abstract class BaseVM {
- /// 是否被销毁
- bool _dispose = false;
- get isDispose => _dispose;
- /// 销毁时回调方法
- void dispose() {
- _dispose = true;
- }
- }
- ///
- /// 数据状态装饰类
- ///
- /// ```
- /// 简单使用方法:
- /// // 数据
- /// var user = DataStatusModel<UserBean>();
- /// user = user.empty();
- /// user = user.loading();
- /// user = user.success(UserBean());
- /// user = user.error('网络异常');
- /// // 组件
- /// setState({});
- /// switch (user.status) {
- /// ...
- /// }
- /// ```
- ///
- /// ```
- /// 配合ValueNotifierExt使用:
- /// // 数据
- /// final user = DataStatusModel<UserBean>().notifier<DataStatusModel<UserBean>>();
- /// user.update(user.value.empty());
- /// user.update(user.value.loading());
- /// user.update(user.value.success(UserBean()));
- /// user.update(user.value.error('网络异常'));
- /// // 组件
- /// user.builder((v) {
- /// switch (v.status) {
- /// ...
- /// }
- /// }
- /// ```
- class DataStatusModel<T> {
- /// 数据状态
- DataStatus status;
- /// 数据内容
- T? data;
- /// 错误信息
- Object? errorData;
- DataStatusModel({
- this.status = DataStatus.empty,
- this.data,
- this.errorData,
- });
- /// 加载中
- DataStatusModel<T> loading({T? data, Object? error}) => DataStatusModel<T>(
- status: DataStatus.loading,
- data: data ?? this.data,
- errorData: error ?? this.errorData,
- );
- /// 加载成功
- DataStatusModel<T> success({T? data, Object? error}) => DataStatusModel<T>(
- status: DataStatus.success,
- data: data ?? this.data,
- errorData: error ?? this.errorData,
- );
- /// 无数据
- DataStatusModel<T> empty({T? data, Object? error}) => DataStatusModel<T>(
- data: data ?? this.data,
- errorData: error ?? this.errorData,
- );
- /// 加载失败
- DataStatusModel<T> error({T? data, Object? error}) => DataStatusModel<T>(
- status: DataStatus.error,
- data: data ?? this.data,
- errorData: error ?? this.errorData,
- );
- }
- /// 数据状态
- enum DataStatus {
- /// 无数据
- empty,
- /// 加载中
- loading,
- /// 加载成功
- success,
- /// 加载失败
- error,
- }
|