image static method

Widget image(
  1. String? imageName, {
  2. double? width,
  3. double? height,
  4. double? size,
  5. BoxFit fit = BoxFit.contain,
  6. bool excludeFromSemantics = false,
  7. bool gaplessPlayback = false,
  8. Color? imageColor,
  9. bool primary = false,
  10. BuildContext? context,
})

传入图片名字获取图片Widget,注意不需要带路径

Implementation

static Widget image(String? imageName,
    {double? width,
    double? height,
    double? size,
    BoxFit fit = BoxFit.contain,
    bool excludeFromSemantics = false,
    bool gaplessPlayback = false,
    Color? imageColor,
    bool primary = false,
    BuildContext? context}) {
  if (imageName == null || imageName.isEmpty) {
    return SizedBox(width: width, height: height,);
  }
  try {
    var image = Image.asset(imagePath(imageName), width: width ?? size,
      height: height ?? size, fit: fit, excludeFromSemantics: excludeFromSemantics,
      gaplessPlayback: gaplessPlayback, errorBuilder: (context, object, trace) {
        return SizedBox(width: width, height: height,);
      },
    );
    return imageColor != null || (primary && context != null)
        ? ColorFiltered(colorFilter: ColorFilter.mode(
                primary ? Theme.of(context!).colorScheme.primary : imageColor!, BlendMode.srcIn), child: image,
          ) : image;
  } catch (e) {
    return SizedBox(width: width, height: height,);
  }
}