Flutter

Flutter align two items on extremes - one on the left and one on the right

27 September 2026 · 9 min read

Flutter align two items on extremes - one on the left and one on the right

Creating responsive and visually appealing user interfaces is a cornerstone of modern app development, and Flutter, Google’s UI toolkit, provides powerful tools to achieve just that. One common design challenge is arranging elements on the screen with specific alignment requirements. This article will guide you through the process of using Flutter to align two items on extremes, specifically positioning one item on the left edge and the other on the right edge of the screen. We’ll explore different approaches, including using Row, Expanded, and MainAxisAlignment, to achieve this layout. Mastering this technique allows developers to build more dynamic and balanced UIs, improving the overall user experience. By understanding these fundamental layout principles, you can unlock greater flexibility in your Flutter app designs. This skill is invaluable when creating navigation bars, headers, or any other UI component where balanced placement is crucial.

Understanding Flutter’s Layout System

Flutter’s layout system is based on widgets. Everything you see on the screen is a widget, and these widgets are arranged in a tree-like structure. Understanding how these widgets interact is crucial for effective UI design. Flutter offers several layout widgets, such as Row, Column, Stack, and Container, which provide the building blocks for organizing and positioning child widgets. The Row widget, in particular, is essential for arranging widgets horizontally. To align two items on extremes within a Row, you need to understand how MainAxisAlignment works. This property controls how widgets are positioned along the main axis of the Row, which is horizontal in this case. Options like MainAxisAlignment.spaceBetween, MainAxisAlignment.start, MainAxisAlignment.end, and MainAxisAlignment.center provide different alignment behaviors.

The Expanded widget is another powerful tool for controlling the size and positioning of widgets within a Row or Column. When a widget is wrapped in an Expanded widget, it takes up all the available space along the main axis. This can be used to push other widgets to the edges of the screen. For example, placing an Expanded widget between two widgets in a Row will force the first widget to the left and the second widget to the right. This approach is particularly useful when you want to dynamically adjust the spacing between elements based on screen size or content.

Understanding the interplay between these layout widgets is key to creating responsive and adaptable UIs in Flutter. By combining Row, Expanded, and MainAxisAlignment, you can achieve a wide range of layout configurations, including the specific goal of aligning two items on extremes. Remember to consider the overall structure of your UI and how different elements contribute to the user experience. By mastering these techniques, you can create visually appealing and functional Flutter applications.

Using Row and MainAxisAlignment to Align Items

The most straightforward way to align two items on extremes in Flutter is by using the Row widget in conjunction with the MainAxisAlignment.spaceBetween property. This property automatically distributes the available space evenly between the child widgets, pushing the first widget to the left and the last widget to the right. This is a simple and effective solution for many common layout scenarios.

Here’s how you can implement this in code:

Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('Left Item'), Text('Right Item'), ], ) 

This code snippet creates a Row widget with two Text widgets as children. The MainAxisAlignment.spaceBetween property ensures that the ‘Left Item’ is positioned on the left edge of the screen and the ‘Right Item’ is positioned on the right edge, with any available space distributed between them. This approach is clean, concise, and easy to understand, making it a popular choice for simple alignment tasks. According to Flutter’s official documentation, using MainAxisAlignment.spaceBetween is often the most efficient way to achieve this specific layout goal Flutter Row Documentation.

Consider a real-world example: a header in a mobile app with a back button on the left and a settings icon on the right. Using Row and MainAxisAlignment.spaceBetween allows you to easily position these elements consistently across different screen sizes. This ensures a professional and polished user experience. Remember to adjust the styling and padding of the individual widgets to fine-tune the appearance and ensure they align perfectly with your design.

Leveraging Expanded Widgets for Flexible Alignment

While MainAxisAlignment.spaceBetween is a simple solution, it may not always be the most flexible, especially if you need more control over the spacing between the items. In such cases, using Expanded widgets can provide more granular control. By wrapping a SizedBox or another widget with an Expanded widget between the two items you want to align two items on extremes, you can effectively push them to the edges of the screen.

Here’s the code:

