I have a NetworkImage and I'd like to know when it's finished loading. How do I do that?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can resolve
to get an ImageStream
and addListener
to the ImageStream
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
State createState() => new MyAppState();
}
class MyAppState extends State<MyApp> {
Image _image = new Image.network(
'https://flutter.io/images/flutter-mark-square-100.png',
);
bool _loading = true;
@override
void initState() {
_image.image.resolve(new ImageConfiguration()).addListener((_, __) {
if (mounted) {
setState(() {
_loading = false;
});
}
});
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
body: new Center(
child: _loading ? new Text('Loading...') : _image,
),
),
);
}
}
回答2:
I find this method in flutter official demo,wish help you.
import 'dart:async';
import 'package:flutter/material.dart';
void _imageLoad() async {
String imageName = "";
Image downloadImage = new Image.network(imageName);
final ImageStream stream = downloadImage.image.resolve(ImageConfiguration.empty);
final Completer<void> completer = Completer<void>();
stream.addListener((ImageInfo info, bool syncCall) => completer.complete());
await completer.future;
if (mounted) {
//do sth
}
}