avatarHamza Mateen

Summary

The article provides solutions to handle overflow issues in Flutter development, both vertically and horizontally, using SingleChildScrollView and Flexible widgets, respectively, to improve user experience.

Abstract

The article addresses a common issue faced by developers new to Flutter, known as the overflow problem, where content exceeds the screen size. The author recounts spending two hours resolving this issue in the past, emphasizing the importance of knowing multiple solutions. The first case presented is the bottom overflow, typically occurring when a Column widget's content is too long for the screen. The author suggests using a SingleChildScrollView to make the content scrollable, enhancing the user interface. The second case covers horizontal overflow within a Row widget, where the author introduces the Flexible widget to prevent overflow by adapting the content to the available space. The article also discusses the flex property to allocate different space proportions to children within a Row or Column. The author encourages readers to experiment with these solutions and shares a personal request for support through sharing and clapping for the article.

Opinions

  • The author believes that handling overflow is a rite of passage for new Flutter developers.
  • Overflow issues are seen as detrimental to user experience if not addressed properly.
  • The use of SingleChildScrollView for vertical overflow is considered a simple and effective solution.
  • The Flexible widget is highly recommended for managing horizontal overflow, ensuring a neat display of content.
  • The author values community support and suggests a cost-effective AI service, ZAI.chat, as a resource for developers.

Flutter: Solving the Overflow Problem

So you are new to Flutter? You must be new to the overflow problem as well ;)

It comes to all of us and it must be solved. I remember investing two hours of my time trying to investigate the problem and solve it. Now, I know how to solve it in more than one way.

So, without further delay, let’s get to it.

Overflow: What does it look like to the developer?

Figure 1: Overflow demo with multiple ListTile(s) in a single Column

Case # 1:

Bottom Overflow:

In the image above(figure 1), inside a Column widget, I simply repeated the tile as many times so that it requires more screen size and creates an overflow problem for me. As a developer, I can see the black and yellow striped overflow indicator with information that with how many pixels the bottom is being overflowed; 1408 in this case. As a user, I won’t be able to see this striped box resulting in a bad user experience; unable to scroll, missing hidden widgets and thus realizing how bad a job the developer did.

Figure 2 contains code for generating the overflow in figure 1.

Figure 2: Code for generating overflow

Solution:

This case is a simple one. We are going to simply wrap the Column widget with another widget named SingleChildScrollView which allows the user to scroll through the contents of its child if they take more than the screen size.

Figure 3: Solution to the simple overflow problem

Let’s run the above code and see the results for ourselves.

Figure 4: After adding SingleChildScrollView

Looks nice! Doesn’t it? Go ahead and test it for yourself and try different variations.

Case # 2:

Horizontal Overflow:

We were able to make the screen scrollable when it came to vertical overflow. Can we do the same for horizontal overflow? Um, yeah but it would look ugly.

Here, I created a horizontal overflow by putting too much content inside a Row widget. See figures 5 and 6.

Figure 5: Code to create horizontal overflow inside a Row widget
Figure 6: Visual representation of horizontal overflow

Introducing Flexible:

Flexible widget can be placed inside Column, Row and similar widgets to surround any of their children. Flexible helps the child to avoid overflow by displaying it only in the available space. In figure 7, you can see that I wrapped the first child of the Row and in figure 8, you can see that it solved our problem (for now).

Figure 7: Flexible widget to avoid overflow
Figure 8: Visual representation of code in figure 7

Now we add another similar-looking child inside our Row widget and see what happens. Refer to figure 9 where I added another child to test if the flexibility holds.

Figure 9: Adding one more child
Figure 10: Overflow after adding a fourth child

As shown in figure 10 above, flexibility isn’t holding now. The simple solution to that is we wrap each of our children in the same Flexible widget.

Figure 11: Wrapping all children in Flexible widget
Figure 12: Solution to overflow

Figure 12 displays how each child inside Row took equal space available. This shows us that Flexible widget is really helpful when we have to display our data in a horizontal format and need it displayed neatly.

Introducing the flex property:

For now, each child of our Row widget was taking equal space. What if we want one of the children to take up more space than the others? Worry not! Flexible comes with a flex property. By default, it is 1 i.e. each child will take one part of the total partition. Let’s change it to 2 for the first child and see what happens. Refer to figure 13 below for code.

Figure 13: Adding the flex property
Figure 14: After adding flex property

We see in figure 14 above that now the first child is taking more space than others i.e. there were four children but now there are five partitions of space and the first one is taking two parts of it. See figure 15 for a visual explanation of the discussed scenario.

If it helped you, do support me by sharing this in your circle and leaving some claps 😊

Flutter
Flutter Widget
Flexible
UI Design
Flutter App Development
Recommended from ReadMedium