在Text Widget 中不可能,请使用RichText Widget,它将TextSpan 作为子级。
将Text 更改为Text Span
List<Widgets> widgets = [
TextSpan(text: 'Theo', style: TextStyle(color: Colors.green)),
TextSpan(text: 'Luca', style: TextStyle(color: Colors.blue))
]
每一行都是一个RichText。 RichText 将TextSpan 作为子元素,其中必须指定文本和相应的样式。
widgets.map((textSpan) {
return Column(
children: [
RichText(
text: TextSpan(
text: 'My name is ',
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
textSpan // Name with color will appear here
],
),
RichText(
children: <TextSpan>[
textSpan // Name with color will appear here
TextSpan (
text: 'How old are you? ',
style: DefaultTextStyle.of(context).style,
),
],
),
],
)
}
或者,这可以通过使用 Row 和 Text 小部件来复制,但它不会是一行。