26

I need a Container with some text in to auto expand. I have an API call, which can be anything from 5 words to 500 words. I don't want to just have 1 fixed size that's huge, but contains 10 words.

I have tried Expanded() and SizedBox.Expand(), but I might be using them wrong

Card( 
 elevation: defaultTargetPlatform ==
      TargetPlatform.android ? 5.0 : 0.0,
  child: Column(
    children: <Widget>[
      Container(
        margin: const EdgeInsets.all(0.0),
        padding: const EdgeInsets.all(2.0),
        decoration: BoxDecoration(color: Colors.black),
        width: _screenSize.width,
        height: 250,
        child: Column(
          children: <Widget>[
            Container(
              color: Colors.black,
              width: _screenSize.width,
              height: 35,
              child: Padding(
                padding: const EdgeInsets.only(
                    left: 15, top: 11),
                child: Text("Title".toUpperCase(),
                  style: TextStyle(
                      color: Colors.white
                  ),
                ),
              ),
            ),
            Container(
              color: Colors.white,
              width: _screenSize.width,
              height: 210,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(top: 8, bottom: 5),
                    child: Text("Title of expanding text", style: TextStyle(
                      fontSize: 25,
                    ),
                    ),
                  ),
                  Text("Expanding text", style: TextStyle(
                      fontSize: 35,
                      fontWeight: FontWeight.w800
                  ),),
                ],
              ),
            ),
          ],
        ),
      ),
    ],
  ),
),

I just need the Container to expand, but stay small/get bigger

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Randy
  • 381
  • 1
  • 4
  • 5

5 Answers5

34

Have you tried not specifying height at all? The Container should wrap according to the child in this case.

Otherwise, the widget has a child but no height, no width, no constraints, and no alignment, and the Container passes the constraints from the parent to the child and sizes itself to match the child.

Above is an extract from the official flutter documentation for Container.

Here is the official flutter documentation link.

bytesizedwizard
  • 5,529
  • 3
  • 17
  • 39
23

You can use FittedBox, this will resize your text according to available area.

You can use it like this :

FittedBox(child: Text('...............Your text...............'));
mega6382
  • 9,211
  • 17
  • 48
  • 69
Kalpesh Kundanani
  • 5,413
  • 4
  • 22
  • 32
17

I would suggest you to use Constraints...this will set Container height according to the Text child's requirement. Please see the example...

Container(
    constraints: BoxConstraints(
    maxHeight: double.infinity,
),
child: Column(
children: [
  Text(
    'Hello flutter...i like flutter...i like google...',
    softWrap: true,
    style: TextStyle(
        color: Colors.white, fontSize: 20 , ),

  ),],),)
Chandan
  • 11,465
  • 1
  • 6
  • 25
Fun Tadka
  • 171
  • 1
  • 4
9

we just neeed to add mainAxisSize: MainAxisSize.min, properties inside child Column or Row where the child is set to Container

for example

AnythingYourWidget(
  child: Container(
    child: Column( // For Example Column
      mainAxisSize: MainAxisSize.min, // these properties following the children content height available.
      children: [
          // YourWidget
      ]
    )
  )
),
Yogi Arif Widodo
  • 563
  • 6
  • 22
4

I too had a container with a text widget inside that would not scale as the text increased in character count. Make the widget tree Container -> IntrinsicWidth -> Text/TextField and it seems to play nicely for me.

IntrinsicWidth will scale the size of the container to its child.