To update height without pressing enter:
onChanged: (String e) {
int sizeIncreaseConstant = 30; //the fontSize 20 + buffer
int widthOfCharacter = 17; // 85% of fontsize
int newNumLines = ((e.length * widthOfCharacter)/widthOfContainer).truncate();
if( newNumLines != numLines) {
setState(() {
if(newNumLines > numLines)
heightOfContainer = heightOfContainer + sizeIncreaseConstant;
else
heightOfContainer = heightOfContainer - sizeIncreaseConstant;
numLines = newNumLines;
});
}
},
initial values:
int numLines = 0;
double widthOfContainer = 120;
double heightOfContainer = 50;
//fontSize = 20;
For this, you will have to use a font that has equal width for all characters like Monospace. And you will also need to determine the width of the character based on fontSize. It is supposed to be 50-60% of the font size but 85% worked for me.