flutter - 如何在 flutter 中使用 Paint() 在自定义 Paint 中编写文本

标签 flutter flutter-layout

我想在一个带有参数paint的小部件上方写入文本,该参数需要Paint()。该小部件在图形节点之间绘制边缘,并且它不接受任何像 TextPaint() 那样的参数。

enter image description here

最佳答案

下面是在 CustomPainter 或 CustomDecoration 内绘制文本的示例。您所需要的只是在 Canvas 上使用 TextPainter。

final textPainter = TextPainter(
  text: TextSpan(
    text: 'Foo\nBar',
    style: TextStyle(
      color: Colors.black,
      fontSize: 30,
    ),
  ),
  textDirection: TextDirection.ltr,
  textAlign: TextAlign.center
);
textPainter.layout();

// Draw the text centered around the point (50, 100) for instance
final offset = Offset(50 - (textPainter.width / 2), 100 - (textPainter.height / 2));
textPainter.paint(canvas, offset);

关于flutter - 如何在 flutter 中使用 Paint() 在自定义 Paint 中编写文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64184263/

相关文章:

AppBar 下方的 Flutter Drawer

flutter - 自动计算 `maxExtent` 的 `SliverPersistentHeaderDelegate`

flutter 桌面 : Disable scrolling when hover a Container

flutter - flutter 如何从共享首选项中删除特定 key

flutter - 是否可以从具有填充的列中排除 1 个小部件?

flutter - 如何修复此错误 Task 'assembleAarRelease' not found in root project 'flutter_plugin_android_lifecycle' .?

flutter - 防止覆盖抖动值

dart - 我如何创建列表列表

javascript - 将 JavaScript 函数转换为 Dart : Word of safety

firebase - Firestore 聊天应用 - 构建消息主页