onLayout method

  1. @override
void onLayout(
  1. double left,
  2. double top,
  3. double right,
  4. double bottom,
)
override

Implementation

@override
void onLayout(double left, double top, double right, double bottom) {
  final double leftOffset = props.leftPadding.convert(width);
  final double topOffset = props.topPadding.convert(height);
  final double rightOffset = props.rightPadding.convert(height);
  final double bottomOffset = props.bottomPadding.convert(height);

  double w = width - leftOffset - rightOffset;
  double h = height - topOffset - bottomOffset;
  bool horizontal = props.direction == Direction.horizontal;
  double size = (horizontal ? w : h);

  int expandCount = 0;
  int unExpandCount = 0;
  _axisMap.forEach((key, value) {
    if (value.show) {
      if (value.expanded) {
        expandCount += 1;
      } else {
        unExpandCount += 1;
      }
    }
  });
  num unExpandAllSize = props.expandWidth * unExpandCount;
  num remainSize = size - unExpandAllSize;
  double interval;
  if (expandCount > 0) {
    interval = remainSize / expandCount;
  } else {
    interval = 0;
  }
  double offsetP = horizontal ? leftOffset : topOffset;

  ///计算在不同布局方向上前后占用的最大高度或者宽度
  List<Size> textSize = measureAxisNameTextMaxSize(_axisMap.keys, props.direction, max(interval, props.expandWidth));

  for (var axis in props.axisList) {
    var node = _axisMap[axis]!;
    if (!node.show) {
      continue;
    }
    double tmpLeft;
    double tmpTop;
    double tmpRight;
    double tmpBottom;
    if (horizontal) {
      tmpLeft = offsetP;
      tmpRight = tmpLeft + (node.expanded ? interval : props.expandWidth);
      tmpTop = topOffset;
      tmpBottom = h;
      offsetP += (tmpRight - tmpLeft);
    } else {
      tmpLeft = leftOffset;
      tmpTop = offsetP;
      tmpRight = width - rightOffset;
      tmpBottom = tmpTop + (node.expanded ? interval : props.expandWidth);
      offsetP += (node.expanded ? interval : props.expandWidth);
    }

    ///处理轴内部
    Rect rect = Rect.fromLTRB(tmpLeft, tmpTop, tmpRight, tmpBottom);
    List<DynamicData> dataSet = [];
    for (var ele in children) {
      if (ele is ParallelChild) {
        var child = ele as ParallelChild;
        dataSet.addAll(child.getDimDataSet(node.index));
      }
    }
    Offset start = rect.topLeft;
    Offset end = (props.direction == Direction.horizontal) ? rect.bottomLeft : rect.topRight;
    if (props.direction == Direction.horizontal) {
      start = start.translate(0, textSize[0].height);
      end = end.translate(0, -textSize[1].height);
    } else {
      start = start.translate(textSize[0].width, 0);
      end = end.translate(-textSize[1].width, 0);
    }

    LineProps layoutProps = LineProps(rect, start, end, textStartSize: textSize[0], textEndSize: textSize[1]);
    node.layout(layoutProps, dataSet);
  }

  for (var ele in children) {
    ele.layout(0, 0, width, height);
  }
}