简体   繁体   English

如何将图标小部件转换为 ImageProvider

[英]How to convert icon widget to ImageProvider

I want to use the Material design icons of flutter in my program, for that, I need to use the Icon widget.我想在我的程序中使用 Flutter 的 Material Design 图标,为此,我需要使用 Icon 小部件。 I have one network image (NetworkImage widget) and I want to display the Material design icon if there is an empty URL.我有一个网络图像(NetworkImage 小部件),如果 URL 为空,我想显示 Material design 图标。

Container(
      width: 48.0,
      height: 48.0,
      decoration: BoxDecoration(
          image: DecorationImage(
            image: imgLink.isEmpty
                ? Icon(Icons.person_outline)
                : NetworkImage(
                    imgLink,
                  ),
          ),
          borderRadius: BorderRadius.circular(10.0)),
    ),

it shows error as icons is not a subtype of ImageProvider.它显示错误,因为图标不是 ImageProvider 的子类型。 Now how I can convert Icon to ImageProvider or any other way.现在我如何将 Icon 转换为 ImageProvider 或任何其他方式。

Container BoxDecoration的图像仅支持ImageProvider类,该类具有以下实现者AssetBundleImageProviderFileImageMemoryImageNetworkImageResizeImageScrollAwareImageProvider因此它不支持图标小部件,因为图标小部件有自己的IconData类,这就是为什么扑给你错误, “图标不是 ImageProvider 的子类型”。

The solution is to create your own provider that converts the icon:解决方案是创建您自己的转换图标的提供程序:

import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class IconImageProvider extends ImageProvider<IconImageProvider> {
  final IconData icon;
  final double scale;
  final int size;
  final Color color;

  IconImageProvider(this.icon, {this.scale = 1.0, this.size = 48, this.color = Colors.white});

  @override
  Future<IconImageProvider> obtainKey(ImageConfiguration configuration) => SynchronousFuture<IconImageProvider>(this);

  @override
  ImageStreamCompleter load(IconImageProvider key, DecoderCallback decode) => OneFrameImageStreamCompleter(_loadAsync(key));

  Future<ImageInfo> _loadAsync(IconImageProvider key) async {
    assert(key == this);

    final recorder = ui.PictureRecorder();
    final canvas = Canvas(recorder);
    canvas.scale(scale, scale);
    final textPainter = TextPainter(textDirection: TextDirection.rtl);
    textPainter.text = TextSpan(
      text: String.fromCharCode(icon.codePoint),
      style: TextStyle(
        fontSize: size.toDouble(),
        fontFamily: icon.fontFamily,
        color: color,
      ),
    );
    textPainter.layout();
    textPainter.paint(canvas, Offset.zero);
    final image = await recorder.endRecording().toImage(size, size);
    return ImageInfo(image: image, scale: key.scale);
  }

  @override
  bool operator ==(dynamic other) {
    if (other.runtimeType != runtimeType) return false;
    final IconImageProvider typedOther = other;
    return icon == typedOther.icon && scale == typedOther.scale && size == typedOther.size && color == typedOther.color;
  }

  @override
  int get hashCode => hashValues(icon.hashCode, scale, size, color);

  @override
  String toString() => '$runtimeType(${describeIdentity(icon)}, scale: $scale, size: $size, color: $color)';
}

This is a standard ImageProvider , all the stock ones look much like this.这是一个标准的ImageProvider ,所有的股票看起来都像这样。 The actual conversion is inside _loadAsync() .实际转换在_loadAsync()内部。 Rather simple, just a normal Canvas operation to draw the icon as text (it is text, actually).相当简单,只是一个普通的Canvas操作将图标绘制为文本(实际上文本)。

如何保存 ImageProvider<object> 图像文件到画廊?<div id="text_translate"><p> 我想将从arSessionManager.snapshot()收到的ImageProvider&lt;Object&gt;文件保存到我的画廊。</p><p> 但是, ImageGallerySaver.saveImage()只能使用Uint8List图像。</p><p> 我应该怎么办?</p><p> 这是我的代码。</p><pre> Future&lt;void&gt; onTakeScreenshot() async { ImageProvider&lt;Object&gt; capture = await arSessionManager.snapshot(); await showDialog( context: context, builder: (_) =&gt; Dialog( child: Container( decoration: BoxDecoration( image: DecorationImage(image: capture, fit: BoxFit.cover)), ), ), ); await ImageGallerySaver.saveImage(capture); // the problematic part }</pre></div></object> - How to save ImageProvider<object> image file to Gallery?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在列表中添加小部件<imageprovider></imageprovider> - How to add widget in a List<ImageProvider> 如何将 CachedNetworkImage 转换为 ImageProvider? - How can I convert CachedNetworkImage to ImageProvider? 如何转换小部件? 到小部件 - How to convert Widget? to Widget 如何将小部件转换为小部件?? - How to convert widget to widget?? 如何在没有 BuildContext 的情况下解析 ImageProvider - How to resolve an ImageProvider without BuildContext Flutter Web:将ui.Image转换为ImageProvider - Flutter Web: Convert ui.Image to ImageProvider 如何保存 ImageProvider<object> 图像文件到画廊?<div id="text_translate"><p> 我想将从arSessionManager.snapshot()收到的ImageProvider&lt;Object&gt;文件保存到我的画廊。</p><p> 但是, ImageGallerySaver.saveImage()只能使用Uint8List图像。</p><p> 我应该怎么办?</p><p> 这是我的代码。</p><pre> Future&lt;void&gt; onTakeScreenshot() async { ImageProvider&lt;Object&gt; capture = await arSessionManager.snapshot(); await showDialog( context: context, builder: (_) =&gt; Dialog( child: Container( decoration: BoxDecoration( image: DecorationImage(image: capture, fit: BoxFit.cover)), ), ), ); await ImageGallerySaver.saveImage(capture); // the problematic part }</pre></div></object> - How to save ImageProvider<object> image file to Gallery? 如何使用 SvgPicture.string 作为 imageProvider flutter - how to use SvgPicture.string as a imageProvider flutter 如何以 imageprovider 类型传递图像资产? - How to pass image asset in imageprovider type? 如何解决“参数类型&#39;ImageProvider<Object> &#39;不能分配给参数类型&#39;ImageProvider&#39;?”颤动 - How to solve "The argument type 'ImageProvider<Object>' can't be assigned to the parameter type 'ImageProvider'?" in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM