Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Types
  • Functions
  • Oops
  • Collections
  • Sets
  • Dart Interview Questions
  • Fluter
  • Android
  • Kotlin
  • Kotlin Android
  • Android with Java
  • Android Studio
Open In App
Next Article:
Flutter - BoxConstraints Widget
Next article icon

Flutter - ConstrainedBox Widget

Last Updated : 26 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

ConstrainedBox is a built-in widget in flutter SDK. Its function is to add size constraints to its child widgets. It comes quite handy if we want a container or image to not exceed a certain height and width. It is also good to keep text in a wrapped layout by making the Text widget a child on ConstrainedBox. This functionality can also be found in SizedBox widget or else.

Constructor of ConstrainedBox Class:

ConstrainedBox( {Key key, @required BoxConstraints constraints, Widget child} )

Property of ConstrainedBox Widget:

  • constraints: This property takes in the BoxConstrain Class as the object. It puts constraints it's child widget using the functions of the BoxConstraints class.

Example 1: 

Dart
import 'package:flutter/material.dart';  //imported googles material design library void main() {   runApp(     /**Our App Widget Tree Starts Here**/     MaterialApp(       home: Scaffold(         appBar: AppBar(           title: Text('GeeksforGeeks'),           backgroundColor: Colors.greenAccent[400],           centerTitle: true,         ), //AppBar         body: Center(           /** ConstrainedBox Widget **/           child: ConstrainedBox(             constraints: BoxConstraints.expand(height: 200, width: 200),             child: Container(               color: Colors.green,             ), //Container widget           ), //ConstrainedBox         ), //Center       ), //Scaffold     ), //MaterialApp   ); } 

Output:

Explanation: In this flutter application we can see that the ConstrainedBox Widget is used to constrain the height and width of its child widget which is a Container. Here we have used BoxConstraints.expanded with the height and width parameter set to 200. We can notice that the height and width parameters are not mentioned in the Container widget it just expands to fill its parent widget.

Example 2:

Dart
import 'package:flutter/material.dart';  //imported googles material design library void main() {   runApp(     /**Our App Widget Tree Starts Here**/     MaterialApp(       home: Scaffold(         appBar: AppBar(           title: Text('GeeksforGeeks'),           backgroundColor: Colors.greenAccent[400],           centerTitle: true,         ), //AppBar         body: Center(           /** ConstrainedBox Widget **/           child: ConstrainedBox(             constraints: BoxConstraints.expand(height: 200, width: 200),             child: Text(               'A Computer Science portal for geeks. It contains well               written,well thought and well explained computer science               and programmingarticles, quizzes, interview experiences               and much more.',               style: TextStyle(fontSize: 15),             ), //Text           ), //ConstrainedBox         ), //Center       ), //Scaffold     ), //MaterialApp   ); } 

Output:

Explanation: In this flutter app the ConstrainedBox widget is used to wrap the text in a 200 X 200 box. Here also the text expands to fill the box as BoxConstraints.expand is used. This application of ConstrainedBox widget is very helpful when we want our text to not spill out of a certain bounded area.


Next Article
Flutter - BoxConstraints Widget
author
ankit_kumar_
Improve
Article Tags :
  • Dart
  • Flutter
  • Flutter-Widgets

Similar Reads

  • Flutter - BoxConstraints Widget
    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
    5 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 simpl
    3 min read
  • Flutter - Checkbox Widget
    Checkbox in Flutter is a material design widget. It is always used in the Stateful Widget as it does not maintain its own state. We can use its onChanged property to interact with or modify other widgets in the Flutter app. Like most of the other flutter widgets, it also comes with many properties l
    4 min read
  • Flutter - AnimatedContainer Widget
    In Flutter a container is a simple widget with well-defined properties like height, width, and color, etc. The AnimatedContainer widget is a simple container widget with animations. These types of widgets can be animated by altering the values of their properties which are the same as the Container
    3 min read
  • Flutter - Custom Widgets
    We create Custom Widgets when we want a custom look and feel to our app, and we know that there will be a repetition of a particular widget. We can create the custom widget in a new dart file with all the codes and defining the parameters that we need in the constructor. For more on how to split wid
    8 min read
  • Flutter - Card Widget
    Card is a built-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 c
    4 min read
  • Flutter - BoxDecoration Widget
    BoxDecoration is a build-in widget in flutter API. At a bare basic level, it describes how a box should be painted on the screen. The shape of the box needs not to be just a rectangle or a square it can circle also. It comes with a ton of properties we can add an image inside, add a radius to the bo
    3 min read
  • Flutter - Expanded Widget
    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
    5 min read
  • Center widget in Flutter
    Center widget comes built-in with flutter, it aligns its child widget to the center of the available space on the screen. The size of this widget will be as big as possible if the widthFactor and heightFactor properties are set to null and the dimensions are constrained. And in case the dimensions a
    2 min read
  • Flutter - Border Widget
    Border widget in flutter is assigned a simple functionality to add borders to the other widgets. The first is by creating all borders using BorderSide. The second way is by using Border.all to create a uniform border having the same color and width. The third is by using Border.fromBorderSide to cre
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences