flutter_in_action_2nd icon indicating copy to clipboard operation
flutter_in_action_2nd copied to clipboard

fix: 4.7 alignment 坐标转换

Open Nzzz964 opened this issue 3 years ago • 1 comments

Chapter 4.7 坐标转换公式有误

https://api.flutter-io.cn/flutter/widgets/Align-class.html

FractionalOffset

The FractionalOffset used in the following example defines two points:

  • (0.2 * width of FlutterLogo, 0.6 * height of FlutterLogo) = (12.0, 36.0) in the coordinate system of the blue container.
  • (0.2 * width of Align, 0.6 * height of Align) = (24.0, 72.0) in the coordinate system of the Align widget.

The Align widget positions the FlutterLogo such that the two points are on top of each other. In this example, the top left of the FlutterLogo will be placed at (24.0, 72.0) - (12.0, 36.0) = (12.0, 36.0) from the top left of the Align widget.

根据官方文档可知公式为:

x = FractionalOffse.x * parentWidth - FractionalOffse.x * childWidth
y = FractionalOffse.y * parentHeight - FractionalOffse.y * chilHeight

Alignment

The Alignment used in the following example defines a single point:

官方文档好像也有错误,我经过测试得出的结论为:实际的坐标也需要根据父组件计算 公式为:

x = Alignment.x * (parentWidth - childWidth) / 2 + (parentWidth - childWidth) / 2
y = Alignment.y * (parentHeight - childHeight) / 2 + (parentHeight - childHeight) / 2

测试环境

Flutter 3.0.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision f1875d570e (4 weeks ago) • 2022-07-13 11:24:16 -0700
Engine • revision e85ea0e79c
Tools • Dart 2.17.6 • DevTools 2.12.2

Nzzz964 avatar Aug 11 '22 18:08 Nzzz964

associate issue #93

Nzzz964 avatar Aug 11 '22 18:08 Nzzz964