This website will be permanently discontinued and will no longer be accessible.
FINAL DAY OF SERVICE: 26 November 2024
Thank You for Your Support
Dear valued users,
We want to inform you that this website will be permanently shutting down. After this date, all services and content will no longer be accessible.
We sincerely thank you for your support and trust throughout our journey.
Important Dates:
Last Day: 26/11/2024
Data Access:Until the last day
Dear valued users,
We want to inform you that this website will be permanently shutting down. After this date, all services and content will no longer be accessible.
We sincerely thank you for your support and trust throughout our journey. Important Dates: Last Day: 26/11/2024 Data Access:Until the last day
In this article you will learn how to use SizedBox widget in flutter.
In this Article
What is SizedBox Widget?
It’s a box widget in which we specify the size of the box.
If we give height or width parameter a null value, then it will just take the child height and width as its dimensions.
If child widget don’t have any height or width then we have to give the dimension to the SizedBox widget.
SizedBox Properties
SizedBox has four properties.
key: This is the widget key.
height: This is the height.
width: This is the width.
child: This is the child that we assign to this widget.
Where we can use SizedBox?
We can use SizedBox to give specific width and height to a specific child. SizedBox enforces the child widget to take a fix height or width that we assign.
It can be used to add Space (gapes) between two widgets.
Difference Between SizedBox and Container Widgets
Container can also be used to add space between widgets Or wrap the child and give it a specific height.
But Container has some extra properties.
So,
We can use SizedBox instead where we can specify child, height and width. And It works similarly.
Also Container has more properties so it’s a heavier widget and you can also make SizedBox widget const. Which will improve performance
How to Use SizedBox widget
1. Give Space Between Widgets
2. Change Size of the Widget
Give Space Between Widgets
As we talk earlier SizedBox is used to give space between widgets.
Snippet
const SizedBox(height: 20), //Add space Vertically
const SizedBox(width: 20), //Add Space Horizontally
Change Size of the Widget
We we give height and width property of the SizedBox. It will ignore the width and height of the child widget and give it a specific width and height.
SizedBox(
height: 20, //This become the height of the container
child: Container(
height: 100, //This height will be ignored
color: Colors.red,
),
),
If we use the double.infinity() as the height or width of the flutter SizedBox widget, then it will take the whole width or height of the parent widget.
Scaffold(
body: SizedBox(
height: double.infinity, // This will take the whole space
width: double.infinity,
child: Container(
height: 100,
color: Colors.red,
),
),
);
SizedBox Name Constructors
SizedBox has three name constructors:
1. SizedBox.expand()
2. SizedBox.fromSize()
3. SizedBox.shrink()
SizedBox.expand()
This constructor can also be used to expand the child to an infinite width and height, which will expand it to the available space.
Scaffold(
body: SizedBox.expand( //Behave just like double.infinity height and width
child: Container(
height: 100,
color: Colors.red,
),
),
);
SizedBox Inside the Column Widget
Inside Column widget, when we assign the double.infinitywidth to the SizedBox. It will take the whole available space on the screen.
But, If We assign the double.infinity to the height property of the SizedBox widget. It will not take the whole vertical space of the screen. Remember that! (Here Column BoxConstraints enforces an infinite height).
Solution
The solution is simple just use the Expanded widget to Make SizedBox to take the complete height of the screen.
Before
Column(
children: [
SizedBox(
height: double.infinity, //Inside column this will produce error
child: Container(
height: 100,
color: Colors.red,
),
),
],
),
After
Column(
children: [
Expanded( //This will solve the problem
child: SizedBox(
height: double.infinity,
child: Container(
height: 100,
color: Colors.red,
),
),
),
],
),
SizedBox.fromSize()
This is used to make a SizedBox widget from the Size() object. This constructor will take the Size() Object.