How to add red asterisk in label of TextFormField In Flutter

5,256

Solution 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

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

Share:
5,256
Zeeshan Ansari
Author by

Zeeshan Ansari

Updated on December 15, 2022

Comments

  • Zeeshan Ansari
    Zeeshan Ansari over 1 year

    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?

  • Elijan9
    Elijan9 about 4 years
    Welcome at Stackoverflow. While your answer may help to achieve what the original poster wants, your post offers no guidance or explanation that may help others seeking a similar solution. Please include more context in your reply, see How do I write a good answer?
  • Chung
    Chung almost 4 years
    But when i use your two customized files, validator of TextFiled disappear?
  • gowthaman C
    gowthaman C over 2 years
    can you share null safety file
  • gowthaman C
    gowthaman C over 2 years
    can you share null safety file
  • gowthaman C
    gowthaman C over 2 years
    can you share null safety file
  • Phúc Nguyễn
    Phúc Nguyễn almost 2 years
    The label param takes a text, not a widget.