forked from flutter/gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splash.dart
275 lines (249 loc) · 7.42 KB
/
splash.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// 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 'dart:math';
import 'package:dual_screen/dual_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';
import 'package:gallery/constants.dart';
import 'package:gallery/layout/adaptive.dart';
import 'package:gallery/pages/home.dart';
const homePeekDesktop = 210.0;
const homePeekMobile = 60.0;
class SplashPageAnimation extends InheritedWidget {
const SplashPageAnimation({
super.key,
required this.isFinished,
required super.child,
});
final bool isFinished;
static SplashPageAnimation? of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType();
}
@override
bool updateShouldNotify(SplashPageAnimation oldWidget) => true;
}
class SplashPage extends StatefulWidget {
const SplashPage({
super.key,
required this.child,
});
final Widget child;
@override
State<SplashPage> createState() => _SplashPageState();
}
class _SplashPageState extends State<SplashPage>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late int _effect;
final _random = Random();
// A map of the effect index to its duration. This duration is used to
// determine how long to display the splash animation at launch.
//
// If a new effect is added, this map should be updated.
final _effectDurations = {
1: 5,
2: 4,
3: 4,
4: 5,
5: 5,
6: 4,
7: 4,
8: 4,
9: 3,
10: 6,
};
bool get _isSplashVisible {
return _controller.status == AnimationStatus.completed ||
_controller.status == AnimationStatus.forward;
}
@override
void initState() {
super.initState();
// If the number of included effects changes, this number should be changed.
_effect = _random.nextInt(_effectDurations.length) + 1;
_controller =
AnimationController(duration: splashPageAnimationDuration, vsync: this)
..addListener(() {
setState(() {});
});
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Animation<RelativeRect> _getPanelAnimation(
BuildContext context,
BoxConstraints constraints,
) {
final height = constraints.biggest.height -
(isDisplayDesktop(context) ? homePeekDesktop : homePeekMobile);
return RelativeRectTween(
begin: const RelativeRect.fromLTRB(0, 0, 0, 0),
end: RelativeRect.fromLTRB(0, height, 0, 0),
).animate(CurvedAnimation(parent: _controller, curve: Curves.easeInOut));
}
@override
Widget build(BuildContext context) {
return NotificationListener<ToggleSplashNotification>(
onNotification: (_) {
_controller.forward();
return true;
},
child: SplashPageAnimation(
isFinished: _controller.status == AnimationStatus.dismissed,
child: LayoutBuilder(
builder: (context, constraints) {
final animation = _getPanelAnimation(context, constraints);
var frontLayer = widget.child;
if (_isSplashVisible) {
frontLayer = MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
_controller.reverse();
},
onVerticalDragEnd: (details) {
if (details.velocity.pixelsPerSecond.dy < -200) {
_controller.reverse();
}
},
child: IgnorePointer(child: frontLayer),
),
);
}
if (isDisplayDesktop(context)) {
frontLayer = Padding(
padding: const EdgeInsets.only(top: 136),
child: ClipRRect(
borderRadius: const BorderRadius.vertical(
top: Radius.circular(40),
),
child: frontLayer,
),
);
}
if (isDisplayFoldable(context)) {
return TwoPane(
startPane: frontLayer,
endPane: GestureDetector(
onTap: () {
if (_isSplashVisible) {
_controller.reverse();
} else {
_controller.forward();
}
},
child: _SplashBackLayer(
isSplashCollapsed: !_isSplashVisible, effect: _effect),
),
);
} else {
return Stack(
children: [
_SplashBackLayer(
isSplashCollapsed: !_isSplashVisible,
effect: _effect,
onTap: () {
_controller.forward();
},
),
PositionedTransition(
rect: animation,
child: frontLayer,
),
],
);
}
},
),
),
);
}
}
class _SplashBackLayer extends StatelessWidget {
const _SplashBackLayer({
required this.isSplashCollapsed,
required this.effect,
this.onTap,
});
final bool isSplashCollapsed;
final int effect;
final GestureTapCallback? onTap;
@override
Widget build(BuildContext context) {
var effectAsset = 'splash_effects/splash_effect_$effect.gif';
final flutterLogo = Image.asset(
'assets/logo/flutter_logo.png',
package: 'flutter_gallery_assets',
);
Widget? child;
if (isSplashCollapsed) {
if (isDisplayDesktop(context)) {
child = Padding(
padding: const EdgeInsets.only(top: 50),
child: Align(
alignment: Alignment.topCenter,
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
onTap: onTap,
child: flutterLogo,
),
),
),
);
}
if (isDisplayFoldable(context)) {
child = Container(
color: Theme.of(context).colorScheme.background,
child: Stack(
children: [
Center(
child: flutterLogo,
),
Padding(
padding: const EdgeInsets.only(top: 100.0),
child: Center(
child: Text(
GalleryLocalizations.of(context)!.splashSelectDemo,
),
),
)
],
),
);
}
} else {
child = Stack(
children: [
Center(
child: Image.asset(
effectAsset,
package: 'flutter_gallery_assets',
),
),
Center(child: flutterLogo),
],
);
}
return ExcludeSemantics(
child: Material(
// This is the background color of the gifs.
color: const Color(0xFF030303),
child: Padding(
padding: EdgeInsets.only(
bottom: isDisplayDesktop(context)
? homePeekDesktop
: isDisplayFoldable(context)
? 0
: homePeekMobile,
),
child: child,
),
),
);
}
}