【问题标题】:How can i format(bold) a word within a text?如何格式化(粗体)文本中的单词?
【发布时间】:2018-12-21 11:36:35
【问题描述】:

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

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

我希望输出是这样的:

您已按下按钮 2

【问题讨论】:

    标签: android-studio dart flutter flutter-layout


    【解决方案1】:

    你必须使用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!'),
                ],
              ),
            )
    

    【讨论】:

    • 我已经提到了这个例子,但我想知道如何在我的特定类型的代码中应用它!
    • 您能帮我关闭更新代码中的所有大括号吗?
    • 确定你的问题是什么?
    • 还有一个问题,如何在上面的代码中添加textAlign?
    • 富文本中有一个属性我再次更新了我的答案
    【解决方案2】:

    您可以尝试连续添加文本小部件来实现此目的。

    final prefixText = Text('You have clicked the button');
    
    final counterText = Text(' $_counter', style: TextStyle(fontWeight: FontWeight.bold),);
    
    final suffixText = Text(' times');
    
    return Scaffold(
          body: Center(
            child: Row(
              children: <Widget>[prefixText, counterText, suffixText],
            ),
          ),
        );
    

    注意:虽然您使用它获得了所需的输出,但我认为@Raouf Rahiche 的答案更合适。

    【讨论】:

    • 对不起,如果我听起来很傻,但我想弄清楚在整个文件中定义 prefixText、counterText 和 suffixText 的位置?
    • 你可以在 'build' 方法中定义它,最终返回 Scaffold。
    【解决方案3】:

    你可以为它设置 fontWeight。

    孩子:新文本(
    StringUtil.FORGOT_PASSWORD,
    样式:新文本样式(
    颜色:常量颜色(0xFF8C919E),
    字体大小:12.0,
    letterSpacing: 0.3,
    fontWeight: FontWeight.bold),
    ),

    【讨论】:

      猜你喜欢
      • 2020-01-25
      • 2023-03-14
      • 2014-05-15
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多