我如何格式化(粗体)文本中的单词?
How can i format(bold) a word within a text?
在下面的代码中,我想将“$_counter”的值设为粗体。如何实现?
new Text(
'You have pushed the button $_counter time${_counter > 1
? 's'
: ''}',
style: new TextStyle(fontSize: fontSize),
textAlign: TextAlign.center,
),
我希望输出是这样的:
您已按下按钮 2 次
你必须使用 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!'),
],
),
)
您可以尝试在一行中附加文本小部件来实现此目的。
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 的答案更合适。
你可以为它设置fontWeight。
child: 新文本(
StringUtil.FORGOT_PASSWORD,
样式:新文本样式(
颜色:常量颜色(0xFF8C919E),
字体大小:12.0,
字母间距:0.3,
字体粗细:FontWeight.bold),
),
在下面的代码中,我想将“$_counter”的值设为粗体。如何实现?
new Text(
'You have pushed the button $_counter time${_counter > 1
? 's'
: ''}',
style: new TextStyle(fontSize: fontSize),
textAlign: TextAlign.center,
),
我希望输出是这样的:
您已按下按钮 2 次
你必须使用 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!'),
],
),
)
您可以尝试在一行中附加文本小部件来实现此目的。
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 的答案更合适。
你可以为它设置fontWeight。
child: 新文本(
StringUtil.FORGOT_PASSWORD,
样式:新文本样式(
颜色:常量颜色(0xFF8C919E),
字体大小:12.0,
字母间距:0.3,
字体粗细:FontWeight.bold),
),