'How to add red asterisk in label of TextFormField In Flutter

As we are not able to make widget like RichText/Text Span For styling TextFormField,

Can anyone help me out regarding this...

Now Getting:-

enter image description here

Expected Result:-

enter image description here

How we can achieve a result like this?



Solution 1:[1]

Simplest way, but not exactly equals:

              TextField(
                decoration: InputDecoration(
                  hintText: 'Ciao',
                  suffixText: '*',
                  suffixStyle: TextStyle(
                    color: Colors.red,
                  ),
                ),
              ),

Or create a custom TextField to use hint as Widget instead of String

You can use my two customized files:

              TextFieldCustom(
                hintText: Text.rich(
                  TextSpan(
                    text: 'FIRST NAME',
                    children: <InlineSpan>[
                      TextSpan(
                        text: '*',
                        style: TextStyle(color: Colors.red),
                      ),
                    ],
                    style: TextStyle(color: Colors.black54),
                  ),
                ),
              ),

Solution 2:[2]

Try these two files I had same requirement. Just add attribute isMandate: true in CInputDecoration and use CTextField.

CTextField(
...
  decoration: new CInputDecoration(
     isMandate: true,
...
))

https://github.com/sumit1954/Flutter/blob/master/CInputDecorator.dart https://github.com/sumit1954/Flutter/blob/master/CTextField.dart

Solution 3:[3]

Achieved this in hard fast way. Replace input_decorator.dart with below code:

https://github.com/neal786y/InputDecoratorForMandatoryFields/blob/master/input_decorator.dart

In your InputDecoration scope add a property "isMandatoryField: true"

Worked for me on temporary basis.

Solution 4:[4]

With the latest version of flutter you can also send a widget in label parameter. So, something like this is possible

 label: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(label.replaceAll('*', '')),
          label.contains('*')
              ? Text(
                  '*',
                  style: TextStyle(color: Colors.red),
                )
              : Container(),
        ],
      ),

Solution 5:[5]

Place this code in TextFormField or TextField

this label works as an hint property

label: RichText(
  text: TextSpan(
    text: 'Full Name',
    style: const TextStyle(
      color: Colors.grey),
      children: [
        TextSpan(
          text: ' *',
          style: TextStyle(
          color: Colors.red,
        )
      )
    ]
  ),
  floatingLabelBehavior:FloatingLabelBehavior.never,
                                          

label parameter

look at above screenshot of label parameter holding a Widget not a String without any error

Solution 6:[6]

Use label property inside InputDecorator See this answer https://stackoverflow.com/a/72209425/8913302

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 andrea689
Solution 2 Sumit
Solution 3 user8253154
Solution 4 Cedric Wilson
Solution 5
Solution 6 Johan