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

Time:05-03

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

CodePudding user response:

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

  • Related