Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change text fragment in TextFormField and make it static (not changeable) Flutter

I have added the text that is displayed in the TextFormField. I need to change the text size of not everything, but only KWh, make it smaller + can the € and KWh values ​​be made static so that they cannot be changed?

code

TextFormField(
            keyboardType: TextInputType.number,
            controller: _priceController
              ..text = '€' + widget.price.toStringAsFixed(2) + ' KWh',
            style: widget.textStyle,
            textAlign: TextAlign.center,
            decoration: const InputDecoration(
              contentPadding: EdgeInsets.zero,
              border: InputBorder.none,
            ),
          ),

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is why Suffix and Prefix are provided in InputDecoration.

To use them for your desired output, do as:

TextFormField(
  keyboardType: TextInputType.number,
  controller: _priceController
      ..text = widget.price.toStringAsFixed(2),
  style: widget.textStyle,
  textAlign: TextAlign.center,
  decoration: const InputDecoration(
  prefix: Text('€',
      style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
  suffix: Text('KWh',
      style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
  contentPadding: EdgeInsets.zero,
  border: InputBorder.none,
  ),
),

You can change the color, fontSize, fontWeight or any such TextStyle property by using widgets for Suffix and Prefix. Prefix and Suffix stay static and can’t be changed/Edited by the user.

Output:

enter image description here

Do Remember, Suffix is placed at the end of the TextFormField, so, you’ve to adjust the its width according to that.

Else, it will look like this with a full width TextFormField:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading