Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply flex in flutter

I am a beginner in a flutter, and I have learned how to handle the sizing and text rendering when the screen sizing changes from desktop to tablet to mobile. But I want to understand how can I change the sizing or flex the content when I am decreasing the screen size within the same screen mode.

For Instance -

return Container(
  child: new Row(
    children: <Widget>[
      new Column(
        children: <Widget>[new Text("Hello World")],
      ),
      new Column(
        children: <Widget>[
          new Text(
              "This is a long text this is a long test this is This is a long text this is a long test this is This is a long text this is a long test this is This is a long text this is a long test this is This is a long text this is a long test this is This is a long text this is a long test this is ")
        ],
      )
    ],
  ),
);

With this scenario when I try to decrease the screen size from desktop to table I start getting oveflow exception. Please guide me on how to handle it.

like image 908
Unbreakable Avatar asked Feb 24 '20 16:02

Unbreakable


People also ask

What is difference between expanded and flexible in flutter?

Flexible widget controls its child's flexibility to expand and fill the available space in the main axis of a Row or a Column, the difference between Expanded widget and Flexible widget is, Flexible does not require the child to fill the available space. let's check out the code to understand better…


1 Answers

By using Expanded and setting the flex property, we can give each widget in a Row or Column its own weight. That means how much of the available space of that widget it is allowed to fill. So we can have the following code for a horizontal alignment:

By default, Expanded takes the whole available space, so if we want 2 widgets to take 50% of the space, we can remove the flex property altogether.

Here's simple code example to understand:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Expanded"),
      ),
      body: Container(
        child: Row(
          children: <Widget>[
            Expanded(
              flex: 6, // 60% of space => (6/(6 + 4))
              child: Container(
                color: Colors.red,
              ),
            ),
            Expanded(
              flex: 4, // 40% of space
              child: Container(
                color: Colors.purple,
              ),
            ),
          ],
        ),
      ),
    );
  }

enter image description here

like image 142
Paresh Mangukiya Avatar answered Sep 21 '22 09:09

Paresh Mangukiya