17

Context of my problem: Expanded widget shrink its child widgets to zero height if required and ignore its children constraints.

Goal: preventing an expanded widget from shrinking to less than its child size so that its widgets won't get destroyed.

In addition, I need the widget to take all available space and expand its child.

Side note: Container is used for allocating height space to make expanded react to the change in space

Here is the code:

import 'package:flutter/material.dart';

class MainScreen extends StatelessWidget {
  const MainScreen({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          Expanded(
            child: Card(
              color: Colors.blueAccent,
              child: LimitedBox(
                maxHeight: 200,
                child: Text(
                  'Test dummy text Test dummy text Test dummy text Test dummy text Test dummy text Test dummy text Test dummy text Test dummy text Test dummy text Test dummy text ',
                  style: TextStyle(fontSize: 60, color: Colors.black),
                ),
              ),
            ),
          ),
          Container(
            color: Colors.amberAccent,
            height: 105,
            width: 200,
          )
        ],
      ),
    );
  }
}

This is the unwanted current output:

Expanded behavior that is required

Expanded normal behavior:

Shrinking behavior that is NOT required

2
  • do you want second output? Commented Jun 30, 2020 at 9:47
  • No. I want to prevent first output Commented Jun 30, 2020 at 9:52

1 Answer 1

10

Use Flexible widget and add the Container with min-height, The size of blue container will increase as the text increases.

 Scaffold(
      appBar: AppBar(),
      body: SingleChildScrollView( 
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Flexible(
              child: Container(
                constraints: BoxConstraints(
                    minHeight: 200, minWidth: double.infinity),
                child: Card(
                  color: Colors.blueAccent,
                  child: Text(
                    'Sample Text',
                    style: TextStyle(fontSize: 60, color: Colors.black),
                  ),
                ),
              ),
            ),
            Container(
              color: Colors.amberAccent,
              height: 105,
              width: 200,
            )
          ],
        ),
      )
  ),

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

This solution will not solve my problem, I need both expanding and prevent shrinking over text behavior
I am not sure exactly what you want now, Can you describe more?
I want to add minimum height to expanded widget so that it will not shrink smaller than that size
No , its not what i want i want you to try that: First remove SingleChildScrollView then try to change the Container height from 100 to 2000. you will notice that the Flexible shrink to very small size and take over the text. I want to prevent this behavior of shrinking over text... i want to get error thatcontainer is overflow its size ... you will say just remove Flexible widget, but i want the widget to be Flexible in expanding because i use this expanding behavior in animation. thanks
so in few words .. i need Flexible with minHeight property
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.