SpriteButton.asset constructor

SpriteButton.asset({
  1. required String path,
  2. required String pressedPath,
  3. required void onPressed()?,
  4. required double width,
  5. required double height,
  6. Widget? label,
  7. Images? images,
  8. Vector2? srcPosition,
  9. Vector2? srcSize,
  10. Vector2? pressedSrcPosition,
  11. Vector2? pressedSrcSize,
  12. String? disabledPath,
  13. Vector2? disabledSrcPosition,
  14. Vector2? disabledSrcSize,
  15. EdgeInsets pressedInsets = const EdgeInsets.only(top: 5),
  16. WidgetBuilder? errorBuilder,
  17. WidgetBuilder? loadingBuilder,
  18. Key? key,
})

Loads the images from the asset path and pressedPath and renders it as a widget.

It will use the loadingBuilder while the image from path is loading. To render without loading, or when you want to have a gapless playback when the path value changes, consider loading the image beforehand and direct pass it to the default constructor.

Implementation

SpriteButton.asset({
  required String path,
  required String pressedPath,
  required this.onPressed,
  required this.width,
  required this.height,
  this.label,
  Images? images,
  this.srcPosition,
  this.srcSize,
  this.pressedSrcPosition,
  this.pressedSrcSize,
  String? disabledPath,
  this.disabledSrcPosition,
  this.disabledSrcSize,
  this.pressedInsets = const EdgeInsets.only(top: 5),
  this.errorBuilder,
  this.loadingBuilder,
  super.key,
}) : _buttonsFuture = (images ?? Flame.images).containsKey(path) &&
              (images ?? Flame.images).containsKey(pressedPath) &&
              (disabledPath == null ||
                  (images ?? Flame.images).containsKey(disabledPath))
          ? [
              Sprite(
                (images ?? Flame.images).fromCache(path),
                srcPosition: srcPosition,
                srcSize: srcSize,
              ),
              Sprite(
                (images ?? Flame.images).fromCache(pressedPath),
                srcPosition: pressedSrcPosition,
                srcSize: pressedSrcSize,
              ),
              if (disabledPath != null)
                Sprite(
                  (images ?? Flame.images).fromCache(disabledPath),
                  srcPosition: disabledSrcPosition,
                  srcSize: disabledSrcSize,
                ),
            ]
          : Future.wait([
              Sprite.load(
                path,
                srcPosition: srcPosition,
                srcSize: srcSize,
                images: images,
              ),
              Sprite.load(
                pressedPath,
                srcPosition: pressedSrcPosition,
                srcSize: pressedSrcSize,
                images: images,
              ),
              if (disabledPath != null)
                Sprite.load(
                  disabledPath,
                  srcPosition: disabledSrcPosition,
                  srcSize: disabledSrcSize,
                  images: images,
                ),
            ]);