首页 > 解决方案 > 在 Flutter 的输入装饰中为 labelText 添加强制(*)

问题描述

我想在InputDecorationlabelText 中添加星号并更改它的颜色(如红色),以便用户轻松理解此字段是必需的。

TextField(
    autofocus: true,
    controller: _nameCtrlr,
    keyboardType: TextInputType.text,
    decoration: InputDecoration(
    labelText: "Name *",
   ))

像这个图像的预期结果 示例图像

标签: flutterdartflutter-layoutlabel

解决方案


您可以像这样使用富文本小部件

RichText getRequiredLabel(String fieldName) {
  return RichText(
      text: TextSpan(
          style: TextStyle(color: Colors.black),
          text: fieldName,
          children: [
        TextSpan(text: '*', style: TextStyle(color: Colors.red))
      ]));
}

推荐阅读