答案 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。