Skip to content

[Bug report] 消息内部有折叠、展开,且滚动位置位于底部(position.pixels==0)的时候,点击展开会向上伸展 #122

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

Closed
urrrich opened this issue Mar 13, 2025 · 1 comment
Assignees

Comments

@urrrich
Copy link

urrrich commented Mar 13, 2025

Version

1.25.1

Platforms

dart

Device Model

iPhone 12(iOS 18)

flutter info

flutter 3.27.4

How to reproduce?

消息内部有折叠、展开,且滚动位置位于底部(position.pixels==0)的时候,点击展开会向上伸展

Logs

Example code (optional)

onExpand: (visible) {
     // 不在底部的时候不会有问题
      if (_scrollController.position.pixels == 0) {
        _scrollController.jumpTo(6);
      }

      final isLastItem = index == _messages.length - 1;

      final refItemIndex = isLastItem ? index : index + 1;

      chatObserver.standby(
        mode: ChatScrollObserverHandleMode.specified,
        refIndexType: ChatScrollObserverRefIndexType.itemIndex,
        refItemIndex: refItemIndex,
        refItemIndexAfterUpdate: refItemIndex,
        customAdjustPosition: (model) {
          // 仅处理最后一个 item 的情况
          if (!isLastItem) return null;
          // 使用 变化前后的底部间距差 来保持位置
          final delta = model.newPosition.extentAfter -
              model.oldPosition.extentAfter;
          // debugPrint(model.adjustPosition + delta);
          return model.adjustPosition + delta;
        },
        customAdjustPositionDelta: (model) {
          // 仅处理不是最后一个 item 的情况
          if (isLastItem) return null;

          // 以 变化前后的 item 偏移差 来保持位置
          final adjustPosition = model.adjustPosition;
          final delta = model.currentItemModel.layoutOffset -
              model.observer.refItemLayoutOffset;

          if (delta < 0) {
            // 收起
            // 因消息的高度太大,在收起时,Flutter 内部对列表的偏移计算有问题
            // 所以这里调整计算方式,改为:视口的当前偏移量 - 内容变化量
            // 注:减去 adjustPosition,是因为保持位置功能的内部会加上 adjustPosition
            return model.currentItemModel.viewportPixels +
                delta -
                adjustPosition;
          }
          // 展开
          return delta;
        },
      );
    },

Contact

No response

@LinXunFeng
Copy link
Member

了解一下 fixedPositionOffset 的作用 3.1、基本使用

fixedPositionOffset = -1

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

No branches or pull requests

2 participants