ImageProvider< T> class
Identifies an image without committing to the precise final asset. This allows a set of images to be identified and for the precise image to later be resolved based on the environment, e.g. the device pixel ratio.
To obtain an ImageStream from an ImageProvider, call resolve, passing it an ImageConfiguration object.
ImageProvider uses the global imageCache to cache images.
The type argument T
is the type of the object used to represent a resolved
configuration. This is also the type used for the key in the image cache. It
should be immutable and implement the ==
operator and the hashCode
getter. Subclasses should subclass a variant of ImageProvider with an
explicit T
type argument.
The type argument does not have to be specified when using the type as an argument (where any image provider is acceptable).
The following image formats are supported: JPEG, PNG, GIF, Animated GIF, WebP, Animated WebP, BMP, and WBMP
class MyImage extends StatefulWidget {
const MyImage({
Key key,
@required this.imageProvider,
}) : assert(imageProvider != null),
super(key: key);
final ImageProvider imageProvider;
@override
_MyImageState createState() => _MyImageState();
}
class _MyImageState extends State<MyImage> {
ImageStream _imageStream;
ImageInfo _imageInfo;
@override
void didChangeDependencies() {
super.didChangeDependencies();
// We call _getImage here because createLocalImageConfiguration() needs to
// be called again if the dependencies changed, in case the changes relate
// to the DefaultAssetBundle, MediaQuery, etc, which that method uses.
_getImage();
}
@override
void didUpdateWidget(MyImage oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.imageProvider != oldWidget.imageProvider)
_getImage();
}
void _getImage() {
final ImageStream oldImageStream = _imageStream;
_imageStream = widget.imageProvider.resolve(createLocalImageConfiguration(context));
if (_imageStream.key != oldImageStream?.key) {
// If the keys are the same, then we got the same image back, and so we don't
// need to update the listeners. If the key changed, though, we must make sure
// to switch our listeners to the new image stream.
oldImageStream?.removeListener(_updateImage);
_imageStream.addListener(_updateImage);
}
}
void _updateImage(ImageInfo imageInfo, bool synchronousCall) {
setState(() {
// Trigger a build whenever the image changes.
_imageInfo = imageInfo;
});
}
@override
void dispose() {
_imageStream.removeListener(_updateImage);
super.dispose();
}
@override
Widget build(BuildContext context) {
return RawImage(
image: _imageInfo?.image, // this is a dart:ui Image object
scale: _imageInfo?.scale ?? 1.0,
);
}
}
- Implementers
- Annotations
- @optionalTypeArgs
Constructors
- ImageProvider()
-
Abstract const constructor. This constructor enables subclasses to provide
const constructors so that they can be used in const expressions.
const
Properties
- hashCode → int
-
The hash code for this object. [...]
read-only, inherited
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
Methods
-
evict(
{ImageCache cache, ImageConfiguration configuration: ImageConfiguration.empty }) → Future< bool> - Evicts an entry from the image cache. [...]
-
load(
T key) → ImageStreamCompleter -
Converts a key into an ImageStreamCompleter, and begins fetching the
image.
@protected
-
obtainKey(
ImageConfiguration configuration) → Future< T> -
Converts an ImageProvider's settings plus an ImageConfiguration to a key
that describes the precise image to load. [...]
@protected
-
resolve(
ImageConfiguration configuration) → ImageStream -
Resolves this image provider using the given
configuration
, returning an ImageStream. [...] -
toString(
) → String -
Returns a string representation of this object.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed. [...]
inherited
Operators
-
operator ==(
dynamic other) → bool -
The equality operator. [...]
inherited