4

I want to add asteric sign in InputDecoration labelText and change color(like red) of it so that user understand easily this field is required.

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

Expected result like this image Sample Image

miken32
  • 42,008
  • 16
  • 111
  • 154
Basher Sarkar
  • 104
  • 3
  • 13

3 Answers3

4

You can use label arguments inside InputDecoration parameters. This work both widgets TextField or TextFormField

TextField(
        decoration: InputDecoration(
          label: Row(
            children: [
              const Text('*', style: TextStyle(color: Colors.red)),
              Padding(
                padding: EdgeInsets.all(3.0),
              ),
              Text("Name")
            ],
          ),
        ),
        controller: _nameCtrlr,
        autofocus: true,
        keyboardType: TextInputType.text,
       );
Johan
  • 207
  • 4
  • 14
3

You can use the Rich Text Widget like this

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

Yes, man you can do that.

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

"\" sign will help compiler to distinguish between asterisk sign(*) from multiplication.

Jay Mungara
  • 6,663
  • 2
  • 27
  • 49