Flutter MemoryImage


Flutter MemoryImage

简介

将给定的Uint8List 缓冲区解码为图像的widget

  • 甚至我们也可以利用的来实现将Base64编码的图片展示出来(利用 Uint8List.fromtList 构造函数)

基本用法

demo中我们作为读取内存图片来展示

  • 提供给MemoryImage的字节不应该再有更改
  • 如果需要经常改变的图片,请使用ImageProvider下的子类

实例演示

import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'dart:typed_data';

class MemoryImageDemo extends StatefulWidget {
  _MemoryImageDemoState createState() => _MemoryImageDemoState();
}

class _MemoryImageDemoState extends State<MemoryImageDemo> {
  Uint8List bytes;

  void initState() {
    super.initState();
    rootBundle.load('assets/images/food01.jpeg').then((data) {
      if (mounted) {
        setState(() {
          bytes = data.buffer.asUint8List();
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    final decoration =  BoxDecoration(
      image: bytes == null
          ? null
          :  DecorationImage(
              image:  MemoryImage(bytes,scale: 1.0),
            ),
    );
    return Container(
       width: 300.0,
          height: 300.0,
          decoration: decoration,
    );
  }
}