calculWrapSpacingChild2 method

void calculWrapSpacingChild2(
  1. FlowPaintingContext context
)

流式布局 Flow 计算

Implementation

void calculWrapSpacingChild2(FlowPaintingContext context) {
  ///初始绘制位置为Flow布局的左上角
  double x = 0.0;
  double y = 0.0;

  //获取当前画布的最小边长,width与height谁小取谁
  double radius = context.size.shortestSide / 3 * 2;

  ///获取当前Flow的大小
  double flowWidth = context.size.width;
  double flowHeight = context.size.height;

  ///左右两个菜单的偏移量
  double dxflag = radius / 3 * radiusRate;

  ///默认将所有的子Widget绘制到左下角
  x = radius;
  y = radius;

  //计算每一个子widget的位置
  for (var i = 0; i < context.childCount - 1; i++) {
    ///获取第i个子Widget的大小
    Size? itemChildSize = context.getChildSize(i);
    if (itemChildSize == null) {
      return;
    }

    ///子child开始绘制的y中心点
    double normalHeight = flowHeight - itemChildSize.height * 2;

    ///子child开始绘制的x中心点
    double normalWidth = flowWidth - itemChildSize.width / 2;

    ///计算每个子Widget 的坐标
    if (i == 0) {
      x = normalWidth / 2 - dxflag;
      y = normalHeight - radius / 4.3 * radiusRate;
    } else if (i == 1) {
      x = normalWidth / 2;
      y = normalHeight - radius / 3 * radiusRate;
    } else {
      x = normalWidth / 2 + dxflag;
      y = normalHeight - radius / 4.3 * radiusRate;
    }

    ///在Flow中进行绘制
    context.paintChild(i,
        transform: new Matrix4.translationValues(x, y, 0.0));
  }

  ///最后一个做为菜单选项
  int lastIndex = context.childCount - 1;
  Size? lastChildSize = context.getChildSize(lastIndex);
  if (lastChildSize == null) {
    return;
  }
  double lastx = (flowWidth - lastChildSize.width / 2) / 2;
  double lasty = flowHeight - lastChildSize.height * 2;

  ///绘制这个菜单在底部中心并旋转
  context.paintChild(lastIndex,

      ///先平移到底部
      transform: Matrix4.translationValues(lastx, lasty, 0.0)

        ///然后将旋转中心平移到子Widget的中心
        ..translate(lastChildSize.width / 2, lastChildSize.height / 2)

        ///合并旋转操作
        ..multiply(Matrix4.rotationZ(radiusRate * 0.8)

          ///再将旋转中心平移回去
          ..translate(-lastChildSize.width / 2, -lastChildSize.height / 2)));
//    context.paintChild(lastIndex,
//        transform: Matrix4Transform()
//            ///绕自身中心旋转
//            .rotateByCenterDegrees(radiusRate*45, lastChildSize)
//            ///平移到底部中心
//            .translate(x: lastx,y: lasty).matrix4);
}