-
Notifications
You must be signed in to change notification settings - Fork 363
/
Copy pathapp.dart
188 lines (170 loc) · 5.33 KB
/
app.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
// Copyright (c) 2021, Very Good Ventures
// https://verygood.ventures
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
// ignore_for_file: public_member_api_docs, avoid_print
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:http/http.dart' as http;
import 'package:very_good_slide_puzzle/helpers/helpers.dart';
import 'package:very_good_slide_puzzle/l10n/l10n.dart';
import 'package:very_good_slide_puzzle/puzzle/puzzle.dart';
class App extends StatefulWidget {
const App({Key? key, ValueGetter<PlatformHelper>? platformHelperFactory})
: _platformHelperFactory = platformHelperFactory ?? getPlatformHelper,
super(key: key);
final ValueGetter<PlatformHelper> _platformHelperFactory;
@override
State<App> createState() => _AppState();
}
class _AppState extends State<App> {
/// The path to local assets folder.
static const localAssetsPrefix = 'assets/';
static final audioControlAssets = [
'assets/images/audio_control/simple_on.png',
'assets/images/audio_control/simple_off.png',
'assets/images/audio_control/dashatar_on.png',
'assets/images/audio_control/green_dashatar_off.png',
'assets/images/audio_control/blue_dashatar_off.png',
'assets/images/audio_control/yellow_dashatar_off.png',
];
static final audioAssets = [
'assets/audio/shuffle.mp3',
'assets/audio/click.mp3',
'assets/audio/dumbbell.mp3',
'assets/audio/sandwich.mp3',
'assets/audio/skateboard.mp3',
'assets/audio/success.mp3',
'assets/audio/tile_move.mp3',
];
late final PlatformHelper _platformHelper;
late final Timer _timer;
@override
void initState() {
super.initState();
_platformHelper = widget._platformHelperFactory();
_timer = Timer(const Duration(milliseconds: 20), () {
for (var i = 1; i <= 15; i++) {
precacheImage(
Image.asset('assets/images/dashatar/green/$i.png').image,
context,
);
precacheImage(
Image.asset('assets/images/dashatar/blue/$i.png').image,
context,
);
precacheImage(
Image.asset('assets/images/dashatar/yellow/$i.png').image,
context,
);
}
precacheImage(
Image.asset('assets/images/dashatar/gallery/green.png').image,
context,
);
precacheImage(
Image.asset('assets/images/dashatar/success/green.png').image,
context,
);
precacheImage(
Image.asset('assets/images/dashatar/gallery/blue.png').image,
context,
);
precacheImage(
Image.asset('assets/images/dashatar/success/blue.png').image,
context,
);
precacheImage(
Image.asset('assets/images/dashatar/gallery/yellow.png').image,
context,
);
precacheImage(
Image.asset('assets/images/dashatar/success/yellow.png').image,
context,
);
precacheImage(
Image.asset('assets/images/logo_flutter_color.png').image,
context,
);
precacheImage(
Image.asset('assets/images/logo_flutter_white.png').image,
context,
);
precacheImage(
Image.asset('assets/images/shuffle_icon.png').image,
context,
);
precacheImage(
Image.asset('assets/images/timer_icon.png').image,
context,
);
precacheImage(
Image.asset('assets/images/simple_dash_large.png').image,
context,
);
precacheImage(
Image.asset('assets/images/simple_dash_medium.png').image,
context,
);
precacheImage(
Image.asset('assets/images/simple_dash_small.png').image,
context,
);
precacheImage(
Image.asset('assets/images/twitter_icon.png').image,
context,
);
precacheImage(
Image.asset('assets/images/facebook_icon.png').image,
context,
);
for (final audioControlAsset in audioControlAssets) {
precacheImage(
Image.asset(audioControlAsset).image,
context,
);
}
for (final audioAsset in audioAssets) {
prefetchToMemory(audioAsset);
}
});
}
/// Prefetches the given [filePath] to memory.
Future<void> prefetchToMemory(String filePath) async {
if (_platformHelper.isWeb) {
// We rely on browser caching here. Once the browser downloads the file,
// the native implementation should be able to access it from cache.
await http.get(Uri.parse('$localAssetsPrefix$filePath'));
return;
}
throw UnimplementedError(
'The function `prefetchToMemory` is not implemented '
'for platforms other than Web.',
);
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
appBarTheme: const AppBarTheme(color: Color(0xFF13B9FF)),
colorScheme: ColorScheme.fromSwatch(
accentColor: const Color(0xFF13B9FF),
),
),
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
],
supportedLocales: AppLocalizations.supportedLocales,
home: const PuzzlePage(),
);
}
}