[英]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类,该类具有以下实现者AssetBundleImageProvider
, FileImage
, MemoryImage
, NetworkImage
, ResizeImage
, ScrollAwareImageProvider
因此它不支持图标小部件,因为图标小部件有自己的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
操作将图标绘制为文本(实际上是文本)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.