fake_image_provider.dart 1.15 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8 9 10
import 'dart:async';
import 'dart:ui' as ui show Codec;

import 'package:flutter/foundation.dart';
11
import 'package:flutter/painting.dart';
12 13 14 15 16 17

/// An image provider implementation for testing that is using a [ui.Codec]
/// that it was given at construction time (typically the job of real image
/// providers is to resolve some data and instantiate a [ui.Codec] from it).
class FakeImageProvider extends ImageProvider<FakeImageProvider> {

18
  const FakeImageProvider(this._codec, { this.scale = 1.0 });
19 20 21 22 23 24 25 26

  final ui.Codec _codec;

  /// The scale to place in the [ImageInfo] object of the image.
  final double scale;

  @override
  Future<FakeImageProvider> obtainKey(ImageConfiguration configuration) {
27
    return SynchronousFuture<FakeImageProvider>(this);
28 29 30
  }

  @override
31
  ImageStreamCompleter load(FakeImageProvider key, DecoderCallback decode) {
32
    assert(key == this);
33 34
    return MultiFrameImageStreamCompleter(
      codec: SynchronousFuture<ui.Codec>(_codec),
35
      scale: scale,
36 37 38
    );
  }
}