loadImageFromFile method

Widget loadImageFromFile(
  1. File file,
  2. BoxFit fit,
  3. double width,
  4. double height,
)

Loads an image from the specified file and returns a widget displaying the image.

This method uses a FutureBuilder to handle the asynchronous image decoding process and displays a loading indicator while the image is being decoded. If an error occurs during decoding, an error placeholder widget is returned.

Parameters:

  • file: A File object representing the image file to load.
  • fit: A BoxFit value that defines how the image should be fitted into the given width and height.
  • width: A double specifying the desired width of the image.
  • height: A double specifying the desired height of the image.

Returns: A Widget that displays the image or a loading indicator/error placeholder if applicable.

Implementation

Widget loadImageFromFile(
  File file,
  BoxFit fit,
  double width,
  double height,
) {
  return FutureBuilder<ui.Image>(
    future: decodeImage(file),
    builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.done &&
          snapshot.hasData) {
        return RawImage(
          image: snapshot.data!,
          fit: fit,
          width: width,
          height: height,
        );
      } else if (snapshot.hasError) {
        return PlaceholderManager.getErrorPlaceholder();
      } else {
        return PlaceholderManager.getLoadingPlaceholder();
      }
    },
  );
}