can you make a container whose child is a textformfield expand based on the number of lines of text?

Mainly the container wasn’t expanding is, because you have inserted

final TextEditingController myController = TextEditingController();
int numLines = 1;
int charLength = 0;
chatBarHeight = 62.5;
//= 52.5;//52.5;
int lastIndexNewLine = 0;

inside the build method of ChatBubbleState; this will cause the chatBarHeight to be 62.5 even if you change it inside onChanged property of the textFormField.

Also if you wanted the container to expand, First remove the expands property from the textFormField, this property will expand the textformField to fit it’s container not expand while typing and second you don’t need the calculation inside the onChanged property.

import 'package:flutter/material.dart';

void main() {
    home: ExpandingText(),

class ExpandingText extends StatefulWidget {
  _ExpandingText createState() => _ExpandingText();

class _ExpandingText extends State<ExpandingText> {
  TextEditingController myController = TextEditingController();

  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          width: 310.0,
          decoration: BoxDecoration(
            color: const Color(0xffffffff),
            boxShadow: [
                color: const Color(0x29000000),
                offset: Offset(3, 3),
                blurRadius: 6,
          child: TextFormField(
            textAlign: TextAlign.start,
            style: TextStyle(
                fontSize: 17,
                color: const Color(0xd9343f4b),
                fontFamily: 'Lato'),
            maxLines: null,
            textCapitalization: TextCapitalization.sentences,
            controller: myController,
            decoration: InputDecoration(
                hintStyle: TextStyle(
                  color: Color(0x80343f4b),
                  fontFamily: 'Lato',
                  fontSize: 15,
                // hintStyle: ,
                hintText: 'Enter Comment'),

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top