This repository was archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathtransformations_demo.dart
245 lines (219 loc) · 7.48 KB
/
transformations_demo.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2019 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';
import 'transformations_demo_board.dart';
import 'transformations_demo_edit_board_point.dart';
// BEGIN transformationsDemo#1
class TransformationsDemo extends StatefulWidget {
const TransformationsDemo({super.key});
@override
State<TransformationsDemo> createState() => _TransformationsDemoState();
}
class _TransformationsDemoState extends State<TransformationsDemo>
with TickerProviderStateMixin {
final GlobalKey _targetKey = GlobalKey();
// The radius of a hexagon tile in pixels.
static const _kHexagonRadius = 16.0;
// The margin between hexagons.
static const _kHexagonMargin = 1.0;
// The radius of the entire board in hexagons, not including the center.
static const _kBoardRadius = 8;
Board _board = Board(
boardRadius: _kBoardRadius,
hexagonRadius: _kHexagonRadius,
hexagonMargin: _kHexagonMargin,
);
final TransformationController _transformationController =
TransformationController();
Animation<Matrix4>? _animationReset;
late AnimationController _controllerReset;
Matrix4? _homeMatrix;
// Handle reset to home transform animation.
void _onAnimateReset() {
_transformationController.value = _animationReset!.value;
if (!_controllerReset.isAnimating) {
_animationReset?.removeListener(_onAnimateReset);
_animationReset = null;
_controllerReset.reset();
}
}
// Initialize the reset to home transform animation.
void _animateResetInitialize() {
_controllerReset.reset();
_animationReset = Matrix4Tween(
begin: _transformationController.value,
end: _homeMatrix,
).animate(_controllerReset);
_controllerReset.duration = const Duration(milliseconds: 400);
_animationReset!.addListener(_onAnimateReset);
_controllerReset.forward();
}
// Stop a running reset to home transform animation.
void _animateResetStop() {
_controllerReset.stop();
_animationReset?.removeListener(_onAnimateReset);
_animationReset = null;
_controllerReset.reset();
}
void _onScaleStart(ScaleStartDetails details) {
// If the user tries to cause a transformation while the reset animation is
// running, cancel the reset animation.
if (_controllerReset.status == AnimationStatus.forward) {
_animateResetStop();
}
}
void _onTapUp(TapUpDetails details) {
final renderBox =
_targetKey.currentContext!.findRenderObject() as RenderBox;
final offset =
details.globalPosition - renderBox.localToGlobal(Offset.zero);
final scenePoint = _transformationController.toScene(offset);
final boardPoint = _board.pointToBoardPoint(scenePoint);
setState(() {
_board = _board.copyWithSelected(boardPoint);
});
}
@override
void initState() {
super.initState();
_controllerReset = AnimationController(
vsync: this,
);
}
@override
Widget build(BuildContext context) {
// The scene is drawn by a CustomPaint, but user interaction is handled by
// the InteractiveViewer parent widget.
return Scaffold(
backgroundColor: Theme.of(context).colorScheme.primary,
appBar: AppBar(
automaticallyImplyLeading: false,
title:
Text(GalleryLocalizations.of(context)!.demo2dTransformationsTitle),
),
body: Container(
color: backgroundColor,
child: LayoutBuilder(
builder: (context, constraints) {
// Draw the scene as big as is available, but allow the user to
// translate beyond that to a visibleSize that's a bit bigger.
final viewportSize = Size(
constraints.maxWidth,
constraints.maxHeight,
);
// Start the first render, start the scene centered in the viewport.
if (_homeMatrix == null) {
_homeMatrix = Matrix4.identity()
..translate(
viewportSize.width / 2 - _board.size.width / 2,
viewportSize.height / 2 - _board.size.height / 2,
);
_transformationController.value = _homeMatrix!;
}
return ClipRect(
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTapUp: _onTapUp,
child: InteractiveViewer(
key: _targetKey,
transformationController: _transformationController,
boundaryMargin: EdgeInsets.symmetric(
horizontal: viewportSize.width,
vertical: viewportSize.height,
),
minScale: 0.01,
onInteractionStart: _onScaleStart,
child: SizedBox.expand(
child: CustomPaint(
size: _board.size,
painter: _BoardPainter(
board: _board,
),
),
),
),
),
),
);
},
),
),
persistentFooterButtons: [resetButton, editButton],
);
}
IconButton get resetButton {
return IconButton(
onPressed: () {
setState(() {
_animateResetInitialize();
});
},
tooltip: 'Reset',
color: Theme.of(context).colorScheme.surface,
icon: const Icon(Icons.replay),
);
}
IconButton get editButton {
return IconButton(
onPressed: () {
if (_board.selected == null) {
return;
}
showModalBottomSheet<Widget>(
context: context,
builder: (context) {
return Container(
width: double.infinity,
height: 150,
padding: const EdgeInsets.all(12),
child: EditBoardPoint(
boardPoint: _board.selected!,
onColorSelection: (color) {
setState(() {
_board = _board.copyWithBoardPointColor(
_board.selected!, color);
Navigator.pop(context);
});
},
),
);
});
},
tooltip: 'Edit',
color: Theme.of(context).colorScheme.surface,
icon: const Icon(Icons.edit),
);
}
@override
void dispose() {
_controllerReset.dispose();
super.dispose();
}
}
// CustomPainter is what is passed to CustomPaint and actually draws the scene
// when its `paint` method is called.
class _BoardPainter extends CustomPainter {
const _BoardPainter({required this.board});
final Board board;
@override
void paint(Canvas canvas, Size size) {
void drawBoardPoint(BoardPoint? boardPoint) {
final color = boardPoint!.color.withOpacity(
board.selected == boardPoint ? 0.7 : 1,
);
final vertices = board.getVerticesForBoardPoint(boardPoint, color);
canvas.drawVertices(vertices, BlendMode.color, Paint());
}
board.forEach(drawBoardPoint);
}
// We should repaint whenever the board changes, such as board.selected.
@override
bool shouldRepaint(_BoardPainter oldDelegate) {
return oldDelegate.board != board;
}
}
// END