large_images.dart 1.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
// Copyright 2014 The Flutter 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:typed_data';

import 'package:flutter/material.dart';

class LargeImagesPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final ImageCache imageCache = PaintingBinding.instance.imageCache;
    imageCache.maximumSize = 30;
    imageCache.maximumSizeBytes = 50 << 20;
    return GridView.builder(
      itemCount: 1000,
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
      itemBuilder: (BuildContext context, int index) => DummyImage(index),
    ).build(context);
  }
}

class DummyImage extends StatelessWidget {
  DummyImage(this.index) : super(key: ValueKey<int>(index));

  @override
  Widget build(BuildContext context) {
    final Future<ByteData> pngData = _getPngData(context);

    return FutureBuilder<ByteData>(
      future: pngData,
      builder: (BuildContext context, AsyncSnapshot<ByteData> snapshot) {
        // Use Image.memory instead of Image.asset to make sure that we're
        // creating many copies of the image to trigger the memory issue.
        return snapshot.data == null
            ? Container()
            : Image.memory(snapshot.data.buffer.asUint8List());
      },
    );
  }

  final int index;

  Future<ByteData> _getPngData(BuildContext context) async {
    return DefaultAssetBundle.of(context).load('assets/999x1000.png');
  }
}