📅  最后修改于: 2023-12-03 15:00:48.872000             🧑  作者: Mango
在 Flutter 中,文本可用于在屏幕上实现各种不同的效果,例如显示标题、正文、列表项、按钮等等。Flutter 提供了一些不同的 Widget,以帮助开发者轻松地在屏幕上显示文本。
最基本的文本 Widget 是 Text
,它可以在屏幕上显示一行文本。我们可以通过 Text
的构造函数来设置文本内容、颜色、大小、字体等样式。
Text(
'Hello, world!',
style: TextStyle(
color: Colors.black,
fontSize: 24.0,
fontWeight: FontWeight.bold,
),
)
其中,TextStyle
用于设置文本样式。我们可以通过它的属性来改变文本的颜色、字体大小、粗细等等。
如果我们需要在一段文本中设置不同的样式,例如将一部分文本设置成蓝色,一部分设置成红色,那么可以使用 RichText
Widget。
RichText(
text: TextSpan(
text: 'Hello, ',
style: TextStyle(color: Colors.black),
children: <TextSpan>[
TextSpan(
text: 'world',
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
TextSpan(text: '!'),
],
),
)
在 RichText
中,我们需要通过 TextSpan
来指定不同的文本样式。
如果我们需要让用户输入文本,那么可以使用 TextFormField
Widget。
TextFormField(
decoration: InputDecoration(
labelText: 'Username',
hintText: 'Enter your username',
),
)
在 TextFormField
中,我们可以通过 decoration
属性来设置文本框的样式,包括提示文本、标签文本等。
以上介绍了几个 Flutter 中常用的文本 Widget,包括 Text
、RichText
和 TextFormField
。通过使用它们,我们可以轻松地在屏幕上显示文本、设置不同的文本样式、让用户输入文本等等。