颤动中文本上的纹理(图像)

Abd*_*ؤمن 3 text textures image foreground flutter

有没有办法在 Flutter 中将图像(纹理)放在文本上?

如下图所示

在此处输入图片说明

Abd*_*ؤمن 6

在文本上绘制图像的操作是异步的,因为它等待图像加载并将其设置在TextStyle前景着色器上。

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;

String textureName = "images/texture.jpg";
 Future<TextStyle> textureText(TextStyle textStyle,
    ) async {
  ui.Image img;
  img = await ImageLoader.load(textureName);
  Float64List matrix4 = new Matrix4.identity().storage;
  return textStyle.copyWith(
      foreground: Paint1()
        ..shader = ImageShader(img, TileMode.mirror, TileMode.mirror, matrix4));
}
Run Code Online (Sandbox Code Playgroud)

下面,FutureBuilder调用textureText并使用返回的TextStyle

  Widget tt = new FutureBuilder<TextStyle>(
    future: texturedText(
        TextStyle(fontSize: 70.0,), // a Future<String> or null
    builder: (BuildContext context, AsyncSnapshot<TextStyle> snapshot) {
      switch (snapshot.connectionState) {
        case ConnectionState.waiting:
          return new Text('Awaiting result...');
        default:
          if (snapshot.hasError)
            return new Text('Error: ${snapshot.error}');
          else
            return new Text(
              'TEXTURE',
              style: snapshot.data,
            );
      }
    },
  );
Run Code Online (Sandbox Code Playgroud)

您可以tt像普通小部件一样使用(texturedText)。

repo 中还提供了一个完整的示例