flutter_in_action_2nd
flutter_in_action_2nd copied to clipboard
fix: 4.7 alignment 坐标转换
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:
- (0.2 * width of FlutterLogo/2 + width of FlutterLogo/2, 0.6 * height of FlutterLogo/2 + height of FlutterLogo/2) = (36.0, 48.0).
官方文档好像也有错误,我经过测试得出的结论为:实际的坐标也需要根据父组件计算 公式为:
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
associate issue #93