The Wayback Machine - https://web.archive.org/web/20240805230142/https://www.geeksforgeeks.org/flutter-expanded-widget/
Open In App

Flutter – Expanded Widget

Last Updated : 18 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Expanded widget in flutter comes in handy when we want a child widget or children widgets to take all the available space along the main-axis (for Row the main axis is horizontal & vertical for Column).  Expanded widget can be taken as the child of Row, Column, and Flex. And in case if we don’t want to give equal spaces to our children widgets we can distribute the available space as our will using flex factor. Expanded widget is similar to the Flexible widget in flutter, with its fit property set to FlexFit.tight as default. Expanded widget is basically a shorthand of Flexible widget. But if you are planning to build responsive apps or web apps, then you should definitely switch to Flexible to get more fit options.

Constructor of Expanded class:

const Expanded(
{Key? key,
int flex: 1,
required Widget child}
)


Properties of Expanded Widget:

  • child: This property sets the widget tree to be placed inside the Expanded widget. Expanded widget can be taken as the child of Row, Column, and Flex.
  • debugTypicalAncestorWidgetClass: This property takes Type as a parameter to set the ParentData for error messages.
  • fit: This property controls how the child widget fills the available space. There are two options given by flutter, the first is FlexFit.tight which sets the child to fill the space available to it and the second is FlexFit.loose which allows the child widget to be as large as the available space.
  • flex: If we decide to distribute the available space unevenly among the children widgets then we use the flex factor to do the same.

Example 1: In this example, Expanded widget used inside a Column, and below it is the app in which Expanded widget is not used, to show the difference in UI.

Dart




import 'package:flutter/material.dart';
 
void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
          title: Text('Geeksforgeeks'),
          backgroundColor: Colors.greenAccent[400],
          leading: IconButton(
            icon: Icon(Icons.menu),
            tooltip: 'Menu',
            onPressed: () {},
          )),
      body: Center(
          child: Column(
        children: <Widget>[
          Container(
            child: Center(
              child: Text(
                'First widget',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
            ),
            color: Colors.blue,
            height: 100,
            width: 200,
          ),
          Expanded(
            child: Container(
              child: Center(
                child: Text(
                  'Second widget',
                  style: TextStyle(
                    color: Colors.white,
                  ),
                ),
              ),
              color: Colors.amber,
              width: 200,
            ),
          ),
          Container(
            child: Center(
              child: Text(
                'Third widget',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
            ),
            color: Colors.orange,
            height: 100,
            width: 200,
          ),
        ],
      )),
    ),
    debugShowCheckedModeBanner: false,
  ));
}


Output:

Explanation: In this app first we have created a simple app bar using the AppBar widget with a leading menu IconButton. The background color of the app bar is set to greenAccent[400] and the title is a Text widget holding ‘Geeksforgeeks‘ as the text. The parent widget in the body of the app is Center with its three children widget being Container. Each of the Containers has Text as their child specifying their names. First and third containers have been assigned a height of 100 and a width of 200 with the background color being blue and pink respectively. The Second Container is a child widget to Expanded, which gives it the ability to take all the available space between the first and the second Container widgets. Similar to the first and third Containers this also has a width of 200. The background color for this is teal. And one thing to notice is we haven’t specified height for this container as it will take all the space available to it, and even if we specify a height to it, that will be overwritten by the Expanded widget.

Output:

Below is the code snippet of the second Container.

...
Container(
child: Center(
child: Text(
'Second widget',
style: TextStyle(
color: Colors.white,
),
),
),
color: Colors.amber,
width: 200,
height: 100,
),
...


This is how UI will look if we do not use the Expanded widget for the second Container in the Column. Note that we need to specify height in this case.

expanded widget

Example 2: In this example, the Expanded widget is used as a child to Row. And next to it is the app without Expanded widget, to show the difference in the UI.

Dart




import 'package:flutter/material.dart';
 
void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
          title: Text('Geeksforgeeks'),
          backgroundColor: Colors.greenAccent[400],
          leading: IconButton(
            icon: Icon(Icons.menu),
            tooltip: 'Menu',
            onPressed: () {},
          )),
      body: Center(
          child: Row(
        children: <Widget>[
          Container(
            child: Center(
              child: Text(
                'First widget',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
            ),
            color: Colors.blue,
            height: 200,
            width: 100,
          ),
          Expanded(
            child: Container(
              child: Center(
                child: Text(
                  'Second widget',
                  style: TextStyle(
                    color: Colors.white,
                  ),
                ),
              ),
              color: Colors.amber,
              height: 200,
            ),
          ),
          Container(
            child: Center(
              child: Text(
                'Third widget',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
            ),
            color: Colors.pink,
            height: 200,
            width: 100,
          ),
        ],
      )),
    ),
    debugShowCheckedModeBanner: false,
  ));
}


