|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:convert'; |
| 7 | +import 'dart:ui' as ui show Codec; |
| 8 | + |
| 9 | +import 'package:flutter/foundation.dart'; |
| 10 | +import 'package:flutter/material.dart'; |
| 11 | + |
| 12 | +/// An animated GIF image with 3 1x1 pixel frames (a red, green, and blue |
| 13 | +/// frames). The GIF animates forever, and each frame has a 100ms delay. |
| 14 | +const String kAnimatedGif = 'R0lGODlhAQABAKEDAAAA//8AAAD/AP///yH/C05FVFNDQVBFMi' |
| 15 | + '4wAwEAAAAh+QQACgD/ACwAAAAAAQABAAACAkwBACH5BAAKAP8A' |
| 16 | + 'LAAAAAABAAEAAAICVAEAIfkEAAoA/wAsAAAAAAEAAQAAAgJEAQ' |
| 17 | + 'A7'; |
| 18 | + |
| 19 | +/// A 50x50 blue square png |
| 20 | +const String kBlueSquare = 'iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAASEl' |
| 21 | + 'EQVR42u3PMQ0AMAgAsGFjL/4tYQU08JLWQSN/9TsgRERERERERE' |
| 22 | + 'REREREREREREREREREREREREREREREREREREREREQ2BgNuaUcSj' |
| 23 | + 'uqqAAAAAElFTkSuQmCC'; |
| 24 | + |
| 25 | +/// A 10x10 grid of animated looping placeholder gifts that fade into a |
| 26 | +/// blue square. |
| 27 | +class AnimatedPlaceholderPage extends StatelessWidget { |
| 28 | + @override |
| 29 | + Widget build(BuildContext context) { |
| 30 | + return GridView.builder( |
| 31 | + itemCount: 100, |
| 32 | + gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 10), |
| 33 | + itemBuilder: (BuildContext context, int index) { |
| 34 | + return FadeInImage( |
| 35 | + placeholder: DelayedBase64Image(Duration.zero, kAnimatedGif), |
| 36 | + image: DelayedBase64Image(Duration(milliseconds: 100 * index), kBlueSquare), |
| 37 | + ); |
| 38 | + }, |
| 39 | + ); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +int _key = 0; |
| 44 | +/// An image provider that is always unique from other DelayedBase64Images and |
| 45 | +/// simulates a delay in loading. |
| 46 | +class DelayedBase64Image extends ImageProvider<int> { |
| 47 | + const DelayedBase64Image(this.delay, this.data); |
| 48 | + |
| 49 | + final String data; |
| 50 | + |
| 51 | + final Duration delay; |
| 52 | + |
| 53 | + @override |
| 54 | + Future<int> obtainKey(ImageConfiguration configuration) { |
| 55 | + return SynchronousFuture<int>(_key++); |
| 56 | + } |
| 57 | + |
| 58 | + @override |
| 59 | + ImageStreamCompleter load(int key, DecoderCallback decode) { |
| 60 | + return MultiFrameImageStreamCompleter( |
| 61 | + codec: Future<ui.Codec>.delayed( |
| 62 | + delay, |
| 63 | + () => decode(base64.decode(data)), |
| 64 | + ), |
| 65 | + scale: 1.0, |
| 66 | + ); |
| 67 | + } |
| 68 | +} |
0 commit comments