AssetImage和Image.asset-Flutter

时间:2018-11-14 22:22:53

标签: dart flutter

在我的应用程序中,我使用了这两个类,但是我不知道我应该优先哪个类。

Image.asset('icons/heart.png')
AssetImage('icons/hear.png')

也许有人能更快地获取图像。

1 个答案:

答案 0 :(得分:14)

ImageStatefulWidget,而Image.asset只是一个命名构造函数,您可以直接在小部件树上使用它。

AssetImageImageProvider,负责获取指定路径的图像。

如果检查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);