首页 > 解决方案 > 如何格式化(粗体)文本中的单词?

问题描述

在下面的代码中,我想将“$_counter”的值加粗。如何做到这一点?

new Text(
  'You have pushed the button $_counter time${_counter > 1
      ? 's'
      : ''}',
  style: new TextStyle(fontSize: fontSize),
  textAlign: TextAlign.center,
),

我希望输出是这样的:

您已按下按钮2

标签: android-studiodartflutterflutter-layout

解决方案


你必须使用RichTextfor

new RichText(
textAlign: TextAlign.center,
          text: new TextSpan(
            style: TextStyle(color: Colors.black),
            children: <TextSpan>[
              new TextSpan(text: 'You have pushed the button '),
              new TextSpan(text: '$_counter', style: new TextStyle(fontWeight: FontWeight.bold)),
              new TextSpan(text: ' time!'),
            ],
          ),
        )

在此处输入图像描述


推荐阅读