Flutter Image 对象到 ImageProvider

Posted

技术标签:

【中文标题】Flutter Image 对象到 ImageProvider【英文标题】:Flutter Image object to ImageProvider 【发布时间】:2020-03-11 05:14:27 【问题描述】:

我必须从 base64 读取我的图像源才能抖动 Image 对象。

Image img = Image.memory(base64Decode(BASE64_STRING));

然后我想将图像作为容器背景。但是DecorationImage 只接受ImageProvider。

如何将 Image 转换为 ImageProvider?或者有没有其他方式将base64图像传递给ImageProvider?

Container(
  decoration: BoxDecoration(
    color: Colors.green,
    image: DecorationImage(
      image: img // <-- Expecting ImageProvider
    )
)

【问题讨论】:

How to convert BASE64 string into Image with Flutter?的可能重复 @RavinderKumar 不,不是。如您所见,我已经将 base64 转换为 Image。但是 Image 对象与 BoxDecoration -> DecorationImage 不兼容。只有 ImageProvider 是兼容的。 你有来自the link above 的checked the Accepted Answer。他这样做就像Uint8List bytes = BASE64.decode(_base64); return new Scaffold( appBar: new AppBar(title: new Text('Example App')), body: new ListTile( leading: new Image.memory(bytes), title: new Text(_base64), ), ); Image.memory 返回 Image 而不是 ImageProvider,顺便说一句,解决方案是 MemoryImage 【参考方案1】:

在您的 ImageProvider 上调用 .image

在你的情况下:

Container(
  decoration: BoxDecoration(
    color: Colors.green,
    image: DecorationImage(
      image: img.image // <--- .image added here
    )
)

【讨论】:

在加载“NetworkImage”之前如何使用“NetworkImage”的占位符?【参考方案2】:

在这种情况下,您可以使用:

Image img = Image.memory(base64Decode(BASE64_STRING));

Container(
  decoration: BoxDecoration(
    color: Colors.green,
    image: DecorationImage(
      image: img.image // convert Image object to ImageProvider
    )
)

【讨论】:

【参考方案3】:

请使用 Uint8List 和 MemoryImage ,见代码 sn -p 您可以在下面复制粘贴运行完整代码 代码sn-p

import 'dart:typed_data';
...
Uint8List bytes = base64Decode(BASE64_STRING);
...
Container(
              height: 200,
              decoration: BoxDecoration(
                  color: Colors.green,
                  image: DecorationImage(
                      image: MemoryImage(bytes)
                  )
              )),

工作演示

完整代码

import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:typed_data';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget 
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  


class MyHomePage extends StatefulWidget 
  MyHomePage(Key key, this.title) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();


class _MyHomePageState extends State<MyHomePage> 
  int _counter = 0;

  void _incrementCounter() 
    setState(() 
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    );
  

  @override
  Widget build(BuildContext context) 
    String BASE64_STRING = 'iVBORw0KGgoAAAANSUhEUgAAANIAAAAzCAYAAADigVZlAAAQN0lEQVR4nO2dCXQTxxnHl0LT5jVteHlN+5q+JCKBJITLmHIfKzBHHCCYBAiEw+I2GIMhDQ0kqQolIRc1SV5e+prmqX3JawgQDL64bK8x2Ajb2Bg7NuBjjSXftmRZhyXZ1nZG1eL1eGa1kg2iyua9X2TvzvHNN/Ofb2Z2ZSiO4ygZGZm+EXADZGSCgYAbICMTDATcABmZYCDgBsjIBAMBN0BGJhgIuAEyMsGA1wQdHZ1UV1cX5XK5qM7Ozg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NA1sMhgdANHLvgzo7u9zXK1n20PnzaVYQ8ZbB5SFBSPzszkp0vgLjEG+dyNL4iEBacvBovHQcFIeU42ZWpEP7KiTSS75qifmF/sS1lwc30H3pB1xkEgpJIZKfj5q4yOevkEjix054fgsJfu0BwkcZEqCs3zQ2Ne8pLin5urpad8hkaltQUnLjGbDfimQyLhjg298gDe7tb9Isoabx3wRV0/jXTvgBrfKkE+aLE8kjzCtcQvD5FB7UCLgyQgh288tTJSEfaVJB68QRQXt/N1GBaRuPmsY/OyP5UYov+DTCvBq65/JRCGq/AlM3tF+4xBSzQYncw7VPCOlhff8ICQqotq7OfRghWKphMZstaxKTUywnTp5qphp2vOn0mXNcKpNhPpWYxKWmpjeDZd0WtG4vjZORuRcoafEI2QO/hASXdAajUcozpEGF14uPpgPhWK22xRaLdUbV7eo3b9ws28+yVXsdDvtceHonC0nmPoShey89ien9jkjNLQaqrc1MxASw2donpaZn1JeVlyeBfdEv2232O/sjMe4DJ8r8+GDo7i8K4va1KrH8PgsJPkuC+yL4tgL8JAGPucvKK2MzM7PaWltbl4AyB/wvj10Wksz9CCeCaDSC+CQkGInq6utF90Q8oIzf5l0tuFheXvkPsI962HN6JwtJ5n6FofEiwn3hsxeShVQF9kVQRPDfSZKwN6Kampt3Xiu83mQymcL5a/BrE1BMspBk7kNUdO8TVeGJoCiShOR+DaiuTvKfFQbpHqmoqMzW6/WJ8PgbOQ6XkQlKsBd5IUFaDAbJkQhitdpWgKUg226zLYS/y0KS+TGAvdjc3OKmqamFamtroywWq+gpHY/ZbBnU3GL4FHx+A8r5BeEhrYxM0BFwA2RkgoGAGyAjEwwE3AAZmWAg4AbIyAQDATdARiYYCLgBMjLBQMANkJEJBgJugIxMMPBfChd6NRZ5pkMAAAAASUVORK5CYII=';
    Image img = Image.memory(base64Decode(BASE64_STRING));
    Uint8List bytes = base64Decode(BASE64_STRING);
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              height: 200,
              decoration: BoxDecoration(
                  color: Colors.green,
                  image: DecorationImage(
                      image: MemoryImage(bytes)
                  )
              )),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  

【讨论】:

【参考方案4】:

虽然您可以使用myImageWidget.imageImage 小部件中获取ImageProvide,但您也可以直接从ImageProvider 开始:

final imageProvider = MemoryImage(bytes);

Image.memory() 为您提供Image 小部件,但MemoryImage 为您提供ImageProvider

有关网络、资产和文件图像,请参阅我的完整答案 here。

【讨论】:

在加载“NetworkImage”之前如何使用“NetworkImage”的占位符? @Kamlesh,您可能想查看cached_network_image 包。 Suragch - 谢谢亲爱的,cached_network_image 是否提供了 imageProvider,因为我的网络图像有 photo_view 包,需要 imageprovider 类型的子项。任何建议。谢谢。

以上是关于Flutter Image 对象到 ImageProvider的主要内容,如果未能解决你的问题,请参考以下文章

Flutter自定义CustomPainter中获取ImageShader参数ui.Image对象的方法

Flutter自定义CustomPainter中获取ImageShader参数ui.Image对象的方法

如何在 Flutter 中获取 Asset 对象的路径?

flutter画布绘制图片和文字

Flutter图片加载和缓存机制探究

检查图像是不是已加载到 Flutter 中的 Image.network 小部件中