NetworkImage和Image.network的区别? [英] Difference in NetworkImage and Image.network?
问题描述
两者是否有区别?
缺点是什么?在正常情况下,哪个更容易使用?
Whats the disadvantage and which one is easier to use for normal situation?
推荐答案
两者是否有区别?
Are there any difference in both of them?
是。它们是不同的。
- NetworkImage 类创建一个对象,该对象通过传递给它的
src
URL提供图像。它不是小部件,也不会在屏幕上输出图像。 -
Image.network 创建一个在屏幕上显示图像的小部件。它只是 Image 类(有状态的小部件)上的命名构造函数。 。它使用
NetworkImage
设置image
属性。该image
属性最终用于显示图像。
- NetworkImage class creates an object the provides an image from the
src
URL passed to it. It is not a widget and does not output an image to the screen. Image.network creates a widget that displays an image on the screen. It is just a named constructor on the Image class(a stateful widget). It sets the
image
property using theNetworkImage
. Thisimage
property is used finally to display the image.
class Image extends StatefulWidget{
Image(...){}; //default Constructor
//the argument src is passed to the NetworkImage and assinged to the image property
Image.network(String src, {...}) : image = NetworkImage(src, ...);
final ImageProvider image;
@override
Widget build(BuildContext context){
display the image
return RawImage(image: image,
...
);
}
}
缺点是什么,在正常情况下哪个更容易使用?
Whats the disadvantage and which one is easier to use for normal situation?
没有缺点。您应该使用适合需要的那一种。例如,考虑:
There is no disadvantage. You should use the one the suits the need. For example consider:
- CircleAvatar 小部件,该小部件显示一个表示用户的圆圈,并使用
backgroundImage
。它需要一个ImageProvider。因此,您传递了NetworkImage(http://image.com)
- FadeInImage 在加载原始图像时显示占位符,也需要
ImageProvider
其图片
属性。因此,您可以提供它NetworkImage(http://image.com)
。
- CircleAvatar widget that displays acircle that represents a user takes
backgroundImage
. It requires an ImageProvider. So you passNetworkImage(http://image.com)
- FadeInImage that displays a placeholder while the original image is loading also takes a
ImageProvider
for itsimage
property. So you can provide itNetworkImage(http://image.com)
.
如果您只想在屏幕上将图像显示为小部件,请使用 Image.network
并在任何位置使用 NetworkImage
预计 ImageProvider
。
If you just want to display the image as a widget on screen use Image.network
and use NetworkImage
wherever an ImageProvider
is expected.
这篇关于NetworkImage和Image.network的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!