Apply Spacing and Dividers Between Widgets in Flutter
Consistent spacing in Flutter UIs is crucial. Manually adding spacers is tedious. This post introduces a utility class to simplify the process and save time.
When building UI with Flutter, one key aspect of a clean and readable layout is the consistent spacing between widgets. Manually adding spacers or dividers can be repetitive and time-consuming. In this post, I will be sharing a utility class that simplifies this process, saving you valuable time.
The SeparatedList
class allows us to apply spacing to rows and columns easily. Copy the code, and paste it inside your project.
Horizontal Spacing with SeparatedList
For a row of widgets that require equal horizontal spacing, SeparatedList.horizontal
can be used:
This wraps the provided children in a Row
widget, inserting a SizedBox
with the specified width between each child.
Vertical Spacing with SeparatedList
For columnar layouts, SeparatedList.vertical
provides a similar functionality but with vertical spacing:
Each child in the Column
will have a SizedBox
with the specified height placed between them.
Custom Separators with SeparatedList
You can also pass a custom widget as a separator with SeparatedList.custom
:
Conclusion
Instead of manually adding spacing widgets after each child SeparatedList
automatically handles this. It provides a clear and concise way to define consistent spacing between widgets in a Row
or Column
, which can be especially useful in dynamic layouts where widgets may be conditionally included.
By Encapsulating the spacing logic within a reusable utility class, we can improve code cleanliness and maintainability. I hope this will save you time while you implement your layouts.