Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

让热重载时不刷新list #43 #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

git-xiaocao
Copy link

No description provided.

@git-xiaocao
Copy link
Author

这样有个问题 修改LoadingMoreList的参数时 不会刷新,我正在这样用sourceList只初始化一次(插件代码未修改)
image

@git-xiaocao
Copy link
Author

我的想法是 在 继承 LoadingMoreBase 的类中 重载==
didUpdateWidget中判断sourceList参数是否改变来决定是否重新加载

@zmtzawqlp
Copy link
Member

热重载更新ui不是很正常的事情吗?

@git-xiaocao
Copy link
Author

UI没有更新 是数据要重新加载 热重装需要重新请求接口加载数据 等请求返回 正常吗?

@zmtzawqlp
Copy link
Member

你先了解下热重载做什么些啥,然后哪些情况下热重载无效

@mdddj
Copy link

mdddj commented Aug 31, 2022

我也遇到了. loadData正常请求完毕, 返回了true, 但是UI界面没有更新, 点击热重载后才正常展示UI

@zmtzawqlp
Copy link
Member

我也遇到了. loadData正常请求完毕, 返回了true, 但是UI界面没有更新, 点击热重载后才正常展示UI

请使用最新版本

@mdddj
Copy link

mdddj commented Aug 31, 2022

问题解决了,感觉是我的用法有问题,我把refresh方法注释掉恢复正常

///照片墙加载更多
class UserPictureLoadmoreObject extends LoadingMoreBase<UserPictureModel> {
  final String username;
  UserPictureLoadmoreObject(this.username);
  int pageindex = 1;
  bool hasmore = true;

  // @override
  // Future<bool> refresh([bool notifyStateChanged = false]) async {
  //   pageindex = 1;
  //   hasmore = true;
  //   await loadData();
  //   return super.refresh(notifyStateChanged);
  // }

  @override
  Future<bool> loadData([bool isloadMoreAction = false]) async {
    final r = await (UsersPicturesApi()
          ..username = username
          ..page = pageindex)
        .request(showDefaultLoading: false);
    if (r.success) {
      final lists = (r!.data as List<dynamic>).asList;
      if (pageindex == 1) {
        clear();
        add(UserPictureModel.addButton());
      }
      final models = List<UserPictureModel>.from(lists.map((e) => UserPictureModel.fromMap(e))).toList();
      addAll(models);
      hasmore = lists.isNotEmpty;
      pageindex++;
      mylog('加载照片墙列表正常结束:$length ${r.data}');
      return true;
    } else {
      mylog('加载照片墙出现问题');
      return false;
    }
  }

  @override
  bool get hasMore => hasmore;
}

///照片墙列表
class UserPictureListWidget extends StatelessWidget {
  final UserPictureLoadmoreObject repo;

  const UserPictureListWidget({Key? key, required this.repo}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return LoadingMoreList(ListConfig<UserPictureModel>(
        itemBuilder: (c, item, index) {
          if (item.isAddButtonWidget) {
            return AddButton(repo);
          }
          return ExtendedImage.network(
            '${item.url}=detail',
            width: double.infinity,
            height: double.infinity,
            shape: BoxShape.rectangle,
            borderRadius: 8.borderRadius,
            fit: BoxFit.cover,
            loadStateChanged: (s) => kImageLoadStateWidget(s, double.infinity, width: double.infinity, height: double.infinity),
          ).aspectRatio(112 / 150).bg(Colors.grey.shade100).clipRadius(8);
        },
        sourceList: repo,
        extendedListDelegate: const SliverWaterfallFlowDelegateWithFixedCrossAxisCount(crossAxisCount: 3, mainAxisSpacing: 12, crossAxisSpacing: 12),
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        indicatorBuilder: (c, s) => kBuildIndicator(c, s, resp: repo)));
  }
}

class AddButton extends ConsumerWidget {
  final UserPictureLoadmoreObject repo;

  const AddButton(this.repo, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return AspectRatio(
      aspectRatio: 112 / 150,
      child: Container(
        alignment: Alignment.center,
        decoration: BoxDecoration(color: const Color(0xffF2F2F4), borderRadius: 8.borderRadius),
        child: const Icon(
          Icons.add,
          size: 30,
        ),
      ),
    ).click(() async {
      try {
        UrlResult? result;
        final success = await UploadImageUtil.uploadImage(context, UploadType.photoWall, PictureWallUploadApi(), urlResult: (v) {
          result = v;
        });
        if (success == true) {
          toast('添加成功');
          repo.refresh();
        }
      } on ApiException catch (e) {
        context.showSimpleDialog(e.msg);
      }
    });
  }
}

class UserPictureModel {
  final int id;
  final String url;

  bool get isAddButtonWidget {
    return id == -1;
  }

  UserPictureModel({
    required this.id,
    required this.url,
  });

  UserPictureModel copyWith({
    int? id,
    String? url,
  }) {
    return UserPictureModel(
      id: id ?? this.id,
      url: url ?? this.url,
    );
  }

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'id': id,
      'url': url,
    };
  }

  factory UserPictureModel.addButton() {
    return UserPictureModel(id: -1, url: "");
  }

  factory UserPictureModel.fromMap(Map<String, dynamic> map) {
    return UserPictureModel(
      id: map['id'] as int,
      url: map['url'] as String,
    );
  }

  String toJson() => json.encode(toMap());

  factory UserPictureModel.fromJson(String source) => UserPictureModel.fromMap(json.decode(source) as Map<String, dynamic>);

  @override
  String toString() => 'UserPictureModel(id: $id, url: $url)';

  @override
  bool operator ==(covariant UserPictureModel other) {
    if (identical(this, other)) return true;

    return other.id == id && other.url == url;
  }

  @override
  int get hashCode => id.hashCode ^ url.hashCode;
}

@zmtzawqlp
Copy link
Member

问题解决了,感觉是我的用法有问题,我把refresh方法注释掉恢复正常

///照片墙加载更多
class UserPictureLoadmoreObject extends LoadingMoreBase<UserPictureModel> {
  final String username;
  UserPictureLoadmoreObject(this.username);
  int pageindex = 1;
  bool hasmore = true;

  // @override
  // Future<bool> refresh([bool notifyStateChanged = false]) async {
  //   pageindex = 1;
  //   hasmore = true;
  //   await loadData();
  //   return super.refresh(notifyStateChanged);
  // }

  @override
  Future<bool> loadData([bool isloadMoreAction = false]) async {
    final r = await (UsersPicturesApi()
          ..username = username
          ..page = pageindex)
        .request(showDefaultLoading: false);
    if (r.success) {
      final lists = (r!.data as List<dynamic>).asList;
      if (pageindex == 1) {
        clear();
        add(UserPictureModel.addButton());
      }
      final models = List<UserPictureModel>.from(lists.map((e) => UserPictureModel.fromMap(e))).toList();
      addAll(models);
      hasmore = lists.isNotEmpty;
      pageindex++;
      mylog('加载照片墙列表正常结束:$length ${r.data}');
      return true;
    } else {
      mylog('加载照片墙出现问题');
      return false;
    }
  }

  @override
  bool get hasMore => hasmore;
}

///照片墙列表
class UserPictureListWidget extends StatelessWidget {
  final UserPictureLoadmoreObject repo;

  const UserPictureListWidget({Key? key, required this.repo}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return LoadingMoreList(ListConfig<UserPictureModel>(
        itemBuilder: (c, item, index) {
          if (item.isAddButtonWidget) {
            return AddButton(repo);
          }
          return ExtendedImage.network(
            '${item.url}=detail',
            width: double.infinity,
            height: double.infinity,
            shape: BoxShape.rectangle,
            borderRadius: 8.borderRadius,
            fit: BoxFit.cover,
            loadStateChanged: (s) => kImageLoadStateWidget(s, double.infinity, width: double.infinity, height: double.infinity),
          ).aspectRatio(112 / 150).bg(Colors.grey.shade100).clipRadius(8);
        },
        sourceList: repo,
        extendedListDelegate: const SliverWaterfallFlowDelegateWithFixedCrossAxisCount(crossAxisCount: 3, mainAxisSpacing: 12, crossAxisSpacing: 12),
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        indicatorBuilder: (c, s) => kBuildIndicator(c, s, resp: repo)));
  }
}

class AddButton extends ConsumerWidget {
  final UserPictureLoadmoreObject repo;

  const AddButton(this.repo, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return AspectRatio(
      aspectRatio: 112 / 150,
      child: Container(
        alignment: Alignment.center,
        decoration: BoxDecoration(color: const Color(0xffF2F2F4), borderRadius: 8.borderRadius),
        child: const Icon(
          Icons.add,
          size: 30,
        ),
      ),
    ).click(() async {
      try {
        UrlResult? result;
        final success = await UploadImageUtil.uploadImage(context, UploadType.photoWall, PictureWallUploadApi(), urlResult: (v) {
          result = v;
        });
        if (success == true) {
          toast('添加成功');
          repo.refresh();
        }
      } on ApiException catch (e) {
        context.showSimpleDialog(e.msg);
      }
    });
  }
}

class UserPictureModel {
  final int id;
  final String url;

  bool get isAddButtonWidget {
    return id == -1;
  }

  UserPictureModel({
    required this.id,
    required this.url,
  });

  UserPictureModel copyWith({
    int? id,
    String? url,
  }) {
    return UserPictureModel(
      id: id ?? this.id,
      url: url ?? this.url,
    );
  }

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'id': id,
      'url': url,
    };
  }

  factory UserPictureModel.addButton() {
    return UserPictureModel(id: -1, url: "");
  }

  factory UserPictureModel.fromMap(Map<String, dynamic> map) {
    return UserPictureModel(
      id: map['id'] as int,
      url: map['url'] as String,
    );
  }

  String toJson() => json.encode(toMap());

  factory UserPictureModel.fromJson(String source) => UserPictureModel.fromMap(json.decode(source) as Map<String, dynamic>);

  @override
  String toString() => 'UserPictureModel(id: $id, url: $url)';

  @override
  bool operator ==(covariant UserPictureModel other) {
    if (identical(this, other)) return true;

    return other.id == id && other.url == url;
  }

  @override
  int get hashCode => id.hashCode ^ url.hashCode;
}

确实。super 里面已经做了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants