Flutter, a framework introduced by Google, has made the task of creating beautiful UI way easier than ever. In this blog, we will specifically focus on understanding the Flutter Positioned class. Trust us, if you aim to create an aesthetically pleasing UI for your Flutter application, you are in the right place.
The Flutter Positioned class finds its significance primarily in arranging multiple widgets inside a stack widget. Particularly useful when you wish to control exactly how and where a child widget should be positioned within the stack. It's important to note that the Positioned widget requires a specific type of parent widget, namely a Stack, to function correctly, as the coordinates are relative to the top-left corner of the Stack.
Asynchronously, Flutter Positioned performs operations and implicitly provides a future. A Future in Dart is similar to promises in JavaScript - a way to perform asynchronous computations, delay code execution, or write computation that completes at a later time.
1Future<void> main() { 2 print('Creating a Flutter application'); 3 print('Adding a Positioned widget'); 4}
Developers can create a 'positioned object' with the Positioned class to specify the widget's exact location within a Stack, offering precise control over its placement.
The Flutter Positioned class comes into play when you have a stack and you want to direct certain child widgets to specific locations. The Flutter Positioned widget, an async function, essentially determines where its child should be positioned.
The future delayed function of the Flutter Positioned plays a role in executing the actual positioning. This is very different from the default widget positioning that Flutter allows, as it provides more explicit control over the dimensions of the child widgets.
For example:
1void main() { 2 Stack( 3 children: <Widget>[ 4 Positioned( 5 top: 10, 6 child: Icon(Icons.star, size: 50), 7 ), 8 ], 9 ); 10}
With Future.delayed function, the Positioned widget is set to be placed 10 units from the top.
Flutter by default, aligns widgets from top down, left to right. However, the Flutter Positioned widget overrides these and allows specific positioning for each widget.
Imagine a synchronous function where the widgets are being aligned in their natural order and then comes a Flutter Positioned widget. The default alignment gets on hold, the Positioned widget gets placed where it is meant to be (based on the four parameters - top, bottom, right, and left), and then the remaining widgets carry on their code execution in their standard alignment.
This gives a lot of power to the developers as they can precisely control the execution flow as per the user's needs. They can dictate where exactly a widget should appear within a stack, even if it means disrupting the standard sequence of widget alignment.
The fundamental structure of a Flutter Positioned widget includes properties such as top, bottom, left, and right to precisely control the positioning of the child widget.
1Positioned ( 2 top: double, 3 bottom: double, 4 left: double, 5 right: double, 6 child: Widget, 7)
Each of these properties accepts a double value and they represent the distance of the child widget from the respective edge of the stack. These vary in specified duration and you're not required to use all four; only what's essential to define the expected position for the widget.
Positioning one widget using the Flutter Positioned widget within your Flutter application is a fairly simple task in Android Studio. Once you know the specifics of where you want to position the widget, you simply have to wrap the Positioned widget in a Container to manage a single widget within your stack and define the properties as per your needs.
1void main() { 2 Stack( 3 children: const <Widget>[ 4 Positioned( 5 top: 20, 6 left: 50, 7 child: const Text('Flutter Positioned Widget'), 8 ), 9 ], 10 ); 11}
It’s important to note that the duration and delay of having the Flutter Positioned widget displayed will be exactly as per the top, and left values entered. We used ‘const duration’ to specify the duration after which the Positioned widget will be visible on the Flutter application.
The Flutter stack widget is a crucial part of this discussion as Flutter Positioned works in sync with it. Stack allows multiple widgets to overlap one another, similar to a stack of cards. But it doesn't specify the position of the widgets which is where the Flutter Positioned class enters.
1Stack( 2 children: [ 3 widget1, 4 widget2, 5 Positioned( 6 top: 0, 7 child: widget3, 8 ) 9 ], 10)
To place multiple widgets using Flutter Positioned Widget within a single stack, you just need to add multiple Positioned widgets to the stack's children.
1Void main() { 2Stack( 3 children: const <Widget>[ 4 Positioned( 5 top: 20, 6 left: 50, 7 child: const Text('Flutter Application Position 1'), 8 ), 9 Positioned( 10 top: 40, 11 left: 100, 12 child: const Text('Flutter Application Position 2'), 13 ), 14 ], 15 ); 16}
By varying the 'const duration seconds', the delay between the appearances of the two widgets, the user can create a much more dynamic UI. The Matcher test aids in checking the likes of spatial alignment of these widgets.
Mastering the Flutter Positioned class not only allows you to disrupt the natural flow of widget alignment but also provides excellent control over the final UI of your Flutter applications.
1Positioned( 2 right: 20.0, 3 top: 40.0, 4 child: FlutterLogo(size: 50), 5)
The above code snippet will place the Flutter logo 20 units from the right and 40 units from the top. With the right use of async and Future.delayed, the duration can be added to when these Position changes get reflected.
Layering widgets: Since Flutter Positioned is used within a stack, it allows for the layering of widgets over one another, opening a wide range of opportunities for creating complex UI designs.
Dynamic UI execution: With a good knowledge of async, await, Future.delayed and durations in Dart, UI elements can be made to appear, disappear, and move dynamically making Flutter applications more interactive and dynamic.
Position children relative: Utilizing the Flutter Positioned widget, developers can 'position children relative' to their parent widgets. This method enhances UI design flexibility and precision, akin to CSS absolute positioning but within the Flutter environment.
With several benefits of using Flutter Positioned, there may be challenges encountered by developers.
Use within Stack: Flutter Positioned must be a descendant of Stack. Using Flutter Positioned in any other widget hierarchy throws an error.
Single Child: Only a single widget can be used as the child of Positioned. So, if you want to position a group of widgets, enclose them within another widget like Column or Row, and use that as the child.
Negative Positions: If incorrect values are used (i.e., negative values), it may lead to unwanted distortions in the UI.
Mitigating these errors and understanding the eventualities can provide a seamless experience in the Flutter Positioned implementation. As with any other code, testing is advised to ensure correct placements and error-free execution.
With explicit control over the positioning of the child widget, the Flutter Positioned class extends the power to the developer to control and test the design and flow of UI elements dynamically within the Flutter application.
With Positioned widget and Stack class in your arsenal, creating beautifully layered and dynamic user interfaces becomes an achievable reality. However, it's important to understand the intricacies of how these two interact with each other, to make the best use of them.
Happy coding with Flutter!
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.