简体   繁体   中英

How expanded widget works in flutter?

I have a simple code which produces design like this

设计 This is my code

This is my code for the design:

class InputPage extends StatefulWidget {
  @override
  _InputPageState createState() => _InputPageState();
}

class _InputPageState extends State<InputPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('BMI CALCULATOR'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          Expanded(
            child: Row(
              children: [
                Expanded(
                  child: ReusableCard(colour: Color(0xff1d1e33),)
                ),
                Expanded(
                  child: ReusableCard(colour: Color(0xff1d1e33),)
                ),
              ],
            ),
          ),
          Expanded(
            child: ReusableCard(colour: Color(0xff1d1e33)),
          ),
          Expanded(
            child: Row(
              children: [
                Expanded(
                  child: ReusableCard(colour: Color(0xff1d1e33),)
                ),
                Expanded(
                  child: ReusableCard(colour: Color(0xff1d1e33),)
                ),
              ],
            ),
          ),
          Container(
            width: double.infinity,
            height: 100,
            color: Colors.pink,
          )
        ],
      ),
    );
  }
}

Now when I remove the parent expanded widget, I get a design like this:

设计

Here is the code for this design:

class _InputPageState extends State<InputPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('BMI CALCULATOR'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          Row(
            children: [
              Expanded(
                child: ReusableCard(colour: Color(0xff1d1e33),)
              ),
              Expanded(
                child: ReusableCard(colour: Color(0xff1d1e33),)
              ),
            ],
          ),
          Expanded(
            child: ReusableCard(colour: Color(0xff1d1e33)),
          ),
          Row(
            children: [
              Expanded(
                child: ReusableCard(colour: Color(0xff1d1e33),)
              ),
              Expanded(
                child: ReusableCard(colour: Color(0xff1d1e33),)
              ),
            ],
          ),
          Container(
            width: double.infinity,
            height: 100,
            color: Colors.pink,
          )
        ],
      ),
    );
  }
}

What is the reason for this? Even if I remove for the parent I have added expanded for the child, so shouldn't it be the same?

ReusableCard:

class ReusableCard extends StatelessWidget{
  final Color colour;
  ReusableCard({@required this.colour});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(15),
      decoration: BoxDecoration(
          color: colour,
          borderRadius: BorderRadius.circular(20)
      ),
    );
  }
}

Basically The Expanded widget will expand to all of the remaining height the parent widget have,

So you in the first code, in your body inside the column we have 4 widgets

      Expanded(
        child: Row(
        ),
      ),
      Expanded(
        child: ReusableCard(colour: Color(0xff1d1e33)),
      ),
      Expanded(
        child: Row(
        ),
      ),
      Container(
        width: double.infinity,
        height: 100,
        color: Colors.pink,
      )

So lets say that the height of the parent widget is 1000, the height of the container at the end is fixed so it will always take 100, so 900 is left

The three expanded widget in the column will take the remaining 900, and because you did not specify any flex property in the Expanded widget all of them will receive same height so 900/3 = 300 for each widget.

in the second code inside the column you have

      Row(
      ),
      Expanded(
        child: ReusableCard(colour: Color(0xff1d1e33)),
      ),
      Row(
      ),
      Container(
        width: double.infinity,
        height: 100,
        color: Colors.pink,
      )

again assuming the height available is 1000, the container is fixed height of 100 so 900 is left, now the expanded widget will take all of the available height which is 900 because the rows don't have any fixed height.

the Expanded widget inside the Rows will make the card inside them expand to the parent height, but the parent row does not have any height (0 height) so it will expand to this 0 height which is useless.

always keep it in mind that the Expanded will take the height or width from its parent widget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM