paint method
Draw the image onto the given canvas.
The image is drawn at the position and size given by the rect
argument.
The image is clipped to the given clipPath
, if any.
The configuration
object is used to resolve the image (e.g. to pick
resolution-specific assets), and to implement the
DecorationImage.matchTextDirection feature.
If the image needs to be painted again, e.g. because it is animated or
because it had not yet been loaded the first time this method was called,
then the onChanged
callback passed to DecorationImage.createPainter
will be called.
Implementation
void paint(Canvas canvas, Rect rect, Path clipPath, ImageConfiguration configuration) {
assert(canvas != null);
assert(rect != null);
assert(configuration != null);
bool flipHorizontally = false;
if (_details.matchTextDirection) {
assert(() {
// We check this first so that the assert will fire immediately, not just
// when the image is ready.
if (configuration.textDirection == null) {
throw FlutterError(
'ImageDecoration.matchTextDirection can only be used when a TextDirection is available.\n'
'When DecorationImagePainter.paint() was called, there was no text direction provided '
'in the ImageConfiguration object to match.\n'
'The DecorationImage was:\n'
' $_details\n'
'The ImageConfiguration was:\n'
' $configuration'
);
}
return true;
}());
if (configuration.textDirection == TextDirection.rtl)
flipHorizontally = true;
}
final ImageStream newImageStream = _details.image.resolve(configuration);
if (newImageStream.key != _imageStream?.key) {
_imageStream?.removeListener(_imageListener);
_imageStream = newImageStream;
_imageStream.addListener(_imageListener);
}
if (_image == null)
return;
if (clipPath != null) {
canvas.save();
canvas.clipPath(clipPath);
}
paintImage(
canvas: canvas,
rect: rect,
image: _image.image,
scale: _image.scale,
colorFilter: _details.colorFilter,
fit: _details.fit,
alignment: _details.alignment.resolve(configuration.textDirection),
centerSlice: _details.centerSlice,
repeat: _details.repeat,
flipHorizontally: flipHorizontally,
filterQuality: FilterQuality.low
);
if (clipPath != null)
canvas.restore();
}