-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathblurhash_widget.dart
246 lines (202 loc) · 6.16 KB
/
blurhash_widget.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
import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blurhash/flutter_blurhash.dart';
const _DEFAULT_SIZE = 32;
/// Display a Hash then fade to Image
class BlurHash extends StatefulWidget {
const BlurHash({
required this.hash,
Key? key,
this.color = Colors.blueGrey,
this.imageFit = BoxFit.fill,
this.decodingWidth = _DEFAULT_SIZE,
this.decodingHeight = _DEFAULT_SIZE,
this.image,
this.onDecoded,
this.onDisplayed,
this.onReady,
this.onStarted,
this.duration = const Duration(milliseconds: 1000),
this.httpHeaders = const {},
this.curve = Curves.easeOut,
this.errorBuilder,
}) : assert(decodingWidth > 0),
assert(decodingHeight != 0),
super(key: key);
/// Callback when hash is decoded
final VoidCallback? onDecoded;
/// Callback when hash is decoded
final VoidCallback? onDisplayed;
/// Callback when image is downloaded
final VoidCallback? onReady;
/// Callback when image is downloaded
final VoidCallback? onStarted;
/// Hash to decode
final String hash;
/// Displayed background color before decoding
final Color color;
/// How to fit decoded & downloaded image
final BoxFit imageFit;
/// Decoding definition
final int decodingWidth;
/// Decoding definition
final int decodingHeight;
/// Remote resource to download
final String? image;
final Duration duration;
final Curve curve;
/// Http headers for secure call like bearer
final Map<String, String> httpHeaders;
/// Network image errorBuilder
final ImageErrorWidgetBuilder? errorBuilder;
@override
BlurHashState createState() => BlurHashState();
}
class BlurHashState extends State<BlurHash> {
late Future<ui.Image> _image;
late bool loaded;
late bool loading;
@override
void initState() {
super.initState();
_init();
}
void _init() {
_decodeImage();
loaded = false;
loading = false;
}
@override
void didUpdateWidget(BlurHash oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.hash != oldWidget.hash ||
widget.image != oldWidget.image ||
widget.decodingWidth != oldWidget.decodingWidth ||
widget.decodingHeight != oldWidget.decodingHeight) {
_init();
}
}
void _decodeImage() {
_image = blurHashDecodeImage(
blurHash: widget.hash,
width: widget.decodingWidth,
height: widget.decodingHeight,
);
_image.whenComplete(() => widget.onDecoded?.call());
}
@override
Widget build(BuildContext context) => Stack(
fit: StackFit.expand,
alignment: Alignment.center,
children: [
buildBlurHashBackground(),
if (widget.image != null) prepareDisplayedImage(widget.image!),
],
);
Widget prepareDisplayedImage(String image) => Image.network(
image,
fit: widget.imageFit,
headers: widget.httpHeaders,
errorBuilder: widget.errorBuilder,
loadingBuilder: (context, img, loadingProgress) {
// Download started
if (loading == false) {
loading = true;
widget.onStarted?.call();
}
if (loadingProgress == null) {
// Image is now loaded, trigger the event
loaded = true;
widget.onReady?.call();
return _DisplayImage(
child: img,
duration: widget.duration,
curve: widget.curve,
onCompleted: () => widget.onDisplayed?.call(),
);
} else {
return const SizedBox();
}
},
);
/// Decode the blurhash then display the resulting Image
Widget buildBlurHashBackground() => FutureBuilder<ui.Image>(
future: _image,
builder: (ctx, snap) => snap.hasData
? Image(
image: UiImage(snap.data!),
fit: widget.imageFit,
errorBuilder: widget.errorBuilder,
)
: Container(color: widget.color),
);
}
// Inner display details & controls
class _DisplayImage extends StatefulWidget {
final Widget child;
final Duration duration;
final Curve curve;
final VoidCallback onCompleted;
const _DisplayImage({
required this.child,
this.duration = const Duration(milliseconds: 800),
required this.curve,
required this.onCompleted,
Key? key,
}) : super(key: key);
@override
_DisplayImageState createState() => _DisplayImageState();
}
class _DisplayImageState extends State<_DisplayImage>
with SingleTickerProviderStateMixin {
late Animation<double> opacity;
late AnimationController controller;
@override
Widget build(BuildContext context) => FadeTransition(
opacity: opacity,
child: widget.child,
);
@override
void initState() {
super.initState();
controller = AnimationController(duration: widget.duration, vsync: this);
final curved = CurvedAnimation(parent: controller, curve: widget.curve);
opacity = Tween<double>(begin: .0, end: 1.0).animate(curved);
controller.forward();
curved.addStatusListener((status) {
if (status == AnimationStatus.completed) widget.onCompleted.call();
});
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
}
class UiImage extends ImageProvider<UiImage> {
final ui.Image image;
final double scale;
const UiImage(this.image, {this.scale = 1.0});
@override
Future<UiImage> obtainKey(ImageConfiguration configuration) =>
SynchronousFuture<UiImage>(this);
@override
ImageStreamCompleter load(UiImage key, DecoderCallback decode) =>
OneFrameImageStreamCompleter(_loadAsync(key));
Future<ImageInfo> _loadAsync(UiImage key) async {
assert(key == this);
return ImageInfo(image: image, scale: key.scale);
}
@override
bool operator ==(dynamic other) {
if (other.runtimeType != runtimeType) return false;
final UiImage typedOther = other;
return image == typedOther.image && scale == typedOther.scale;
}
@override
int get hashCode => hashValues(image.hashCode, scale);
@override
String toString() =>
'$runtimeType(${describeIdentity(image)}, scale: $scale)';
}