image.dart 23.8 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:async';
6
import 'dart:io' show File;
7
import 'dart:typed_data';
8

9
import 'package:flutter/foundation.dart';
10
import 'package:flutter/painting.dart';
11
import 'package:flutter/services.dart';
12
import 'package:flutter/semantics.dart';
13 14 15

import 'basic.dart';
import 'framework.dart';
Ian Hickson's avatar
Ian Hickson committed
16
import 'localizations.dart';
17
import 'media_query.dart';
18
import 'ticker_provider.dart';
19

20
export 'package:flutter/painting.dart' show
21 22
  AssetImage,
  ExactAssetImage,
23 24 25 26 27
  FileImage,
  ImageConfiguration,
  ImageInfo,
  ImageStream,
  ImageProvider,
28
  MemoryImage,
29
  NetworkImage;
30

31 32 33 34 35
/// Creates an [ImageConfiguration] based on the given [BuildContext] (and
/// optionally size).
///
/// This is the object that must be passed to [BoxPainter.paint] and to
/// [ImageProvider.resolve].
36 37 38
///
/// If this is not called from a build method, then it should be reinvoked
/// whenever the dependencies change, e.g. by calling it from
39
/// [State.didChangeDependencies], so that any changes in the environment are
40 41 42 43 44
/// picked up (e.g. if the device pixel ratio changes).
///
/// See also:
///
///  * [ImageProvider], which has an example showing how this might be used.
45
ImageConfiguration createLocalImageConfiguration(BuildContext context, { Size size }) {
46
  return ImageConfiguration(
47
    bundle: DefaultAssetBundle.of(context),
48
    devicePixelRatio: MediaQuery.of(context, nullOk: true)?.devicePixelRatio ?? 1.0,
Ian Hickson's avatar
Ian Hickson committed
49 50
    locale: Localizations.localeOf(context, nullOk: true),
    textDirection: Directionality.of(context),
51
    size: size,
52
    platform: defaultTargetPlatform,
53 54 55
  );
}

56 57 58
/// Prefetches an image into the image cache.
///
/// Returns a [Future] that will complete when the first image yielded by the
59
/// [ImageProvider] is available or failed to load.
60 61
///
/// If the image is later used by an [Image] or [BoxDecoration] or [FadeInImage],
62 63
/// it will probably be loaded faster. The consumer of the image does not need
/// to use the same [ImageProvider] instance. The [ImageCache] will find the image
64 65 66 67 68
/// as long as both images share the same key.
///
/// The [BuildContext] and [Size] are used to select an image configuration
/// (see [createLocalImageConfiguration]).
///
69 70
/// The `onError` argument can be used to manually handle errors while precaching.
///
71 72 73
/// See also:
///
///   * [ImageCache], which holds images that may be reused.
74
Future<void> precacheImage(
75 76 77 78 79
  ImageProvider provider,
  BuildContext context, {
  Size size,
  ImageErrorListener onError,
}) {
80
  final ImageConfiguration config = createLocalImageConfiguration(context, size: size);
81
  final Completer<void> completer = Completer<void>();
82 83 84 85
  final ImageStream stream = provider.resolve(config);
  void listener(ImageInfo image, bool sync) {
    completer.complete();
  }
86 87 88 89 90
  void errorListener(dynamic exception, StackTrace stackTrace) {
    completer.complete();
    if (onError != null) {
      onError(exception, stackTrace);
    } else {
91
      FlutterError.reportError(FlutterErrorDetails(
92 93 94 95 96 97 98 99 100
        context: 'image failed to precache',
        library: 'image resource service',
        exception: exception,
        stack: stackTrace,
        silent: true,
      ));
    }
  }
  stream.addListener(listener, onError: errorListener);
101
  completer.future.then<void>((void value) { stream.removeListener(listener); });
102 103 104
  return completer.future;
}

105 106 107 108 109
/// A widget that displays an image.
///
/// Several constructors are provided for the various ways that an image can be
/// specified:
///
110 111 112 113 114 115
///  * [new Image], for obtaining an image from an [ImageProvider].
///  * [new Image.asset], for obtaining an image from an [AssetBundle]
///    using a key.
///  * [new Image.network], for obtaining an image from a URL.
///  * [new Image.file], for obtaining an image from a [File].
///  * [new Image.memory], for obtaining an image from a [Uint8List].
116
///
117 118
/// The following image formats are supported: {@macro flutter.dart:ui.imageFormats}
///
119 120 121 122 123 124
/// To automatically perform pixel-density-aware asset resolution, specify the
/// image using an [AssetImage] and make sure that a [MaterialApp], [WidgetsApp],
/// or [MediaQuery] widget exists above the [Image] widget in the widget tree.
///
/// The image is painted using [paintImage], which describes the meanings of the
/// various fields on this class in more detail.
125 126 127
///
/// See also:
///
Ian Hickson's avatar
Ian Hickson committed
128 129 130 131
///  * [Icon], which shows an image from a font.
///  * [new Ink.image], which is the preferred way to show an image in a
///    material application (especially if the image is in a [Material] and will
///    have an [InkWell] on top of it).
132 133 134 135
class Image extends StatefulWidget {
  /// Creates a widget that displays an image.
  ///
  /// To show an image from the network or from an asset bundle, consider using
136
  /// [new Image.network] and [new Image.asset] respectively.
137
  ///
Ian Hickson's avatar
Ian Hickson committed
138 139
  /// The [image], [alignment], [repeat], and [matchTextDirection] arguments
  /// must not be null.
Ian Hickson's avatar
Ian Hickson committed
140 141 142 143 144
  ///
  /// Either the [width] and [height] arguments should be specified, or the
  /// widget should be placed in a context that sets tight layout constraints.
  /// Otherwise, the image dimensions will change as the image is loaded, which
  /// will result in ugly layout changes.
145 146
  ///
  /// If [excludeFromSemantics] is true, then [semanticLabel] will be ignored.
147
  const Image({
148 149
    Key key,
    @required this.image,
150 151
    this.semanticLabel,
    this.excludeFromSemantics = false,
152 153 154
    this.width,
    this.height,
    this.color,
155
    this.colorBlendMode,
156
    this.fit,
157 158
    this.alignment = Alignment.center,
    this.repeat = ImageRepeat.noRepeat,
159
    this.centerSlice,
160 161
    this.matchTextDirection = false,
    this.gaplessPlayback = false,
162
  }) : assert(image != null),
Ian Hickson's avatar
Ian Hickson committed
163 164 165
       assert(alignment != null),
       assert(repeat != null),
       assert(matchTextDirection != null),
166
       super(key: key);
167 168 169 170

  /// Creates a widget that displays an [ImageStream] obtained from the network.
  ///
  /// The [src], [scale], and [repeat] arguments must not be null.
Ian Hickson's avatar
Ian Hickson committed
171 172 173 174 175
  ///
  /// Either the [width] and [height] arguments should be specified, or the
  /// widget should be placed in a context that sets tight layout constraints.
  /// Otherwise, the image dimensions will change as the image is loaded, which
  /// will result in ugly layout changes.
176 177
  ///
  /// All network images are cached regardless of HTTP headers.
Ian Hickson's avatar
Ian Hickson committed
178 179 180
  ///
  /// An optional [headers] argument can be used to send custom HTTP headers
  /// with the image request.
181 182
  ///
  /// If [excludeFromSemantics] is true, then [semanticLabel] will be ignored.
183
  Image.network(String src, {
184
    Key key,
185
    double scale = 1.0,
186 187
    this.semanticLabel,
    this.excludeFromSemantics = false,
188 189 190
    this.width,
    this.height,
    this.color,
191
    this.colorBlendMode,
192
    this.fit,
193 194
    this.alignment = Alignment.center,
    this.repeat = ImageRepeat.noRepeat,
195
    this.centerSlice,
196 197
    this.matchTextDirection = false,
    this.gaplessPlayback = false,
198
    Map<String, String> headers,
199
  }) : image = NetworkImage(src, scale: scale, headers: headers),
Ian Hickson's avatar
Ian Hickson committed
200 201 202
       assert(alignment != null),
       assert(repeat != null),
       assert(matchTextDirection != null),
203 204
       super(key: key);

Ian Hickson's avatar
Ian Hickson committed
205 206 207 208
  /// Creates a widget that displays an [ImageStream] obtained from a [File].
  ///
  /// The [file], [scale], and [repeat] arguments must not be null.
  ///
Ian Hickson's avatar
Ian Hickson committed
209 210 211 212 213
  /// Either the [width] and [height] arguments should be specified, or the
  /// widget should be placed in a context that sets tight layout constraints.
  /// Otherwise, the image dimensions will change as the image is loaded, which
  /// will result in ugly layout changes.
  ///
Ian Hickson's avatar
Ian Hickson committed
214 215
  /// On Android, this may require the
  /// `android.permission.READ_EXTERNAL_STORAGE` permission.
216 217
  ///
  /// If [excludeFromSemantics] is true, then [semanticLabel] will be ignored.
Ian Hickson's avatar
Ian Hickson committed
218 219
  Image.file(File file, {
    Key key,
220
    double scale = 1.0,
221 222
    this.semanticLabel,
    this.excludeFromSemantics = false,
Ian Hickson's avatar
Ian Hickson committed
223 224 225
    this.width,
    this.height,
    this.color,
226
    this.colorBlendMode,
Ian Hickson's avatar
Ian Hickson committed
227
    this.fit,
228 229
    this.alignment = Alignment.center,
    this.repeat = ImageRepeat.noRepeat,
Ian Hickson's avatar
Ian Hickson committed
230
    this.centerSlice,
231 232
    this.matchTextDirection = false,
    this.gaplessPlayback = false,
233
  }) : image = FileImage(file, scale: scale),
Ian Hickson's avatar
Ian Hickson committed
234 235 236
       assert(alignment != null),
       assert(repeat != null),
       assert(matchTextDirection != null),
Ian Hickson's avatar
Ian Hickson committed
237 238
       super(key: key);

239 240 241
  /// Creates a widget that displays an [ImageStream] obtained from an asset
  /// bundle. The key for the image is given by the `name` argument.
  ///
242 243 244 245
  /// The `package` argument must be non-null when displaying an image from a
  /// package and null otherwise. See the `Assets in packages` section for
  /// details.
  ///
246 247 248
  /// If the `bundle` argument is omitted or null, then the
  /// [DefaultAssetBundle] will be used.
  ///
249 250
  /// By default, the pixel-density-aware asset resolution will be attempted. In
  /// addition:
251
  ///
252
  /// * If the `scale` argument is provided and is not null, then the exact
253 254
  /// asset specified will be used. To display an image variant with a specific
  /// density, the exact path must be provided (e.g. `images/2x/cat.png`).
255 256
  ///
  /// If [excludeFromSemantics] is true, then [semanticLabel] will be ignored.
257 258 259 260 261 262 263
  //
  // TODO(ianh): Implement the following (see ../services/image_resolution.dart):
  // ///
  // /// * If [width] and [height] are both specified, and [scale] is not, then
  // ///   size-aware asset resolution will be attempted also, with the given
  // ///   dimensions interpreted as logical pixels.
  // ///
Ian Hickson's avatar
Ian Hickson committed
264 265 266
  // /// * If the images have platform, locale, or directionality variants, the
  // ///   current platform, locale, and directionality are taken into account
  // ///   during asset resolution as well.
267 268
  ///
  /// The [name] and [repeat] arguments must not be null.
269
  ///
Ian Hickson's avatar
Ian Hickson committed
270 271 272 273 274
  /// Either the [width] and [height] arguments should be specified, or the
  /// widget should be placed in a context that sets tight layout constraints.
  /// Otherwise, the image dimensions will change as the image is loaded, which
  /// will result in ugly layout changes.
  ///
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  /// ## Sample code
  ///
  /// Suppose that the project's `pubspec.yaml` file contains the following:
  ///
  /// ```yaml
  /// flutter:
  ///   assets:
  ///     - images/cat.png
  ///     - images/2x/cat.png
  ///     - images/3.5x/cat.png
  /// ```
  ///
  /// On a screen with a device pixel ratio of 2.0, the following widget would
  /// render the `images/2x/cat.png` file:
  ///
  /// ```dart
291
  /// Image.asset('images/cat.png')
292 293 294 295 296 297
  /// ```
  ///
  /// This corresponds to the file that is in the project's `images/2x/`
  /// directory with the name `cat.png` (the paths are relative to the
  /// `pubspec.yaml` file).
  ///
298 299 300 301 302 303 304 305
  /// On a device with a 4.0 device pixel ratio, the `images/3.5x/cat.png` asset
  /// would be used. On a device with a 1.0 device pixel ratio, the
  /// `images/cat.png` resource would be used.
  ///
  /// The `images/cat.png` image can be omitted from disk (though it must still
  /// be present in the manifest). If it is omitted, then on a device with a 1.0
  /// device pixel ratio, the `images/2x/cat.png` image would be used instead.
  ///
306 307 308 309 310 311 312 313 314 315
  ///
  /// ## Assets in packages
  ///
  /// To create the widget with an asset from a package, the [package] argument
  /// must be provided. For instance, suppose a package called `my_icons` has
  /// `icons/heart.png` .
  ///
  /// Then to display the image, use:
  ///
  /// ```dart
316
  /// Image.asset('icons/heart.png', package: 'my_icons')
317 318 319 320 321
  /// ```
  ///
  /// Assets used by the package itself should also be displayed using the
  /// [package] argument as above.
  ///
322
  /// If the desired asset is specified in the `pubspec.yaml` of the package, it
323
  /// is bundled automatically with the app. In particular, assets used by the
324
  /// package itself must be specified in its `pubspec.yaml`.
325 326
  ///
  /// A package can also choose to have assets in its 'lib/' folder that are not
327
  /// specified in its `pubspec.yaml`. In this case for those images to be
328 329 330 331 332 333 334 335 336
  /// bundled, the app has to specify which ones to include. For instance a
  /// package named `fancy_backgrounds` could have:
  ///
  /// ```
  /// lib/backgrounds/background1.png
  /// lib/backgrounds/background2.png
  /// lib/backgrounds/background3.png
  ///```
  ///
337
  /// To include, say the first image, the `pubspec.yaml` of the app should
338 339 340 341 342 343 344
  /// specify it in the assets section:
  ///
  /// ```yaml
  ///  assets:
  ///    - packages/fancy_backgrounds/backgrounds/background1.png
  /// ```
  ///
Ian Hickson's avatar
Ian Hickson committed
345
  /// The `lib/` is implied, so it should not be included in the asset path.
346 347
  ///
  ///
348 349 350 351 352 353
  /// See also:
  ///
  ///  * [AssetImage], which is used to implement the behavior when the scale is
  ///    omitted.
  ///  * [ExactAssetImage], which is used to implement the behavior when the
  ///    scale is present.
354 355
  ///  * <https://flutter.io/assets-and-images/>, an introduction to assets in
  ///    Flutter.
356
  Image.asset(String name, {
357 358
    Key key,
    AssetBundle bundle,
359 360
    this.semanticLabel,
    this.excludeFromSemantics = false,
361 362 363 364
    double scale,
    this.width,
    this.height,
    this.color,
365
    this.colorBlendMode,
366
    this.fit,
367 368
    this.alignment = Alignment.center,
    this.repeat = ImageRepeat.noRepeat,
369
    this.centerSlice,
370 371
    this.matchTextDirection = false,
    this.gaplessPlayback = false,
Ian Hickson's avatar
Ian Hickson committed
372
    String package,
373
  }) : image = scale != null
374 375
         ? ExactAssetImage(name, bundle: bundle, scale: scale, package: package)
         : AssetImage(name, bundle: bundle, package: package),
Ian Hickson's avatar
Ian Hickson committed
376 377 378 379
       assert(alignment != null),
       assert(repeat != null),
       assert(matchTextDirection != null),
       super(key: key);
380

381 382 383
  /// Creates a widget that displays an [ImageStream] obtained from a [Uint8List].
  ///
  /// The [bytes], [scale], and [repeat] arguments must not be null.
Ian Hickson's avatar
Ian Hickson committed
384 385 386 387 388
  ///
  /// Either the [width] and [height] arguments should be specified, or the
  /// widget should be placed in a context that sets tight layout constraints.
  /// Otherwise, the image dimensions will change as the image is loaded, which
  /// will result in ugly layout changes.
389 390
  ///
  /// If [excludeFromSemantics] is true, then [semanticLabel] will be ignored.
391 392
  Image.memory(Uint8List bytes, {
    Key key,
393
    double scale = 1.0,
394 395
    this.semanticLabel,
    this.excludeFromSemantics = false,
396 397 398
    this.width,
    this.height,
    this.color,
399
    this.colorBlendMode,
400
    this.fit,
401 402
    this.alignment = Alignment.center,
    this.repeat = ImageRepeat.noRepeat,
403
    this.centerSlice,
404 405
    this.matchTextDirection = false,
    this.gaplessPlayback = false,
406
  }) : image = MemoryImage(bytes, scale: scale),
Ian Hickson's avatar
Ian Hickson committed
407 408 409
       assert(alignment != null),
       assert(repeat != null),
       assert(matchTextDirection != null),
410 411
       super(key: key);

412 413 414 415 416 417 418
  /// The image to display.
  final ImageProvider image;

  /// If non-null, require the image to have this width.
  ///
  /// If null, the image will pick a size that best preserves its intrinsic
  /// aspect ratio.
Ian Hickson's avatar
Ian Hickson committed
419 420 421 422 423 424
  ///
  /// It is strongly recommended that either both the [width] and the [height]
  /// be specified, or that the widget be placed in a context that sets tight
  /// layout constraints, so that the image does not change size as it loads.
  /// Consider using [fit] to adapt the image's rendering to fit the given width
  /// and height if the exact image dimensions are not known in advance.
425 426 427 428 429 430
  final double width;

  /// If non-null, require the image to have this height.
  ///
  /// If null, the image will pick a size that best preserves its intrinsic
  /// aspect ratio.
Ian Hickson's avatar
Ian Hickson committed
431 432 433 434 435 436
  ///
  /// It is strongly recommended that either both the [width] and the [height]
  /// be specified, or that the widget be placed in a context that sets tight
  /// layout constraints, so that the image does not change size as it loads.
  /// Consider using [fit] to adapt the image's rendering to fit the given width
  /// and height if the exact image dimensions are not known in advance.
437 438
  final double height;

439
  /// If non-null, this color is blended with each image pixel using [colorBlendMode].
440 441
  final Color color;

442 443 444 445 446 447 448 449 450 451
  /// Used to combine [color] with this image.
  ///
  /// The default is [BlendMode.srcIn]. In terms of the blend mode, [color] is
  /// the source and this image is the destination.
  ///
  /// See also:
  ///
  ///  * [BlendMode], which includes an illustration of the effect of each blend mode.
  final BlendMode colorBlendMode;

Adam Barth's avatar
Adam Barth committed
452
  /// How to inscribe the image into the space allocated during layout.
453 454 455
  ///
  /// The default varies based on the other fields. See the discussion at
  /// [paintImage].
456
  final BoxFit fit;
457 458 459

  /// How to align the image within its bounds.
  ///
Ian Hickson's avatar
Ian Hickson committed
460
  /// The alignment aligns the given position in the image to the given position
461 462
  /// in the layout bounds. For example, an [Alignment] alignment of (-1.0,
  /// -1.0) aligns the image to the top-left corner of its layout bounds, while an
463
  /// [Alignment] alignment of (1.0, 1.0) aligns the bottom right of the
Ian Hickson's avatar
Ian Hickson committed
464
  /// image with the bottom right corner of its layout bounds. Similarly, an
465
  /// alignment of (0.0, 1.0) aligns the bottom middle of the image with the
Ian Hickson's avatar
Ian Hickson committed
466 467 468 469 470 471
  /// middle of the bottom edge of its layout bounds.
  ///
  /// To display a subpart of an image, consider using a [CustomPainter] and
  /// [Canvas.drawImageRect].
  ///
  /// If the [alignment] is [TextDirection]-dependent (i.e. if it is a
472
  /// [AlignmentDirectional]), then an ambient [Directionality] widget
Ian Hickson's avatar
Ian Hickson committed
473 474
  /// must be in scope.
  ///
475
  /// Defaults to [Alignment.center].
476 477 478 479 480 481 482
  ///
  /// See also:
  ///
  ///  * [Alignment], a class with convenient constants typically used to
  ///    specify an [AlignmentGeometry].
  ///  * [AlignmentDirectional], like [Alignment] for specifying alignments
  ///    relative to text direction.
483
  final AlignmentGeometry alignment;
484 485 486 487 488 489 490 491 492 493 494 495 496

  /// How to paint any portions of the layout bounds not covered by the image.
  final ImageRepeat repeat;

  /// The center slice for a nine-patch image.
  ///
  /// The region of the image inside the center slice will be stretched both
  /// horizontally and vertically to fit the image into its destination. The
  /// region of the image above and below the center slice will be stretched
  /// only horizontally and the region of the image to the left and right of
  /// the center slice will be stretched only vertically.
  final Rect centerSlice;

Ian Hickson's avatar
Ian Hickson committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
  /// Whether to paint the image in the direction of the [TextDirection].
  ///
  /// If this is true, then in [TextDirection.ltr] contexts, the image will be
  /// drawn with its origin in the top left (the "normal" painting direction for
  /// images); and in [TextDirection.rtl] contexts, the image will be drawn with
  /// a scaling factor of -1 in the horizontal direction so that the origin is
  /// in the top right.
  ///
  /// This is occasionally used with images in right-to-left environments, for
  /// images that were designed for left-to-right locales. Be careful, when
  /// using this, to not flip images with integral shadows, text, or other
  /// effects that will look incorrect when flipped.
  ///
  /// If this is true, there must be an ambient [Directionality] widget in
  /// scope.
  final bool matchTextDirection;

514 515 516 517
  /// Whether to continue showing the old image (true), or briefly show nothing
  /// (false), when the image provider changes.
  final bool gaplessPlayback;

518 519 520 521 522 523 524 525 526 527 528 529
  /// A Semantic description of the image.
  ///
  /// Used to provide a description of the image to TalkBack on Andoid, and
  /// VoiceOver on iOS.
  final String semanticLabel;

  /// Whether to exclude this image from semantics.
  ///
  /// Useful for images which do not contribute meaningful information to an
  /// application.
  final bool excludeFromSemantics;

530
  @override
531
  _ImageState createState() => _ImageState();
532 533

  @override
534 535
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
536 537 538 539 540 541 542 543 544 545 546 547
    properties.add(DiagnosticsProperty<ImageProvider>('image', image));
    properties.add(DoubleProperty('width', width, defaultValue: null));
    properties.add(DoubleProperty('height', height, defaultValue: null));
    properties.add(DiagnosticsProperty<Color>('color', color, defaultValue: null));
    properties.add(EnumProperty<BlendMode>('colorBlendMode', colorBlendMode, defaultValue: null));
    properties.add(EnumProperty<BoxFit>('fit', fit, defaultValue: null));
    properties.add(DiagnosticsProperty<AlignmentGeometry>('alignment', alignment, defaultValue: null));
    properties.add(EnumProperty<ImageRepeat>('repeat', repeat, defaultValue: ImageRepeat.noRepeat));
    properties.add(DiagnosticsProperty<Rect>('centerSlice', centerSlice, defaultValue: null));
    properties.add(FlagProperty('matchTextDirection', value: matchTextDirection, ifTrue: 'match text direction'));
    properties.add(StringProperty('semanticLabel', semanticLabel, defaultValue: null));
    properties.add(DiagnosticsProperty<bool>('this.excludeFromSemantics', excludeFromSemantics));
548 549 550 551 552 553
  }
}

class _ImageState extends State<Image> {
  ImageStream _imageStream;
  ImageInfo _imageInfo;
554
  bool _isListeningToStream = false;
555
  bool _invertColors;
556 557

  @override
558
  void didChangeDependencies() {
559 560
    _invertColors = MediaQuery.of(context, nullOk: true)?.invertColors
      ?? SemanticsBinding.instance.accessibilityFeatures.invertColors;
561
    _resolveImage();
562 563 564 565 566 567

    if (TickerMode.of(context))
      _listenToStream();
    else
      _stopListeningToStream();

568
    super.didChangeDependencies();
569 570
  }

571
  @override
572
  void didUpdateWidget(Image oldWidget) {
573
    super.didUpdateWidget(oldWidget);
574
    if (widget.image != oldWidget.image)
575 576 577
      _resolveImage();
  }

578 579
  @override
  void reassemble() {
580
    _resolveImage(); // in case the image cache was flushed
581 582 583
    super.reassemble();
  }

584
  void _resolveImage() {
585 586 587
    final ImageStream newStream =
      widget.image.resolve(createLocalImageConfiguration(
          context,
588
          size: widget.width != null && widget.height != null ? Size(widget.width, widget.height) : null
589 590 591
      ));
    assert(newStream != null);
    _updateSourceStream(newStream);
592 593
  }

594
  void _handleImageChanged(ImageInfo imageInfo, bool synchronousCall) {
595 596 597 598 599
    setState(() {
      _imageInfo = imageInfo;
    });
  }

600 601 602 603 604 605 606 607 608 609
  // Update _imageStream to newStream, and moves the stream listener
  // registration from the old stream to the new stream (if a listener was
  // registered).
  void _updateSourceStream(ImageStream newStream) {
    if (_imageStream?.key == newStream?.key)
      return;

    if (_isListeningToStream)
      _imageStream.removeListener(_handleImageChanged);

610 611 612
    if (!widget.gaplessPlayback)
      setState(() { _imageInfo = null; });

613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
    _imageStream = newStream;
    if (_isListeningToStream)
      _imageStream.addListener(_handleImageChanged);
  }

  void _listenToStream() {
    if (_isListeningToStream)
      return;
    _imageStream.addListener(_handleImageChanged);
    _isListeningToStream = true;
  }

  void _stopListeningToStream() {
    if (!_isListeningToStream)
      return;
    _imageStream.removeListener(_handleImageChanged);
    _isListeningToStream = false;
  }

632
  @override
633 634
  void dispose() {
    assert(_imageStream != null);
635
    _stopListeningToStream();
636 637
    super.dispose();
  }
638

639 640
  @override
  Widget build(BuildContext context) {
641
    final RawImage image = RawImage(
642
      image: _imageInfo?.image,
643 644
      width: widget.width,
      height: widget.height,
645
      scale: _imageInfo?.scale ?? 1.0,
646
      color: widget.color,
647
      colorBlendMode: widget.colorBlendMode,
648 649 650
      fit: widget.fit,
      alignment: widget.alignment,
      repeat: widget.repeat,
Ian Hickson's avatar
Ian Hickson committed
651 652
      centerSlice: widget.centerSlice,
      matchTextDirection: widget.matchTextDirection,
653
      invertColors: _invertColors,
654
    );
655 656
    if (widget.excludeFromSemantics)
      return image;
657
    return Semantics(
658 659 660 661 662
      container: widget.semanticLabel != null,
      image: true,
      label: widget.semanticLabel == null ? '' : widget.semanticLabel,
      child: image,
    );
663
  }
664 665

  @override
666
  void debugFillProperties(DiagnosticPropertiesBuilder description) {
667
    super.debugFillProperties(description);
668 669
    description.add(DiagnosticsProperty<ImageStream>('stream', _imageStream));
    description.add(DiagnosticsProperty<ImageInfo>('pixels', _imageInfo));
670
  }
671
}