Output:

expanded vertical widget

Explanation: This app is also similar to the app in the first example except for the part the parent widget in the app body is Row instead of Column, with each Container having a height of 200. The width assigned to the first and third Containers is 100. The second Container in this app is a child to Expanded widget, which gives it the ability to take all the space between the first and the third Containers. Again we have not set any width to the second container as it won’t count.

Output:

This is the code snippet of the second container.

...
Container(
child: Center(
child: Text(
'Second widget',
style: TextStyle(
color: Colors.white,
),
),
),
color: Colors.amber,
width: 100,
height: 200,
),
...


This is how the user interface chance if we remove the Expanded widget from being the parent of the second Container. Here also we have explicitly specified the width of the second Container.

expanded left alignment widget



Similar Reads

Expanded Class in Flutter
When we create any child of a row or column we provide the size of the widget according to the screen size but sometimes when we provide more size of child as compared to screen size we get a warning and our widget goes out of the screen for resolving this we put a child of a row or column in an expanded widget so that the child occupies only the a
5 min read
Expandable Widget in Flutter
The expandable widgets do not have a fixed size, they expand on the screen according to the available areas on the screen. Due to size constraints, certain widgets can throw rendering errors, so widgets are made expandable. The one use of expandable widgets is cards that include images, texts, and to further read the text we can click "Read More" o
3 min read
Flutter - Card Widget
Card is a build-in widget in flutter which derives its design from Google's Material Design Library. The functionality of this widget on screen is, that it is a bland space or panel with round corners and a slight elevation on the lower side. It comes with many properties like color, shape, shadow color, etc which lets developers customize it the w
4 min read
Flutter - ColoredBox Class Widget
A Colored Box is a container that fills it with color according to its child or A widget that paints its area with a specified Color and then draws its child on top of that color. A sample image is given below to get an idea about what we are going to do in this article. How to use it? You can simple use this widget anywhere in your project as you
3 min read
How to Get the Height of a Widget in Flutter?
To get the height of a widget in Flutter, you can use the LayoutBuilder widget to obtain the constraints of the parent container and then get the height from those constraints. How to use it? You can simply use this widget anywhere in your project as you needed that. LayoutBuilder( builder: (BuildContext context, BoxConstraints constraints) { doubl
3 min read
Difference Between Stateless and Stateful Widget in Flutter
As we all know the flutter app consists of widgets only, these are broadly classified into two types: Stateless WidgetStateful WidgetState: Before knowing the difference between the stateless and stateful widget we should take a look at the definition of State of a widget. The State is information that can read synchronously when the widget is buil
3 min read
Flutter - Wrap Widget
Wrap widget aligns the widgets in a horizontal and vertical manner. Generally, we use Rows and Columns to do that but if we have some widgets which are not able to fit in the Row/Column then it will give us Overflow Message ( for ex: Right Overflowed by 570 pixels). Constructor of Wrap class:Wrap({Key key, Axis direction, WrapAlignment alignment, d
3 min read
Raised Button widget in Flutter
RaisedButton is the material design button based on a Material widget that elevates when pressed upon in flutter. It is one of the most widely used buttons in the flutter library. Let's understand this button with the help of an example. Disclaimer: As of May 2021 the RaisedButton class in flutter is deprecated. ElevatedButton class should be used
5 min read
OffStage Widget in Flutter
Flutter provides an inbuilt widget called “Offstage”, which is been used to hide or show any widget programmatically depending on user action/event. Offstage Widget is a widget that lays the child out as if it was in the tree, but without painting anything, without making the child available for hit-testing, and without taking any room in the paren
4 min read
NotificationListener Widget in Flutter
In Flutter, every Scrollable sends Notifications that contain information about the current scroll state. So to catch these notifications we use NotificationListener Widget. NotificationListener Widget will listen for Notifications bubbling up the tree. In this, article we will learn about NotificationListener Widget Default Constructor:Notificatio
4 min read
Article Tags :
three90RightbarBannerImg