Expanded widget of flutter:
Expanded widget can be used with a Row, Column or Flex widget. It is used to expand the childs to fill all available spaces. One thing you should note that this widget should be a descendent of Row, Column or Flex. We can also provide one flex property to these elements to change the expanded space.
Expanded with a Column:
Let’s try with Column first. For the below example widget,
class DemoPage extends StatelessWidget {
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text('Expanded Demo'),
),
body: Center(
child: Column(
children: [
Container(
color: Colors.cyan,
width: 200,
height: 200,
),
Container(
color: Colors.red,
width: 200,
height: 200,
),
Container(
color: Colors.green,
width: 200,
height: 200,
)
],
),
));
}
}
It will give the below output:
Now, if I use Expanded for the first and the last Container,
child: Column(
children: [
Expanded(
child: Container(
color: Colors.cyan,
width: 200,
)),
Container(
color: Colors.red,
width: 200,
height: 200,
),
Expanded(
child: Container(
color: Colors.green,
width: 200,
))
],
),
We can remove the height for Expanded and it will expand both to fill the remaining space:
Expanded with Row:
We can also use it with Row:
body: Center(
child: Row(
children: [
Expanded(
child: Container(
color: Colors.cyan,
height: 200,
)),
Container(
color: Colors.red,
width: 20,
height: 200,
),
Expanded(
child: Container(
color: Colors.green,
height: 200,
))
],
),
));
Expanded with flex:
We can use flex with Expanded to change the size relatively:
body: Center(
child: Row(
children: [
Expanded(
flex: 1,
child: Container(
color: Colors.cyan,
height: 200,
)),
Container(
color: Colors.red,
width: 20,
height: 200,
),
Expanded(
flex: 3,
child: Container(
color: Colors.green,
height: 200,
))
],
),
));
This will result the below output:
You might also like:
- How to customize the textfield cursor in Flutter
- Flutter TextField how to change the maximum and minimum lines
- How to remove the debug banner in Flutter apps in development mode
- Changing the size of a floating action button in Flutter
- Flutter AppBar widget and its properties
- How to change the colors of a Floating action button in Flutter