如何在Flutter中装饰文字笔划?

gra*_*ste 10 flutter

如何在Flutter中装饰文字笔划?这就像-webkit-text-stroke-CSS

Pie*_*lse 17

我也在寻找这个,但是找不到它.但我确实在TextStyle中找到了使用4个阴影的解决方法:

Text("Border test",
    style: TextStyle(
      inherit: true,
      fontSize: 48.0,
      color: Colors.pink,
      shadows: [
        Shadow( // bottomLeft
          offset: Offset(-1.5, -1.5),
          color: Colors.white
        ),
        Shadow( // bottomRight
          offset: Offset(1.5, -1.5),
          color: Colors.white
        ),
        Shadow( // topRight
          offset: Offset(1.5, 1.5),
          color: Colors.white
        ),
        Shadow( // topLeft
          offset: Offset(-1.5, 1.5),
          color: Colors.white
        ),
      ]
    ),
);
Run Code Online (Sandbox Code Playgroud)

我还在GitHub上打开了一个问题:https://github.com/flutter/flutter/issues/24108

  • 字体看起来很乱,线条不干净。 (2认同)

Sky*_*ost 6

如果您更喜欢阴影方法,可以使用以下命令配置描边宽度:

/// Outlines a text using shadows.
static List<Shadow> outlinedText({double strokeWidth = 2, Color strokeColor = Colors.black, int precision = 5}) {
  Set<Shadow> result = HashSet();
  for (int x = 1; x < strokeWidth + precision; x++) {
    for(int y = 1; y < strokeWidth + precision; y++) {
      double offsetX = x.toDouble();
      double offsetY = y.toDouble();
      result.add(Shadow(offset: Offset(-strokeWidth / offsetX, -strokeWidth / offsetY), color: strokeColor));
      result.add(Shadow(offset: Offset(-strokeWidth / offsetX, strokeWidth / offsetY), color: strokeColor));
      result.add(Shadow(offset: Offset(strokeWidth / offsetX, -strokeWidth / offsetY), color: strokeColor));
      result.add(Shadow(offset: Offset(strokeWidth / offsetX, strokeWidth / offsetY), color: strokeColor));
    }
  }
  return result.toList();
}
Run Code Online (Sandbox Code Playgroud)

像这样使用它:

Text(
  'My text',
  style: TextStyle(shadows: outlinedText(strokeColor: Colors.blue)),
);
Run Code Online (Sandbox Code Playgroud)


Ale*_*leh 5

受这篇文章的启发,为了达到效果,我更喜欢使用一种将两个 Text 小部件和 TextStype.foreground 属性与自定义 Paint() 混合的技术:

class StrokeText extends StatelessWidget {
  final String text;
  final double fontSize;
  final FontWeight fontWeight;
  final Color color;
  final Color strokeColor;
  final double strokeWidth;

  const StrokeText(
    this.text, {
    Key key,
    this.fontSize,
    this.fontWeight,
    this.color,
    this.strokeColor,
    this.strokeWidth,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Text(
          text,
          style: TextStyle(
            fontSize: fontSize,
            fontWeight: fontWeight,
            foreground: Paint()..color = color,
          ),
        ),
        Text(
          text,
          style: TextStyle(
            fontSize: fontSize,
            fontWeight: fontWeight,
            foreground: Paint()
              ..strokeWidth = strokeWidth
              ..color = strokeColor
              ..style = PaintingStyle.stroke,
          ),
        ),
      ],
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


Gar*_*ian 5

由于在TextStyle中添加了前景色,因此无需任何变通方法即可进行中风。TextStyle文档中添加了填充边框文本下方的笔触的显式示例:https : //master-api.flutter.dev/flutter/painting/TextStyle-class.html#painting.TextStyle.6

此示例在此处转载:

在此处输入图片说明

Stack(
  children: <Widget>[
    // Stroked text as border.
    Text(
      'Greetings, planet!',
      style: TextStyle(
        fontSize: 40,
        foreground: Paint()
          ..style = PaintingStyle.stroke
          ..strokeWidth = 6
          ..color = Colors.blue[700],
      ),
    ),
    // Solid text as fill.
    Text(
      'Greetings, planet!',
      style: TextStyle(
        fontSize: 40,
        color: Colors.grey[300],
      ),
    ),
  ],
)
Run Code Online (Sandbox Code Playgroud)

可以通过删除堆栈并仅使用第一个笔触文本小部件来单独进行笔划。笔划/填充顺序也可以通过交换第一个和第二个“文本”小部件来调整。

  • 自从 Flutter 升级为使用 Impeller 渲染引擎后,这在 iOS 上不再适用。阴影解决方案很好。 (5认同)