Flutter: Make a Widget Fill Remaining Space of Row/Column

Last updated on August 17, 2021 Guest Contributor Loading... Post a comment

In Flutter, you can make a widget fill up the remaining space of a Row or Column by wrapping it inside an Expanded or a Flexible widget.

Example

Preview:

In this example, the amber box will occupy all of the free space of its parent row. Other boxes have fixed size.

The Code:

// main.dart
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        // Remove the debug banner
        debugShowCheckedModeBanner: false,
        title: 'Kindacode.com',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: HomePage());
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Kindacode.com'),
      ),
      body: Row(
        children: [
          Container(
            width: 50,
            height: 300,
            color: Colors.red,
            child: Center(
              child: Text('50'),
            ),
          ),
          Container(
            width: 80,
            height: 300,
            color: Colors.orange,
            child: Center(
              child: Text('80'),
            ),
          ),
          Container(
            width: 60,
            height: 300,
            color: Colors.pink,
            child: Center(
              child: Text('60'),
            ),
          ),
          Flexible(
            child: Container(
              height: 300,
              color: Colors.amber,
              child: Center(
                child: Text('Fill'),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

Another Example

Preview:

This example contains a column with some boxes inside it. The orange box will occupy the free space of its parent.

The Code:

// main.dart
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        // Remove the debug banner
        debugShowCheckedModeBanner: false,
        title: 'Kindacode.com',
        home: HomePage());
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Kindacode.com'),
      ),
      body: Column(
        children: [
          Container(
            width: double.infinity,
            height: 100,
            color: Colors.greenAccent,
            child: Center(
              child: Text('100'),
            ),
          ),
          Container(
            width: double.infinity,
            height: 150,
            color: Colors.red,
            child: Center(
              child: Text('150'),
            ),
          ),
          Flexible(
            child: Container(
              width: double.infinity,
              color: Colors.orange,
              child: Center(
                child: Text('Fill'),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

What’s Next?

Continue learning more new and interesting things about Flutter by taking a look at the following articles:

You can also check out our Flutter category page or Dart category page for the latest tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You May Also Like