仅使文本行中的某些单词加粗

时间:2019-09-22 07:48:04

标签: flutter dart

我试图在下面的代码中仅将“名称:”一词加粗,但是不确定这样做的最佳方法。我尝试了“ textspan”功能,并将两个单词放在单独的行上,但这似乎不起作用。

原始代码如下:

foreach (var Window in App.Current.Windows)
  if (Window.GetType().Name.Equals("Recorder"))
      ((Window)Window).Close();

1 个答案:

答案 0 :(得分:0)

您应该将RichTextTextSpan一起使用

Widget build(BuildContext context) {
    TextStyle defaultStyle = TextStyle(fontWeight: FontWeight.bold, color: Colors.black, fontSize: 20.0);
    return RichText(
          text: TextSpan(
            style: defaultStyle,
            text: "Name: ",
            children: <TextSpan>[
              TextSpan(text: 'Timmy', style: TextStyle(fontWeight: FontWeight.normal)),
            ],
          ),
        );
  }

或这样

Widget build(BuildContext context) {
    TextStyle defaultStyle = TextStyle(color: Colors.black, fontSize: 20.0);
    return RichText(
      text: TextSpan(
        style: defaultStyle,
        children: <TextSpan>[
          TextSpan(text: 'Name: ', style: TextStyle(fontWeight: FontWeight.bold)),
          TextSpan(text: 'Timmy'),
        ],
      ),
    );
  }

enter image description here