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

时间:2018-10-30 14:24:15

标签: image text flutter textures foreground

是否可以在Flutter中将图像(纹理)放在文本上?

如下图所示

enter image description here

1 个答案:

答案 0 :(得分:2)

在文本上绘制图像的操作是异步的,因为它等待图像被加载并将其设置在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));
}

下面,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,
            );
      }
    },
  );

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

complete example中也提供repo