-
Notifications
You must be signed in to change notification settings - Fork 53
/
discovery.dart
203 lines (183 loc) · 5.25 KB
/
discovery.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
import 'package:flutter/material.dart';
import 'package:flutter_portal/flutter_portal.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool showDiscovery = false;
int count = 0;
@override
Widget build(BuildContext context) {
return Portal(
child: MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Discovery example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('You have clicked the button this many times:'),
// Text('$count', style: Theme.of(context).textTheme.headline4),
Text('$count'),
ElevatedButton(
onPressed: () => setState(() => showDiscovery = true),
child: const Text('Show discovery'),
)
],
),
),
floatingActionButton: Discovery(
visible: showDiscovery,
description: const Text('Click to increment the counter'),
onClose: () => setState(() => showDiscovery = false),
child: FloatingActionButton(
onPressed: _increment,
child: const Icon(Icons.add),
),
),
),
),
);
}
void _increment() {
setState(() {
showDiscovery = false;
count++;
});
}
}
class Discovery extends StatelessWidget {
const Discovery({
Key? key,
required this.visible,
required this.onClose,
required this.description,
required this.child,
}) : super(key: key);
final Widget child;
final Widget description;
final bool visible;
final VoidCallback onClose;
@override
Widget build(BuildContext context) {
return Barrier(
visible: visible,
onClose: onClose,
child: PortalTarget(
visible: visible,
closeDuration: kThemeAnimationDuration,
anchor: const Aligned(
target: Alignment.center,
follower: Alignment.center,
),
portalFollower: Stack(
children: [
CustomPaint(
painter: HolePainter(Theme.of(context).colorScheme.secondary),
child: TweenAnimationBuilder<double>(
duration: kThemeAnimationDuration,
curve: Curves.easeOut,
tween: Tween(begin: 50, end: visible ? 200 : 50),
builder: (context, radius, _) {
return Padding(
padding: EdgeInsets.all(radius),
child: child,
);
},
),
),
Positioned(
top: 100,
left: 50,
width: 200,
// child: DefaultTextStyle(
// style: Theme.of(context).textTheme.headline5!,
child: TweenAnimationBuilder<double>(
duration: kThemeAnimationDuration,
curve: Curves.easeOut,
tween: Tween(begin: 0, end: visible ? 1 : 0),
builder: (context, opacity, _) {
return Opacity(
opacity: opacity,
child: description,
);
},
),
)
],
),
child: child,
),
);
}
}
class HolePainter extends CustomPainter {
const HolePainter(this.color);
final Color color;
@override
void paint(Canvas canvas, Size size) {
final paint = Paint();
paint.color = color;
final center = Offset(size.width / 2, size.height / 2);
canvas.drawPath(
Path.combine(
PathOperation.difference,
Path()
..addOval(Rect.fromCircle(center: center, radius: size.width / 2)),
Path()
..addOval(Rect.fromCircle(center: center, radius: 40))
..close(),
),
paint,
);
}
@override
bool shouldRepaint(HolePainter oldDelegate) {
return color != oldDelegate.color;
}
}
class Barrier extends StatelessWidget {
const Barrier({
Key? key,
required this.onClose,
required this.visible,
required this.child,
}) : super(key: key);
final Widget child;
final VoidCallback onClose;
final bool visible;
@override
Widget build(BuildContext context) {
return PortalTarget(
visible: visible,
closeDuration: kThemeAnimationDuration,
portalFollower: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: onClose,
child: TweenAnimationBuilder<Color>(
duration: kThemeAnimationDuration,
tween: ColorTween(
begin: Colors.transparent,
end: visible ? Colors.black54 : Colors.transparent,
),
builder: (context, color, child) {
return ColoredBox(color: color);
},
),
),
child: child,
);
}
}
/// Non-nullable version of ColorTween.
class ColorTween extends Tween<Color> {
ColorTween({required Color begin, required Color end})
: super(begin: begin, end: end);
@override
Color lerp(double t) => Color.lerp(begin, end, t)!;
}