Design Converter
Education
Software Development Executive - II
Software Development Executive - II
Last updated on May 6, 2024
Last updated on Apr 24, 2024
In Flutter, layout design plays an integral role in creating an interactive user interface. One significant player in this layout design process is the Flutter Flexible class. The Flexible class provides the mechanism for controlling how a child of a Row, Column, or Flex flexes.
In other words, it enables a widget to occupy available space in the main axis (horizontally for a Row or vertically for a Column) accordingly.
That way, the UI becomes adaptable to a variety of screen sizes, leading to more responsive layouts.
The Flutter Flexible widget plays a pivotal role in controlling the size and positioning of its child widget within a flex container, like a Row, Column, or Flex. The widget, by default, does not require its child to fill up the available space, but this can be adjusted using the fit property.
Also, it's important to note that Flutter Flexible must be a descendant of Row, Column, or Flex, and the path from the Flexible widget to its enclosing Row, Column, or Flex must contain only StatelessWidgets or StatefulWidgets (not other kinds of widgets, like RenderObjectWidgets).
Each child widget is allocated a non-negative flex factor. The larger the flex factor, the more room the child widget consumes within the flex container. In the case of multiple children, the available space is distributed to them by their flex factor. It's also the value that decides the flexibility of the child widget in a Flexible widget.
Moving on, let's compare Flutter Flexible with the Expanded widget in the next section.
1void main() { 2 runApp(); 3} 4 5class MyApp extends StatelessWidget { 6 7 Widget build(BuildContext context) { 8 return Scaffold( 9 body: Row( 10 children: [ 11 Flexible( 12 flex: 1, 13 fit: FlexFit.tight, 14 child: Container( 15 color: Colors.red, 16 )), 17 Flexible( 18 flex: 1, 19 fit: FlexFit.loose, 20 child: Container( 21 color: Colors.blue, 22 )), 23 ], 24 ), 25 ); 26 } 27}
This example contains two child containers within the row. Each container has a flex value of 1, indicating equal distribution of available space between the two containers. The fit property is used to specify how the child container should adjust its width to fill its allocated space.
The first container uses 'FlexFit.tight', which forces the child to expand to fill the available space, while the second container uses 'FlexFit.loose', which doesn't require its child to expand to fill the allocated space.
Both Flutter Flexible and the expanded widget have significant roles in creating flexible and responsive layouts in Flutter applications. They are used when you want a widget or widgets to take up the remaining available space in a parent widget. However, they do differ in their usage and behavior in specific scenarios.
Expanded widgets class, an implementation of Flexible, forces its child widget to fill available space. It doesn't just allocate the remaining space; it requires its child to use all this space. It's a just basic filling of given extra space. It's useful when you want to ensure a widget (or widgets) expands to fill any remaining space in a parent widget.
On the other hand, depending on the FlexFit setting, a Flexible widget provides a free distraction to discover human stories within the interface design. In other words, it does not require the child widget to use all the remaining space.
When using a Flex or Row widget where you have multiple child widgets, the Flexible widget comes to an advantage. However, if there's only a single child widget to fill the space, the Expanded Widget is a better choice.
In the given code below, the Expanded Widget is used to distribute the available space equally amongst child widgets (containers).
1void main() { 2 runApp(); 3} 4 5class MyApp extends StatelessWidget { 6 7 Widget build(BuildContext context) { 8 return Scaffold( 9 body: Row( 10 children: <Widget>[ 11 Expanded( 12 child: Container( 13 color: Colors.red, 14 ), 15 ), 16 Expanded( 17 child: Container( 18 color: Colors.blue, 19 ), 20 ), 21 ], 22 ), 23 ); 24 } 25}
In this piece of code, two rows are formed with two containers in each. Each row is then expanded to fill the available space in the horizontal direction. The Expanded widget allows the child widget to expand and fill the allocated space.
Flexible and Expanded aren't interchangeable. The choice between Flutter Flexible and Expanded depends entirely on your layout requirements. While the Expanded widget might seem like the better widget to use because it forces the child widgets to fill up all available space, that's not always the desired effect.
Using the Flutter Flexible class is simple with just a basic understanding of how flex factors work. The higher the flex value of a widget, the more space that widget will occupy in the flexible widget, thus making your widget adjust itself to multiple device sizes.
In the following example, we implement three Flexible widgets with different flex factors in a row. The child container in each Flexible widget is assigned a color. The UI will automatically adjust the widths of containers based on the flex factors provided, which in turn creates a pleasing visual distribution of screen space.
1void main() { 2 runApp(); 3} 4 5class MyApp extends StatelessWidget { 6 7 Widget build(BuildContext context) { 8 return Scaffold( 9 body: Row( 10 children: <Widget>[ 11 Flexible( 12 flex: 2, 13 child: Container( 14 color: Colors.amber, 15 )), 16 Flexible( 17 flex: 1, 18 child: Container( 19 color: Colors.green, 20 )), 21 Flexible( 22 flex: 3, 23 child: Container( 24 color: Colors.blue, 25 )), 26 ], 27 ), 28 ); 29 } 30}
With the help of Flexible widgets, we were able to create responsive layouts that look consistent on various screen sizes. This is one of the various features that support independent authors and developers on the mission to create engaging and responsive applications.
This widget helps how much space a particular widget can take while adjusting it to the main axis direction. The combined use of flex and fit properties assist in informing the widget about filling the space as needed. Next, we'll be discussing about the flexible widgets.
In a Flutter flexible layout, flexible widgets are the children's widgets that are wrapped in a Flexible widget. Each flexible widget is allocated available space based on the proportional distribution of its flex value compared to the total flex value of all flexible widgets in the Flex container. This flexible widget plays a crucial role in developing responsive apps, where the app appearance adapts to different screen sizes (or screen orientations).
The application of flexible widgets expands beyond just Rows, Columns, or Flex layouts; flexible widgets are seen in other complex layouts too. For instance, in a ListView.builder, list items can be flexible widgets, ensuring items adapt their size and shape according to the device size.
One thing to remember, when it comes to flexible widgets, is that the child widget does not necessarily need to be a constraint-limited child, such as Container. It could be a Text Widget or an Icon, which do not have preset height or width limits. Flexible will not impose any constraints on them.
When considering creating a responsive app, it is essential to think about how it will appear on different screen sizes. By using flexible widgets, we can ensure our app will automatically adjust its layout to better fit any screen size. Let's move on to applying the Flexible widget.
1void main() { 2 runApp(); 3} 4 5class MyApp extends StatelessWidget { 6 7 Widget build(BuildContext context) { 8 return Scaffold( 9 body: Column( 10 children: <Widget>[ 11 Flexible( 12 flex: 1, 13 child: Container( 14 color: Colors.yellow, 15 )), 16 Flexible( 17 flex: 2, 18 child: Container( 19 color: Colors.green, 20 )), 21 Flexible( 22 flex: 1, 23 child: Container( 24 color: Colors.blue, 25 )), 26 ], 27 ), 28 ); 29 } 30}
In this snippet, it changes from a horizontal direction to a vertical direction. The Column widget aligns its children vertically unlike Row, which does so horizontally. This example showcases how Flexible widgets are used and function within a Column. The sum of the flex values (4) determines the proportion each container should take. The yellow and blue containers occupy one-fourth of the screen width, while the green container occupies half of the screen width.
While working with the Flutter Flexible class, understanding the role of child widgets gives a significant advantage. Each Flexible widget must contain a single child widget and how much space this child widget occupies within its parent Flexible widget depends on the settings specified in Flexible.
Primarily, the fit and flex properties govern this space allocation. The fit property of Flexible allows developers to control how the child widget should adjust its width to fill up the allocated space, where the options are either FlexFit.tight or FlexFit.loose. Notably, when FlexFit.tight is used, the child widget is forced to fill up the available space.
In case you have more than one child within a Flexible widget's parent (like a Row or Column), then the flex factor comes into play. The available space is divided proportionally among the children, based on their flex values. Child widgets with higher flex values receive a larger portion of the available space. Ultimately, this concept allows the creation of truly flexible and responsive layouts that adapt to varying screen sizes.
It's also worth noting that the child widget wrapped within Flexible can be any UI element - from Containers and Rows to even other Flexible or Expanded widgets.
1void main() { 2 runApp(); 3} 4 5class MyApp extends StatelessWidget { 6 7 Widget build(BuildContext context) { 8 return Scaffold( 9 body: Row( 10 children: [ 11 Flexible( 12 flex: 2, 13 child: Image.network( 14 'https://example.com/images/img1.png', 15 )), 16 Flexible( 17 flex: 3, 18 child: Image.network( 19 'https://example.com/images/img2.png' 20 )), 21 ], 22 ), 23 ); 24 } 25}
Here, the image serving as the child widget is wrapped in a Flexible widget. The image is being fetched from a network location. When used within the app, the images will self-adjust based on the flex values provided, ensuring that the UI remains attractive and functional, regardless of the device's screen size.
On a finishing note, Flutter Flexible and child widgets together create dynamic layouts providing not only the best member-only stories but also the thrill of creating adaptable UI designs.
In real-world applications, the use of the Flexible class is quite common and crucial for developing user interfaces that adjust according to different screen sizes. A common use case is in a news app or music player where different sections of the screen are to be divided based on the type of content to be displayed.
Let's take a classic music player UI. The entire screen space can be divided primarily into three sections, each for album cover, song controls, and playlist display. In Flutter, we can manage this layout effectively using a Column widget, with each child wrapped in a Flexible widget. By assigning appropriate flex values, we can control how much space each section can occupy.
Another example is its usage in an e-commerce app where the screen must contain product images, descriptions, reviews, and purchase actions. By using the Flexible widget, we can customize each portion's size, giving more room for the product image while giving less room for the buy button.
While working with a ListView or GridView widget, each list or grid element can be made flexible, thus providing ample room for accommodation on different devices and orientations. This way, we ensure a consistent user experience across all devices.
Understanding the Flutter Flexible class is crucial for crafting elegant, flexible, and responsive user interfaces. As we explored, the Flexible class gives us the ability to control how a child of a Row, Column, or Flex flexes in a variety of ways.
From implementing the flexible widget to using child widgets with Flexible and using the Expanded widget, it all comes down to creating interfaces that enrich user experience across multiple device sizes. This class truly grants you the freedom to build comprehensive layouts.
Continue to dive deeper into Flutter, and discover the endless possibilities and new ways to enhance your app designs.
Keep fluttering!!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.