fromImageProvider method

Future<PaletteGenerator> fromImageProvider (ImageProvider imageProvider, { Size size, Rect region, int maximumColorCount, List<PaletteFilter> filters, List<PaletteTarget> targets, Duration timeout: const Duration(seconds: 15) })

Create a PaletteGenerator from an ImageProvider, like FileImage, or AssetImage, asynchronously.

The size is the desired size of the image. The image will be resized to this size before creating the PaletteGenerator from it.

The region specifies the part of the (resized) image to inspect for color candidates. By default it uses the entire image. Must not be equal to Rect.zero, and must not be larger than the image dimensions.

The maximumColorCount sets the maximum number of colors that will be returned in the PaletteGenerator. The default is 16 colors.

The filters specify a lost of PaletteFilter instances that can be used to include certain colors in the list of colors. The default filter is an instance of AvoidRedBlackWhitePaletteFilter, which stays away from whites, blacks, and low-saturation reds.

The targets are a list of target color types, specified by creating custom PaletteTargets. By default, this is the list of targets in PaletteTarget.baseTargets.

The timeout describes how long to wait for the image to load before giving up on it. A value of Duration.zero implies waiting forever. The default timeout is 15 seconds.

The imageProvider and timeout arguments must not be null.

Implementation

static Future<PaletteGenerator> fromImageProvider(
  ImageProvider imageProvider, {
  Size size,
  Rect region,
  int maximumColorCount,
  List<PaletteFilter> filters,
  List<PaletteTarget> targets,
  Duration timeout = const Duration(seconds: 15),
}) async {
  assert(imageProvider != null);
  assert(timeout != null);
  assert(region == null || (region != null && size != null));
  assert(region == null || region != Rect.zero);
  assert(
      region == null ||
          (region.topLeft.dx >= 0.0 && region.topLeft.dy >= 0.0),
      'Region $region is outside the image ${size.width}x${size.height}');
  assert(region == null || size.contains(region.topLeft),
      'Region $region is outside the image $size');
  assert(
      region == null ||
          (region.bottomRight.dx <= size.width &&
              region.bottomRight.dy <= size.height),
      'Region $region is outside the image $size');
  final ImageStream stream = imageProvider.resolve(
    ImageConfiguration(size: size, devicePixelRatio: 1.0),
  );
  final Completer<ui.Image> imageCompleter = Completer<ui.Image>();
  Timer loadFailureTimeout;
  ImageStreamListener listener;
  listener = ImageStreamListener((ImageInfo info, bool synchronousCall) {
    loadFailureTimeout?.cancel();
    stream.removeListener(listener);
    imageCompleter.complete(info.image);
  });

  if (timeout != Duration.zero) {
    loadFailureTimeout = Timer(timeout, () {
      stream.removeListener(listener);
      imageCompleter.completeError(
        TimeoutException(
            'Timeout occurred trying to load from $imageProvider'),
      );
    });
  }
  stream.addListener(listener);
  return PaletteGenerator.fromImage(
    await imageCompleter.future,
    region: region,
    maximumColorCount: maximumColorCount,
    filters: filters,
    targets: targets,
  );
}