Flutter - BoxConstraints Widget
Last Updated :
26 Sep, 2022
BoxConstraints is a built-in widget in flutter SDK. Its functionality is to add sized constraints on its child widget. It is usually taken as the object of constraints property in ConstrainedBox widget. It comes packed with many properties for customization. Below we will see all its properties with and an example,
Constructor of BoxConstraints:
It paints a box with the mentioned constraints.
const BoxConstraints(
{double minWidth: 0.0,
double maxWidth: double.infinity,
double minHeight: 0.0,
double maxHeight: double.infinity}
)
Constructor of BoxConstraints.expand :
It paints a box which expands to fill its parent BoxConstraints widget.
const BoxConstraints.expand(
{double? width,
double? height}
)
Constructor of BoxConstraints.loose:
It created a box which does not grow beyond the mentioned size.
BoxConstraints.loose(
Size size
)
Constructor of BoxConstraints.tight:
It will create a box which does not change its size.
BoxConstraints.tight(
Size size
)
Constructor of BoxConstraints.tightfor:
It will paint a box on the screen with by just mentioning its height and width.
const BoxConstraints.tightFor(
{double? width,
double? height}
)
Constructor of BoxConstraints.tightForFinite:
Here we get a box whose height and width is set to infinity if not mentioned.
const BoxConstraints.tightForFinite(
{double width: double.infinity,
double height: double.infinity}
)
Properties of BoxConstraints widget:
- biggest: This property takes in Size class as the object to specify the biggest the box can get.
- flipped: The flipped property takes in BoxConstraints class as the object to flip the height and width properties of the widget.
- hasBoundedHeight: This property takes in a boolean as the object to specify whether the maxHeight had an upper limit.
- hasBoundedWidth: This property takes in a boolean as the object to specify whether the maxWidth had an upper limit.
- hasInfiniteHeight: This property also takes in a boolean as the object to determine whether the box has infinite height.
- hasInfiniteWidth: This property also takes in a boolean as the object to determine whether the box has infinite width.
- hasTightHeight: This property determines whether the box height will be fixed to only one value or not by taking in a boolean as the object.
- hasTightWidth: This property determines whether the box width will be fixed to only one value or not by taking in a boolean as the object
- isNormalized: It also takes in a boolean value as the object to specify if the maximum and minimum height and width will be same ort not.
- isTight: The isTight property also takes in a boolean as the object to determine whether the box constraints will be fixed to only one height and width or not.
- maxHeight: This property takes in a double value as the object to control the maximum height the box can get to.
- maxWith: This property takes in a double value as the object to control the maximum width box will get to.
- minHeight: This property takes in a double value as the object to control the minimum height box will get to.
- minWidth: This property takes in a double value as the object to control the minimum width box will get to.
- smallest: This property controls the smallest size the box can achieve by taking in Size class as the object.
Example 1:
Dart
import 'package:flutter/material.dart';
//Material design library
void main() {
runApp(
//widget tree starts here
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('GeeksforGeeks'),
backgroundColor: Colors.greenAccent[400],
centerTitle: true,
), //AppBar
body: Center(
child: Container(
color: Colors.green,
padding: EdgeInsets.all(20),
child: Text(
'GfG',
style: TextStyle(color: Colors.white, fontSize: 20),
), //Text
/** BoxConstraints Widget **/
constraints: BoxConstraints(
minHeight: 50,
minWidth: 50,
maxHeight: 80,
maxWidth: 80), //BoxConstraints
), //container
), //Center
), //Scaffold
), //MaterialApp
);
}
Output:

Explanation: Inside the Container widget which is a child of Center widget (Parent widget in the body) the constraints property is holding BoxConstraints widget. Inside that the minHeight and minWidth are set to 50 px and maxHeight and maxWidth is set to 80 px. The Container is green colored and is holding white colored text. The constraints that are set will not allow the container to have a height and width greater than 80 px and less than 50 px.
Example 2:
Dart
import 'package:flutter/material.dart';
//Material design library
void main() {
runApp(
//widget tree starts here
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('GeeksforGeeks'),
backgroundColor: Colors.greenAccent[400],
centerTitle: true,
), //AppBar
body: Center(
child: Container(
color: Colors.green,
padding: EdgeInsets.all(20),
/** BoxConstraints Widget **/
constraints: BoxConstraints(
minHeight: 50,
minWidth: 50,
maxHeight: 80,
maxWidth: 80), //BoxConstraints
child: Container(
color: Colors.greenAccent[400],
child: Text(
'GfG',
style: TextStyle(color: Colors.white, fontSize: 20),
), //Text
constraints: BoxConstraints.expand(height: 50, width: 50),
)), //container
), //Center
), //Scaffold
), //MaterialApp
);
}
Output:

Explanation: This flutter app is similar to the previous one, except the fact the child widget in the Container which was a Text is replaced by another Container widget. The second container is assigned a color of greenAccent[400], and the child is a Text widget. The constraints property in the second container is taking BoxConstraints.expand as the object, with its height and width property set to 50 px each. The BoxConstraints.expands is expanding in its parent BoxConstraints to a height and width of 50 px. And because of the fact it expands, it always starts from the center.
Similar Reads
Architecture of 8085 microprocessor A microprocessor is fabricated on a single integrated circuit (IC) or chip that is used as a central processing unit (CPU).The 8085 microprocessor is an 8-bit microprocessor that was developed by Intel in the mid-1970s. It was widely used in the early days of personal computing and was a popular cho
11 min read
Android Architecture Android architecture contains a different number of components to support any Android device's needs. Android software contains an open-source Linux Kernel having a collection of a number of C/C++ libraries which are exposed through application framework services. Among all the components Linux Kern
5 min read
States of a Process in Operating Systems In an operating system, a process is a program that is being executed. During its execution, a process goes through different states. Understanding these states helps us see how the operating system manages processes, ensuring that the computer runs efficiently. Please refer Process in Operating Sys
11 min read
Flutter Tutorial This Flutter Tutorial is specifically designed for beginners and experienced professionals. It covers both the basics and advanced concepts of the Flutter framework.Flutter is Googleâs mobile SDK that builds native Android and iOS apps from a single codebase. It was developed in December 2017. When
7 min read
Android Tutorial In this Android Tutorial, we cover both basic and advanced concepts. So whether you are a fresher (graduate) or an experienced candidate with several years of Android Development experience, you can follow this Android tutorial to kick-start your journey in Android app development. Our Android Tutor
15+ min read
Dart Tutorial Dart is an open-source general-purpose programming language developed by Google. It supports application development on both the client and server side. However, it is widely used for the development of Android apps, iOS apps, IoT(Internet of Things), and web applications using the Flutter Framework
7 min read
Activity Lifecycle in Android with Demo App In Android, an activity is referred to as one screen in an application. It is very similar to a single window of any desktop application. An Android app consists of one or more screens or activities. Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when
9 min read
Introduction to Android Development Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
5 min read
Android UI Layouts Layouts in Android define the user interface and hold UI controls or widgets that appear on the screen of an application. Every Android application consists of View and ViewGroup elements. Since an application contains multiple activitiesâeach representing a separate screenâevery activity has multip
5 min read
Top 50 Android Interview Questions and Answers - SDE I to SDE III A Linux-based open-source OS, Android was created by Andy Rubin and became one of the most popular smartphone operating systems. With 71 percent of the market share worldwide, Android is on top. Because it is on top in the smartphone OS, Android development is always in demand.If you are seeking a j
15+ min read