如何在Flutter中创建ImageShader?

问题描述 投票:0回答:1

我在玩 ShaderMask 并且它接受两种类型的 Shaders

Gradient & ImageShader

我可以创造 Gradient Shader 像这样

LinearGradient(colors: <Color>[
                      Colors.deepOrange,
                      Colors.blue,
                      Colors.green,
                      Colors.amber,
                    ]).createShader(bound)

但是 我找不到办法 营造 ImageShader.

当我尝试创建这样的:

ImageShader(
                    Image.network(
                      "https://product-image.juniqe-production.juniqe.com/media/catalog/product/cache/x800/401/62/401-62-101P.jpg",
                    ),
                    TileMode.mirror,
                    TileMode.mirror,
                    Float64List.fromList([1.0, 1, 0, 1, 1, 1, 1, 1, 0]));
              }

它说: 参数类型 widget/Image 不能分配给参数类型 ui/Painting/Image

我知道这两个图像是不同的,但我无法从widgetImage创建uiPaintingImage。

也有可能有一种直观的方法来创建 ImageShader?

什么是正确的方式来装箱ImageShader在Flutter?

flutter dart
1个回答
0
投票

在flutter里有两个叫做Image的类,在不同的包里。

有一个Widget,它的行为就像一个widget,可以通过它的命名构造函数从资产、内存或网络中实例化。

还有一个是ui包Image,它是在低级别的绘画时使用的。

import 'dart:ui' as ui;

例如:

 ImageShader(
   Image.network(
      "https://product-image.juniqe-production.juniqe.com/media/catalog/product/cache/x800/401/62/401-62-101P.jpg",
   ),
   TileMode.mirror,
   TileMode.mirror,
   Float64List.fromList([1.0, 1, 0, 1, 1, 1, 1, 1, 0]),
 );
© www.soinside.com 2019 - 2024. All rights reserved.