loading_more_list icon indicating copy to clipboard operation
loading_more_list copied to clipboard

itemBuilder返回的index改变后,没法区分占位index

Open pengfei2015 opened this issue 2 years ago • 12 comments

Content

+ final int actualIndex = getActualIndex?.call(index) ?? index; return itemBuilder( context, - sourceList[getActualIndex?.call(index) ?? index], - index, + source.elementAt(actualIndex), + actualIndex, ); 大佬这样改后,返回的index含义改动了啊。这样没有原始的index返回,当增加占位单元格的时候,没有办法区分占位index。

pengfei2015 avatar Dec 19 '23 14:12 pengfei2015

有没有能重现问题的 demo

zmtzawqlp avatar Dec 20 '23 01:12 zmtzawqlp

有没有能重现问题的 demo

数据源

  @override
  Future<bool> loadData([bool isLoadMoreAction = false]) async {
    bool isSuccess = false;
    try {
      clear();
      addAll([
        MediaItem(id: "1"),
        MediaItem(id: "2"),
        MediaItem(id: "3"),
        MediaItem(id: "4"),
        MediaItem(id: "5"),
        MediaItem(id: "6")
      ]);
      _hasMore = false;
      // _hasMore = feedList.isNotEmpty;
      _pageIndex++;
      isSuccess = true;
    } catch (exception, stack) {
      isSuccess = false;
      print(exception);
      print(stack);
    }
    return isSuccess;
  }

View

@override
  Widget build(BuildContext context) {
    return LoadingMoreList<MediaItem>(
      ListConfig<MediaItem>(
        itemBuilder: (context, item, index) {
          return Container(
            color: Colors.blue,
            child: Text(item.id),
          );
        },
        sourceList: listSourceRepository,
        padding: const EdgeInsets.all(0.0),
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 4,
          childAspectRatio: 1,
          mainAxisSpacing: 1,
          crossAxisSpacing: 1,
        ),
        extendedListDelegate: ExtendedListDelegate(closeToTrailing: true),
        getActualIndex: (index) {
          final colum = index ~/ 4;
          final row = 3 - (index % 4);
          int i = colum * 4 + row;
          return max(0, i - 2);
        },
        itemCountBuilder: (count) {
          return count + 2;
        },
        reverse: true,
      ),
    );
  }

pengfei2015 avatar Dec 21 '23 12:12 pengfei2015

image 最后两个Cell,是要隐藏的。现在没有原始index,不能定位最后的两个Cell了。

pengfei2015 avatar Dec 21 '23 12:12 pengfei2015

不知道你这个需求想做啥。。是不是思路有问题?

zmtzawqlp avatar Dec 22 '23 01:12 zmtzawqlp

不知道你这个需求想做啥。。是不是思路有问题?

就是一个相册选择器,只是显示方式不同。

pengfei2015 avatar Dec 22 '23 13:12 pengfei2015

不知道你这个需求想做啥。。是不是思路有问题?

就是一个相册选择器,只是显示方式不同。

具体逻辑?

zmtzawqlp avatar Dec 23 '23 00:12 zmtzawqlp

不知道你这个需求想做啥。。是不是思路有问题?

就是一个相册选择器,只是显示方式不同。

具体逻辑?

从左上到右下,根据图片的日期排列,最新的图片靠在右下的位置。就是上面的截图,数字越小表示图片越新,最后两个1是占位单元格,本身是没有内容的。source和ItemCount的数量不一样, 但是因为itemBuilder的回调,必定需要从根据index从source取值,会造成数组越界。所以占位的下标重置成0了啊。

就类似微信朋友圈发图片的九宫格排序,把个数限制取消。

pengfei2015 avatar Dec 23 '23 06:12 pengfei2015

建议你看看 https://github.com/fluttercandies/flutter_wechat_assets_picker 的实现

zmtzawqlp avatar Dec 23 '23 08:12 zmtzawqlp

建议你看看 https://github.com/fluttercandies/flutter_wechat_assets_picker 的实现

不是这个意思,你这个4.1.0版本添加的这个 add getActualIndex in case child count != source.length。解决的就是类似列表添加占位格,配置特殊的header和footer时数据源取值问题吧。 6.0版本更改后,我使用了getActualIndex能从sourceList取值了,但是childCount是 > getActualIndex的最大值的吧。 那么在itemBuilder中,我获取不到实际的index,我怎么配置特殊的header和footer呢?

而且itemBuilder这个地方,返回的Item和index感觉就比较重复吧,返回Item解决的是什么问题呢?

pengfei2015 avatar Dec 25 '23 11:12 pengfei2015

配置特殊的header和footer ,你自己指定就好了吧? 比如你是 class A, 你header footer 传一个 HeaderA FooterA 进去呢? 你这个 index 逻辑弄的太乱了。你懂我的意思吗

zmtzawqlp avatar Dec 26 '23 01:12 zmtzawqlp

配置特殊的header和footer ,你自己指定就好了吧? 比如你是 class A, 你header footer 传一个 HeaderA FooterA 进去呢? 你这个 index 逻辑弄的太乱了。你懂我的意思吗

不好意思,在这个ListConfig中,我没有找到可以传递classA的地方啊。总不能塞入sourceList里吧,在itemBuilder里判断Item的类型?

现在一个很奇怪的地方就是,config提供了一个itemCountBuilder,可以配置childCount 不等于 sourceList的个数。但是在Itembuild中直接内部从sourceList取值。造成如果不配置getActualIndex的话,就会数据越界。配置后解决了数组越界,没地方配置多出来的单元格了。

这个我感觉设计思路上本身就是相背的,关于index的逻辑问题,在iOS开发中,Itembuild返回的只有index,没有Item。感觉可以类似的做法吧,也就不需要getActualIndex这个参数了。

pengfei2015 avatar Dec 26 '23 04:12 pengfei2015

在这个ListConfig中,我没有找到可以传递classA的地方啊。总不能塞入sourceList里吧,在itemBuilder里判断Item的类型?

是的。根据不同 item来处理

zmtzawqlp avatar Dec 26 '23 05:12 zmtzawqlp