fake_image_provider.dart 1.14 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2017 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.

import 'dart:async';
import 'dart:ui' as ui show Codec;

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

/// 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> {

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

  final ui.Codec _codec;

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

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

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