How to make a widget fill remaining space in a Column

FlutterDartFlutter Layout

Flutter Problem Overview


In Android, we can do the following to make ImageView to fill as much space as possible depending on the size of the TextView.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

How do we achieve this in Flutter? Assume I need to make Datetime(green) to fill as much as possible.

new Column(
	children: <Widget>[
		new Text('Title',
			style: new TextStyle(fontWeight: FontWeight.bold)
		),
		new Text('Datetime',
			style: new TextStyle(color: Colors.grey)
		),
	],
)

enter image description here

Flutter Solutions


Solution 1 - Flutter

Not 100% sure but I think you mean this. The Expanded widget expands to the space it can use. Althought I think it behaves a bit differently in a Row or Column.

new Column(
children: <Widget>[
    new Text('Title',
        style: new TextStyle(fontWeight: FontWeight.bold)
    ),
    new Expanded(
        child: new Text('Datetime',
             style: new TextStyle(color: Colors.grey)
        ),
    ),
],
)

Solution 2 - Flutter

You can use:

  1. Expanded with Align to position child

     Column(
       children: [
         FlutterLogo(),
         Expanded( // add this
           child: Align(
             alignment: Alignment.bottomCenter,
             child: FlutterLogo(),
           ),
         ),
       ],
     )
    
  2. Spacer

     Column(
       children: [
         FlutterLogo(),
         Spacer(), // add this
         FlutterLogo(),
       ],
     )
    
  3. mainAxisAlignment

     Column(
       mainAxisAlignment: MainAxisAlignment.spaceBetween, // add this
       children: [
         FlutterLogo(colors: Colors.orange),
         FlutterLogo(colors: Colors.blue),
       ],
     )
    

Solution 3 - Flutter

If you need just to add blank space, use Spacer()

new Column(
children: <Widget>[
    new Text('Title',
        style: new TextStyle(fontWeight: FontWeight.bold)
    ),
    new Text('Datetime',
        style: new TextStyle(color: Colors.grey)
    ),
    Spacer(),
],

Solution 4 - Flutter

Flexible(
  child: Column(
    mainAxisAlignment:mainAxisAlignment.spacebetween,
    children: <Widget>[
        new Text('Title',
            style: new TextStyle(fontWeight: FontWeight.bold)
        ),
        new Text('Datetime',
            style: new TextStyle(color: Colors.grey)
        ),
  ],
)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGusterView Question on Stackoverflow
Solution 1 - FlutterKevin WalterView Answer on Stackoverflow
Solution 2 - FlutterCopsOnRoadView Answer on Stackoverflow
Solution 3 - FlutterVadim OsovskyView Answer on Stackoverflow
Solution 4 - FlutterAhmad AbdullahView Answer on Stackoverflow