skip to Main Content

I have this TextField:

Hint

As you see this is the hint of my TextField, aligned at right.

The problem is that when I type something inside the TextField, I want that value to be written from the left, but actually, it’s written on the right also:

TextField value

I want the hint to stay aligned, but when I type something, it goes to another side of TextField.

The code I’m trying:

TextField(
  textAlign: TextAlign.left,
);

Thank you!

2

Answers


  1. Try the following code:

    TextField(
      textDirection: TextDirection.ltr,
      decoration: InputDecoration(
        …
      ),
    ),
    
    Login or Signup to reply.
  2. Create this variable to keep text aligned:

    TextAlign textAlign = TextAlign.right;
    

    In the onChange method add:

    onChanged: (val) {
      if (val.isNotEmpty){
        setState(() {
          textAlign = TextAlign.left;
        });
      } else {
        setState(() {
          textAlign = TextAlign.right;
        });
      }
    },
    

    Finally, add this to your text field:

    TextField(
      textAlign: textAlign,
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search