Skip to content

Commit b031ad2

Browse files
author
fbchen
committed
fix pinned rotate bug
1 parent 680b62a commit b031ad2

File tree

2 files changed

+87
-15
lines changed

2 files changed

+87
-15
lines changed

example/pinned_position.dart

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
24
import 'package:flutter_constraintlayout/src/constraint_layout.dart';
35

46
import 'custom_app_bar.dart';
57

6-
class PinnedPositionExample extends StatelessWidget {
8+
class PinnedPositionExample extends StatefulWidget {
79
const PinnedPositionExample({Key? key}) : super(key: key);
810

11+
@override
12+
State createState() => PinnedPositionExampleState();
13+
}
14+
15+
class PinnedPositionExampleState extends State<PinnedPositionExample> {
16+
late Timer timer;
17+
int angle = 0;
18+
19+
@override
20+
void initState() {
21+
super.initState();
22+
timer = Timer.periodic(const Duration(milliseconds: 16), (_) {
23+
setState(() {
24+
angle++;
25+
angle %= 360;
26+
});
27+
});
28+
}
29+
30+
@override
31+
void dispose() {
32+
super.dispose();
33+
timer.cancel();
34+
}
35+
936
@override
1037
Widget build(BuildContext context) {
1138
ConstraintId anchor = ConstraintId('anchor');
@@ -26,14 +53,63 @@ class PinnedPositionExample extends StatelessWidget {
2653
Container(
2754
color: Colors.cyan,
2855
).applyConstraint(
29-
size: 40,
56+
size: 100,
3057
pinnedInfo: PinnedInfo(
3158
anchor,
32-
PinnedPos(0, PinnedType.absolute, 0.5, PinnedType.percent),
59+
PinnedPos(0.2, PinnedType.percent, 0.2, PinnedType.percent),
60+
PinnedPos(1, PinnedType.percent, 1, PinnedType.percent),
61+
rotateDegree: angle,
62+
),
63+
),
64+
Container(
65+
color: Colors.orange,
66+
).applyConstraint(
67+
size: 60,
68+
pinnedInfo: PinnedInfo(
69+
anchor,
70+
PinnedPos(1, PinnedType.percent, 1, PinnedType.percent),
71+
PinnedPos(0, PinnedType.percent, 0, PinnedType.percent),
72+
rotateDegree: 360 - angle,
73+
),
74+
),
75+
Container(
76+
color: Colors.black,
77+
).applyConstraint(
78+
size: 60,
79+
pinnedInfo: PinnedInfo(
80+
anchor,
81+
PinnedPos(0.5, PinnedType.percent, 0.5, PinnedType.percent),
3382
PinnedPos(0.5, PinnedType.percent, 0.5, PinnedType.percent),
34-
rotateDegree: 45,
83+
rotateDegree: angle,
84+
),
85+
),
86+
Container(
87+
decoration: const BoxDecoration(
88+
color: Colors.red,
89+
borderRadius: BorderRadius.all(Radius.circular(10)),
90+
),
91+
).applyConstraint(
92+
size: 10,
93+
centerBottomRightTo: anchor,
94+
),
95+
Container(
96+
decoration: const BoxDecoration(
97+
color: Colors.red,
98+
borderRadius: BorderRadius.all(Radius.circular(10)),
3599
),
36-
)
100+
).applyConstraint(
101+
size: 10,
102+
centerTopLeftTo: anchor,
103+
),
104+
Container(
105+
decoration: const BoxDecoration(
106+
color: Colors.red,
107+
borderRadius: BorderRadius.all(Radius.circular(10)),
108+
),
109+
).applyConstraint(
110+
size: 10,
111+
centerTo: anchor,
112+
),
37113
],
38114
),
39115
);

lib/src/constraint_layout.dart

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3780,21 +3780,17 @@ class _ConstraintRenderBox extends RenderBox
37803780

37813781
if (element.pinnedInfo != null) {
37823782
context.canvas.save();
3783+
Offset anchorOffset =
3784+
element.pinnedInfo!.selfPos.resolve(element.getSize());
37833785
context.canvas.translate(
3784-
element.offset.dx +
3785-
offset.dx +
3786-
paintShift.dx +
3787-
element.getMeasuredWidth() / 2,
3788-
element.offset.dy +
3789-
offset.dy +
3790-
paintShift.dy +
3791-
element.getMeasuredHeight() / 2);
3786+
element.offset.dx + offset.dx + paintShift.dx + anchorOffset.dx,
3787+
element.offset.dy + offset.dy + paintShift.dy + anchorOffset.dy);
37923788
context.canvas
37933789
.rotate(pi + pi * (element.pinnedInfo!.rotateDegree / 180));
37943790
context.paintChild(
37953791
element.renderBox!,
3796-
Offset(-element.getMeasuredWidth() / 2,
3797-
-element.getMeasuredHeight() / 2));
3792+
Offset(-element.getMeasuredWidth() + anchorOffset.dx,
3793+
-element.getMeasuredHeight() + anchorOffset.dy));
37983794
context.canvas.restore();
37993795
} else {
38003796
context.paintChild(

0 commit comments

Comments
 (0)