在我的应用程序中,我使用这两个类,但我不知道应该优先考虑哪一个.
Image.asset('icons/heart.png')
AssetImage('icons/hear.png')
Run Code Online (Sandbox Code Playgroud)
也许有人能更快地获取图像.
die*_*per 28
Image
是一个StatefulWidget
并且Image.asset
只是一个命名构造函数,您可以直接在小部件树上使用它.
AssetImage
是ImageProvider
负责获取指定路径的图像的.
如果您检查了源代码,Image.asset
您会发现它使用AssetImage来获取图像.
Image.asset(String name, {
Key key,
AssetBundle bundle,
this.semanticLabel,
this.excludeFromSemantics = false,
double scale,
this.width,
this.height,
this.color,
this.colorBlendMode,
this.fit,
this.alignment = Alignment.center,
this.repeat = ImageRepeat.noRepeat,
this.centerSlice,
this.matchTextDirection = false,
this.gaplessPlayback = false,
String package,
this.filterQuality = FilterQuality.low,
}) : image = scale != null
? ExactAssetImage(name, bundle: bundle, scale: scale, package: package)
: AssetImage(name, bundle: bundle, package: package),
assert(alignment != null),
assert(repeat != null),
assert(matchTextDirection != null),
super(key: key);
Run Code Online (Sandbox Code Playgroud)
感谢@diegovoper From flutter version 2.5
,建议将Image StatefulWidget
与const修饰符一起使用,这是不可能的Image.asset
。但是,您需要将 提供为该对象image path
的参数,其中该对象是的命名参数“image”的AssetImage
值,如下所示。Image StatefulWidget
Image(
image: AssetImage('asset/dice1.png'),
)
Run Code Online (Sandbox Code Playgroud)
来自推荐的Dart 教程书Dart Apprentice const and final
对对象的修饰符减少了后续的编译时间和运行时间。
因此,对于干净且少行的代码,请使用Image.asset
,但对于快速且 CPU 友好的代码,请使用Image StatefulWidget
。
归档时间: |
|
查看次数: |
3739 次 |
最近记录: |