performLayout method

  1. @override
void performLayout()
override

Do the work of computing the layout for this render object.

Do not call this function directly: call layout instead. This function is called by layout when there is actually work to be done by this render object during layout. The layout constraints provided by your parent are available via the constraints getter.

If sizedByParent is true, then this function should not actually change the dimensions of this render object. Instead, that work should be done by performResize. If sizedByParent is false, then this function should both change the dimensions of this render object and instruct its children to layout.

In implementing this function, you must call layout on each of your children, passing true for parentUsesSize if your layout information is dependent on your child's layout information. Passing true for parentUsesSize ensures that this render object will undergo layout if the child undergoes layout. Otherwise, the child can change its layout information without informing this render object.

Implementation

@override
void performLayout() {
  assert(childCount == 2);

  //layout two child
  BoxConstraints exactlyConstraints = constraints.loosen();
  header.layout(exactlyConstraints, parentUsesSize: true);
  content.layout(exactlyConstraints, parentUsesSize: true);

  //header's size should not large than content's size.
  double headerLogicalExtent = _overlapsContent ? 0 : header.size.height;

  double width =
      max(constraints.minWidth, max(header.size.width, content.size.width));
  double height = max(constraints.minHeight,
      max(header.size.height, headerLogicalExtent + content.size.height));
  size = Size(width, height);
  assert(size.width == constraints.constrainWidth(width));
  assert(size.height == constraints.constrainHeight(height));

  //calc content offset
  positionChild(content, Offset(0, headerLogicalExtent));

  checkRefreshContainerOffset();

  double sliverListOffset = _getSliverListVisibleScrollOffset();
  double currContainerOffset = -1;
  if (_listIndex < _controller.containerOffsets.length) {
    currContainerOffset = _controller.containerOffsets[_listIndex]!;
  }
  bool containerPainted = (_listIndex == 0 && currContainerOffset == 0) ||
      currContainerOffset > 0;
  if (!containerPainted) {
    positionChild(header, Offset.zero);
    return;
  }
  double minScrollOffset = _listIndex >= _controller.containerOffsets.length
      ? 0
      : _controller.containerOffsets[_listIndex]!;
  double maxScrollOffset = minScrollOffset + size.height;

  //when [ExpandableSectionContainer] size changed, SliverList may give a wrong
  // layoutOffset at first time, so check offsets for store right layoutOffset
  // in [containerOffsets].
  if (_listIndex < _controller.containerOffsets.length) {
    currContainerOffset = _controller.containerOffsets[_listIndex]!;
    int nextListIndex = _listIndex + 1;
    if (nextListIndex < _controller.containerOffsets.length &&
        _controller.containerOffsets[nextListIndex]! < maxScrollOffset) {
      _controller.containerOffsets =
          _controller.containerOffsets.sublist(0, nextListIndex);
    }
  }

  if (sliverListOffset > minScrollOffset &&
      sliverListOffset <= maxScrollOffset) {
    if (_stickyIndex != _listIndex) {
      _stickyIndex = _listIndex;
      _controller.updatePercent(_controller.switchingSectionIndex, 1);
      //update sticky index
      _controller.stickySectionIndex = sectionIndex;
    }
  } else if (sliverListOffset <= 0) {
    _controller.stickySectionIndex = -1;
    _stickyIndex = -1;
  } else {
    _stickyIndex = -1;
  }

  //calc header offset
  double currHeaderOffset = 0;
  double headerMaxOffset = height - header.size.height;
  if (_sticky && isStickyChild && sliverListOffset > minScrollOffset) {
    currHeaderOffset = sliverListOffset - minScrollOffset;
  }
 // print(
 //     "index:$listIndex currHeaderOffset:${currHeaderOffset.toStringAsFixed(2)}" +
 //         " sliverListOffset:${sliverListOffset.toStringAsFixed(2)}" +
 //         " [$minScrollOffset,$maxScrollOffset] size:${content.size.height}");
  positionChild(header, Offset(0, min(currHeaderOffset, headerMaxOffset)));

  //callback header hide percent
  if (currHeaderOffset >= headerMaxOffset && currHeaderOffset <= height) {
    double switchingPercent =
        (currHeaderOffset - headerMaxOffset) / header.size.height;
    _controller.updatePercent(sectionIndex, switchingPercent);
  } else if (sliverListOffset < minScrollOffset + headerMaxOffset &&
      _controller.switchingSectionIndex == sectionIndex) {
    //ensure callback 0% percent.
    _controller.updatePercent(sectionIndex, 0);
    //reset switchingSectionIndex
    _controller.updatePercent(-1, 1);
  }
}