How to use Expanded in SingleChildScrollView?

CodeWithFlutter
2 Min Read

The Expanded widget can be used inside a SingleChildScrollView to provide flexible space for its child widget. The Expanded widget takes up all remaining free space within its parent widget. This way, the child widget can grow or shrink depending on the available space.

Here’s an example of using Expanded inside a SingleChildScrollView:

SingleChildScrollView(
  child: Column(
    children: [
      Container(
        height: 100,
        color: Colors.yellow,
      ),
      Expanded(
        child: Container(
          color: Colors.green,
        ),
      ),
    ],
  ),
)

In this example, the SingleChildScrollView contains a Column widget with two children. The first child is a Container with a fixed height of 100, and the second child is an Expanded widget with a green Container as its child. The Expanded widget takes up all the remaining free space within the Column widget, so that the SingleChildScrollView can scroll the green Container when the content inside it overflows.

By using Expanded, you can control how your child widgets grow or shrink based on the available space, and make sure they take up all the space they need, no matter the screen size or orientation.

Share this Article
Leave a comment