fromImageProvider static method
- ImageProvider<
Object> imageProvider, { - Size? size,
- Rect? region,
- int maximumColorCount = _defaultCalculateNumberColors,
- List<
PaletteFilter> filters = const <PaletteFilter>[avoidRedBlackWhitePaletteFilter], - List<
PaletteTarget> targets = const <PaletteTarget>[], - 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.
Implementation
static Future<PaletteGenerator> fromImageProvider(
ImageProvider imageProvider, {
Size? size,
Rect? region,
int maximumColorCount = _defaultCalculateNumberColors,
List<PaletteFilter> filters = const <PaletteFilter>[
avoidRedBlackWhitePaletteFilter
],
List<PaletteTarget> targets = const <PaletteTarget>[],
Duration timeout = const Duration(seconds: 15),
}) async {
assert(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;
late 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);
final ui.Image image = await imageCompleter.future;
ui.Rect? newRegion = region;
if (size != null && region != null) {
final double scale = image.width / size.width;
newRegion = Rect.fromLTRB(
region.left * scale,
region.top * scale,
region.right * scale,
region.bottom * scale,
);
}
return PaletteGenerator.fromImage(
image,
region: newRegion,
maximumColorCount: maximumColorCount,
filters: filters,
targets: targets,
);
}