calculWrapSpacingChild2 method
流式布局 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;
///计算每个子Widget 的坐标
x = flowWidth - itemChildSize.height * 1.5;
y = normalHeight - dxflag * (i + 1) * 0.9;
///在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.height * 1.5);
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)));
}