Search code examples
fluttermobilealignment

Flutter TextField Hint or Text Not Align CenterLeft while set the Container height 40


I have used TextField inside Container with fixed width and height. Once I set container height:40 then TextField hint text and text are not aligned centerLeft but if I set Container height:50 then all things go fine. I have also tried to set fontSize but still not working. I have checked these are the questions : Question 1 , Question 2, Question 3 and also tried some other code also but won't worked while container height:40.

This is the Code that I am using :

Container(
  width: 300,
  height: 40,
  alignment: Alignment.centerLeft,
  decoration: BoxDecoration(
    borderRadius: BorderRadius.circular(10),
    color: Colors.deepPurpleAccent,
  ),
  child: TextFormField(
    textAlign: TextAlign.left,
    style: TextStyle(color: Colors.white),
    decoration: InputDecoration(
      contentPadding: EdgeInsets.only(left: 16),
      hintText: 'Enter your text',
      border: InputBorder.none,
      hintStyle: TextStyle(
        color: Colors.white,
      ),
    ),
  ),
),

UI(Out Put) when height:50 (perfect):

enter image description here enter image description here

UI(Out Put) when height:40(Issue with alignment):

enter image description hereenter image description here


Solution

  • You can use isDense: true, it will adapt the height.

    decoration: InputDecoration(
      contentPadding: EdgeInsets.only(left: 16),
      hintText: 'Enter your text',
      isDense: true,