This repository was archived by the owner on Jun 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpreview_layer.dart
201 lines (179 loc) · 5.31 KB
/
preview_layer.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
import 'package:flutter/material.dart';
import 'package:magiceye/magiceye.dart';
typedef PreviewLayerBuilder = Widget Function(
BuildContext,
PreviewLayerContext,
);
/// Predefined preview layers to be used on [MagicEye] constructor.
///
/// Example:
/// ```dart
///MaterialPageRoute(
/// builder: (context) => MagicEye(
/// previewLayer: PreviewLayer.circleFocus(scale: 2 / 3),
/// ),
///),
/// ```
abstract class PreviewLayer {
// This class should not be instantiated.
factory PreviewLayer._() => null;
/// Generates a layer that focus on a rectangular area that respects the preview's aspect ratio.
///
/// The [color] parameter defaults to a transparent black, and the focus
/// size will be proportionally derived from [scale].
static PreviewLayerBuilder aspectFocus({
final double scale = 0.5,
final Color color = Colors.black38,
}) =>
(final BuildContext context, PreviewLayerContext layerContext) =>
ClipPath(
clipper: _RectClipper(
rectBuilder: (size) => Rect.fromCenter(
center: Offset(size.width / 2, size.height / 2),
width: size.width * scale,
height: size.height * scale,
),
),
child: Container(
color: color,
),
);
/// Generates a layer that focus on a square area.
///
/// The [color] parameter defaults to a transparent black, and the focus
/// size will be proportionally derived from the [scale] applied to the
/// shortest side of the preview size.
static PreviewLayerBuilder squareFocus({
final double scale = 0.5,
final Color color = Colors.black38,
}) =>
(final BuildContext context, PreviewLayerContext layerContext) =>
ClipPath(
clipper: _RectClipper(
rectBuilder: _squareRectBuilder(scale),
),
child: Container(
color: color,
),
);
/// Generates a layer that focus on a circular area.
///
/// The [color] parameter defaults to a transparent black, and the focus
/// size will be proportionally derived from [size].
static PreviewLayerBuilder circleFocus({
final double scale = 0.5,
final Color color = Colors.black38,
}) =>
(final BuildContext context, PreviewLayerContext layerContext) =>
ClipPath(
clipper: _RoundRectClipper(
rectBuilder: _squareRectBuilder(scale),
),
child: Container(
color: color,
),
);
/// Generates a layer with a centered [image].
///
/// The image will not scale unless a different [scale] is passed.
static PreviewLayerBuilder image(
final Image image, {
final double scale = 1,
}) =>
(final BuildContext context, PreviewLayerContext layerContext) => Center(
child: Transform.scale(
scale: scale,
child: image,
),
);
/// Generate a grid layer with [color].
static PreviewLayerBuilder grid({
Color color = Colors.white30,
}) =>
(final BuildContext context, PreviewLayerContext layerContext) =>
CustomPaint(
painter: _GridPainter(color: color),
child: Container(),
);
/// Builds the builder for square rects.
static Rect Function(Size) _squareRectBuilder(final double scale) =>
(final Size size) => Rect.fromCenter(
center: Offset(size.width / 2, size.height / 2),
width: size.shortestSide * scale,
height: size.shortestSide * scale,
);
}
class _RectClipper extends CustomClipper<Path> {
final Rect Function(Size size) rectBuilder;
const _RectClipper({
@required this.rectBuilder,
});
@override
Path getClip(Size size) => Path()
..addRect(rectBuilder(size))
..addRect(
Rect.fromLTWH(
0,
0,
size.width,
size.height,
),
)
..fillType = PathFillType.evenOdd;
@override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
class _RoundRectClipper extends CustomClipper<Path> {
final Rect Function(Size size) rectBuilder;
const _RoundRectClipper({
@required this.rectBuilder,
});
@override
Path getClip(Size size) => Path()
..addOval(rectBuilder(size))
..addRect(
Rect.fromLTWH(
0,
0,
size.width,
size.height,
),
)
..fillType = PathFillType.evenOdd;
@override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
class _GridPainter extends CustomPainter {
final Color color;
_GridPainter({this.color});
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..strokeWidth = 1
..color = color;
// Draw horizontal lines
canvas.drawLine(
Offset(0, size.height / 3),
Offset(size.width, size.height / 3),
paint,
);
canvas.drawLine(
Offset(0, size.height / 3 * 2),
Offset(size.width, size.height / 3 * 2),
paint,
);
// Draw vertical lines
canvas.drawLine(
Offset(size.width / 3, 0),
Offset(size.width / 3, size.height),
paint,
);
canvas.drawLine(
Offset(size.width / 3 * 2, 0),
Offset(size.width / 3 * 2, size.height),
paint,
);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}