Skip to content

Commit 2595449

Browse files
committed
feat: add image precaching
1 parent 106028e commit 2595449

File tree

3 files changed

+47
-36
lines changed

3 files changed

+47
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- The checkmark indicator example has been simplified.
77
- Minor corrections to the envelope indicator.
88
- Removed unused code.
9+
- Added image precaching.
910
## 2.2.1
1011
- Fixed typos in documentation
1112
## 2.2.0

example/lib/indicators/ice_cream_indicator.dart

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
import 'dart:async';
2+
13
import 'package:custom_refresh_indicator/custom_refresh_indicator.dart';
24
import 'package:flutter/material.dart';
35

46
class ParallaxConfig {
5-
final int? level;
6-
final AssetImage? image;
7+
final int level;
8+
final AssetImage image;
79

810
const ParallaxConfig({
9-
this.level,
10-
this.image,
11+
required this.level,
12+
required this.image,
1113
});
1214
}
1315

@@ -23,8 +25,7 @@ class IceCreamIndicator extends StatefulWidget {
2325
State<IceCreamIndicator> createState() => _IceCreamIndicatorState();
2426
}
2527

26-
class _IceCreamIndicatorState extends State<IceCreamIndicator>
27-
with SingleTickerProviderStateMixin {
28+
class _IceCreamIndicatorState extends State<IceCreamIndicator> with SingleTickerProviderStateMixin {
2829
static const _assets = <ParallaxConfig>[
2930
ParallaxConfig(
3031
image: AssetImage("assets/ice_cream_indicator/cup2.png"),
@@ -64,22 +65,28 @@ class _IceCreamIndicatorState extends State<IceCreamIndicator>
6465

6566
@override
6667
void initState() {
67-
_spoonController =
68-
AnimationController(vsync: this, duration: const Duration(seconds: 1));
68+
_spoonController = AnimationController(vsync: this, duration: const Duration(seconds: 1));
69+
WidgetsBinding.instance.addPostFrameCallback((_) => _precacheImages());
6970
super.initState();
7071
}
7172

73+
void _precacheImages() {
74+
for (final config in _assets) {
75+
unawaited(precacheImage(config.image, context));
76+
}
77+
}
78+
7279
Widget _buildImage(IndicatorController controller, ParallaxConfig asset) {
7380
return Transform.translate(
7481
offset: Offset(
7582
0,
76-
-(asset.level! * (controller.value.clamp(1.0, 1.5) - 1.0) * 20) + 10,
83+
-(asset.level * (controller.value.clamp(1.0, 1.5) - 1.0) * 20) + 10,
7784
),
7885
child: OverflowBox(
7986
maxHeight: _imageSize,
8087
minHeight: _imageSize,
8188
child: Image(
82-
image: asset.image!,
89+
image: asset.image,
8390
fit: BoxFit.contain,
8491
height: _imageSize,
8592
),
@@ -126,9 +133,7 @@ class _IceCreamIndicatorState extends State<IceCreamIndicator>
126133
child: _buildImage(controller, _assets[i]),
127134
builder: (context, child) {
128135
return Transform.rotate(
129-
angle: (-_spoonTween
130-
.transform(_spoonController.value)) *
131-
1.25,
136+
angle: (-_spoonTween.transform(_spoonController.value)) * 1.25,
132137
child: child,
133138
);
134139
},

example/lib/indicators/plane_indicator.dart

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:custom_refresh_indicator/custom_refresh_indicator.dart';
24
import 'package:flutter/material.dart';
35

@@ -14,19 +16,20 @@ class _Cloud {
1416
];
1517

1618
AnimationController? controller;
17-
final Color? color;
18-
final AssetImage? image;
19-
final double? width;
20-
final double? dy;
21-
final double? initialValue;
22-
final Duration? duration;
19+
final Color color;
20+
final AssetImage image;
21+
final double width;
22+
final double dy;
23+
final double initialValue;
24+
final Duration duration;
25+
2326
_Cloud({
24-
this.color,
25-
this.image,
26-
this.width,
27-
this.dy,
28-
this.initialValue,
29-
this.duration,
27+
required this.color,
28+
required this.image,
29+
required this.width,
30+
required this.dy,
31+
required this.initialValue,
32+
required this.duration,
3033
});
3134
}
3235

@@ -41,8 +44,7 @@ class PlaneIndicator extends StatefulWidget {
4144
State<PlaneIndicator> createState() => _PlaneIndicatorState();
4245
}
4346

44-
class _PlaneIndicatorState extends State<PlaneIndicator>
45-
with TickerProviderStateMixin {
47+
class _PlaneIndicatorState extends State<PlaneIndicator> with TickerProviderStateMixin {
4648
static final _planeTween = CurveTween(curve: Curves.easeInOut);
4749
late AnimationController _planeController;
4850

@@ -54,9 +56,16 @@ class _PlaneIndicatorState extends State<PlaneIndicator>
5456
);
5557

5658
_setupCloudsAnimationControllers();
59+
WidgetsBinding.instance.addPostFrameCallback((_) => _precacheImages());
5760
super.initState();
5861
}
5962

63+
void _precacheImages() {
64+
for (final config in _clouds) {
65+
unawaited(precacheImage(config.image, context));
66+
}
67+
}
68+
6069
static final _clouds = [
6170
_Cloud(
6271
color: _Cloud._dark,
@@ -162,8 +171,7 @@ class _PlaneIndicatorState extends State<PlaneIndicator>
162171
),
163172
builder: (BuildContext context, Widget? child) {
164173
return Transform.translate(
165-
offset: Offset(0.0,
166-
10 * (0.5 - _planeTween.transform(_planeController.value))),
174+
offset: Offset(0.0, 10 * (0.5 - _planeTween.transform(_planeController.value))),
167175
child: child,
168176
);
169177
},
@@ -191,8 +199,7 @@ class _PlaneIndicatorState extends State<PlaneIndicator>
191199
}
192200
},
193201
onRefresh: () => Future.delayed(const Duration(seconds: 3)),
194-
builder: (BuildContext context, Widget child,
195-
IndicatorController controller) {
202+
builder: (BuildContext context, Widget child, IndicatorController controller) {
196203
return AnimatedBuilder(
197204
animation: controller,
198205
child: child,
@@ -214,10 +221,8 @@ class _PlaneIndicatorState extends State<PlaneIndicator>
214221
for (final cloud in _clouds)
215222
Transform.translate(
216223
offset: Offset(
217-
((screenWidth + cloud.width!) *
218-
cloud.controller!.value) -
219-
cloud.width!,
220-
cloud.dy! * controller.value,
224+
((screenWidth + cloud.width) * cloud.controller!.value) - cloud.width,
225+
cloud.dy * controller.value,
221226
),
222227
child: OverflowBox(
223228
minWidth: cloud.width,
@@ -227,7 +232,7 @@ class _PlaneIndicatorState extends State<PlaneIndicator>
227232
alignment: Alignment.topLeft,
228233
child: Image(
229234
color: cloud.color,
230-
image: cloud.image!,
235+
image: cloud.image,
231236
fit: BoxFit.contain,
232237
),
233238
),

0 commit comments

Comments
 (0)