Row( children: [ Text('Left Item'), Expanded( child: SizedBox(), // Or any widget that takes up space ), Text('Right Item'), ], ) 

In this example, the Expanded widget wraps an empty SizedBox. The Expanded widget forces the SizedBox to take up all the available space between the ‘Left Item’ and the ‘Right Item’, effectively pushing them to the left and right edges, respectively. You can replace the SizedBox with any other widget, such as a Container with a specific width or height, to further customize the spacing. This method gives you more control over how the space is distributed between the items.

A common use case for this approach is in creating a navigation bar where you want to dynamically adjust the space between the title and the action buttons based on the available screen width. By using Expanded widgets, you can ensure that the title remains centered while the action buttons are always positioned on the right edge. This approach is particularly useful when dealing with different screen sizes and orientations. According to a Stack Overflow survey, developers often prefer using Expanded for complex layouts where precise control over spacing is crucial Stack Overflow Trends.

Advanced Techniques and Considerations

Beyond the basic approaches, there are more advanced techniques you can use to refine your layouts and handle specific scenarios. One such technique involves using the Spacer widget. The Spacer widget is essentially a SizedBox wrapped in an Expanded widget, providing a convenient shorthand for creating flexible spacing. It automatically expands to fill the available space, pushing other widgets to the edges. This is another way to align two items on extremes.

Another consideration is handling different screen sizes and orientations. To ensure that your layouts adapt correctly to different devices, you can use LayoutBuilder to dynamically adjust the layout based on the available screen dimensions. LayoutBuilder allows you to access the constraints of the parent widget and make decisions about how to arrange the child widgets accordingly. This is particularly useful when you need to change the layout from a horizontal Row to a vertical Column on smaller screens. You can find further details about adaptive layouts in the official Flutter documentation Flutter Adaptive Layouts.

Furthermore, consider accessibility when designing your layouts. Ensure that your widgets are properly labeled and that the layout is intuitive for users with disabilities. Use semantic widgets and provide alternative text for images to improve accessibility. By following accessibility best practices, you can create inclusive apps that are usable by everyone. For example, if you have icons on the left and right that act as buttons, ensure they are large enough and have sufficient contrast for users with visual impairments.

  • Use MainAxisAlignment.spaceBetween for simple alignment.
  • Employ Expanded widgets for flexible spacing control.
  • Consider using Spacer for a shorthand approach.

FAQ

Q: What is the best way to align two items on extremes in Flutter?
A: The best way depends on your specific needs. `MainAxisAlignment.spaceBetween` is simple and effective for basic scenarios. `Expanded` widgets provide more control for complex layouts.
Q: How can I handle different screen sizes?
A: Use `LayoutBuilder` to dynamically adjust the layout based on the available screen dimensions.
Q: What is the purpose of the `Spacer` widget?
A: The `Spacer` widget is a convenient shorthand for creating flexible spacing, automatically expanding to fill the available space.
1. Start with a `Row` widget. 2. Add the two items you want to align. 3. Use `MainAxisAlignment.spaceBetween` or `Expanded` widgets to achieve the desired alignment. 4. Test on different screen sizes.
Infographic here showing comparison of alignment methods.
Featured Snippet:

To align two items on extremes in Flutter, the most direct method is using a Row widget with mainAxisAlignment: MainAxisAlignment.spaceBetween. This evenly distributes space between child widgets, pushing the first to the left and the last to the right. This approach is straightforward and ideal for scenarios where you want balanced spacing without complex calculations or custom sizing logic.

  • Ensure the code is clean and readable.
  • Test the layout on different devices and orientations.
  • Prioritize accessibility to make your app usable by everyone.

By now, you should be well-equipped to tackle the common UI challenge of aligning items on the left and right extremes of your Flutter applications. We’ve explored the simplicity of MainAxisAlignment.spaceBetween and the flexibility of Expanded widgets, along with advanced techniques like using Spacer and adapting to different screen sizes. Remember, the key is to understand the underlying principles of Flutter’s layout system and choose the approach that best suits your specific design requirements. Practice these techniques and you will be able to quickly create UIs that are visually balanced and responsive.

Ready to level up your Flutter skills? Check out our other articles on Flutter animations and state management for even more advanced UI development techniques. Don’t forget to experiment with these concepts in your own projects to solidify your understanding. And for further exploration, consider delving into custom layout widgets to unlock even greater design possibilities. You can also explore our advanced course on Flutter UI Mastery, which includes advanced tips, tricks, and best practices.

Question & Answer :
I am trying to align two items at extremes one on the left and one on the right. I have one row that is aligned to the left and then a child of that row is aligned to the right. However it seems the child row is picking up the alignment property from its parent. This is my code

var SettingsRow = new Row( mainAxisAlignment: MainAxisAlignment.end, crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.max, children: <Widget>[ Text("Right",softWrap: true,), ], ); var nameRow = new Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.max, children: <Widget>[ Text("Left"), SettingsRow, ], ); 

as a result I get something like this

Left Right 

What I would like is

Left Right 

Also there is enough space on the Row. My question is why is the child Row not exhibiting its MainAxisAlignment.end property ?

Use a single Row instead, with mainAxisAlignment: MainAxisAlignment.spaceBetween.

new Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ new Text("left"), new Text("right") ] ); 

Or you can use Expanded

new Row( children: [ new Text("left"), new Expanded( child: settingsRow, ), ], );