In some cases, it can be handy to cache images as they’re downloaded from theweb so they can be used offline. For this purpose, you’ll employ thecached_network_image
package.
In addition to caching, the cached_image_network package also supportsplaceholders and fading images in as they’re loaded.
CachedNetworkImage(
imageUrl: 'https://picsum.photos/250?image=9',
);
Adding a placeholder
The cached_network_image
package allows you to use any Widget as aplaceholder. In this example, you’ll display a spinner while the image loads.
CachedNetworkImage(
placeholder: (context, url) => CircularProgressIndicator(),
imageUrl: 'https://picsum.photos/250?image=9',
);
Complete example
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final title = 'Cached Images';
return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: CachedNetworkImage(
placeholder: (context, url) => CircularProgressIndicator(),
imageUrl:
'https://picsum.photos/250?image=9',
),
),
),
);
}
}
当前内容版权归 flutter.dev 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 flutter.dev .