How To Easily Use Flutter Positioned Widget – Example Code

flutter positioned widget

In this Flutter post, we will learn how to practically use and customize Flutter positioned widget by using proper Flutter example. We will be using multiple Flutter examples to properly demonstrate the role of Flutter positioned widget.

After reading this post, I am sure you will be able to incorporate Flutter positioned widget in your Flutter apps with ease.

What is Flutter Positioned Widget?

As the name suggests, it is used to specify the position/place of its child widget. Let’s use a Flutter stack widget example to understand how the Flutter positioned widget works.

Implementing Flutter Positioned Widget (Easy Examples)

For that, we first have to define a simple Flutter stack widget. See below code:

Container(
             height: 200,
             width: 200,
             color: Colors.grey,
               child: Stack(children: [
                      Container(height: 50, width: 50, color: Colors.green),
                    ]))
flutter stack widget container
You can see that we have first defined a container with some height, width and a background color. Then we have passed Flutter stack widget as a child to it. Finally, we have specified another container widget inside stack widget.
Now let’s see how to change the position of that container that is passed as a child to the stack widget.
For that, we have to wrap that container with a Flutter positioned widget. See below code:
 Positioned(
    child: Container(
       height: 50, width: 50, color: Colors.green))
Let’s now use the constructors of Flutter positioned widget. We will customize the position of this container using these constructors.

Flutter Positioned Widget Constructors

Its constructors are listed below:

  1. top
  2. left
  3. right
  4. bottom
  5. height 
  6. width

Top Constructor

It is used to specify the position of the child widget using the top border of its parent. See below code:

 Positioned(
             top: 20,
                 child: Container(
                  height: 50, width: 50, color: Colors.green))
flutter stack widget top

The greater the value, the more distance it will have from the top and vice versa.

Left Constructor

It will specify the distance from the left border of its parent widget. See below code

 Positioned(
      left: 20,
        child: Container(
        height: 50, width: 50, color: Colors.green))
flutter stack widget left

Right Constructor

It specifies the distance/space from the right. See code below:

 Positioned(
      right: 20,
        child: Container(
        height: 50, width: 50, color: Colors.green))
flutter stack widget right

Bottom Constructor

It’s used to give space from the bottom border of the parent widget. See below code:

 Positioned(
      bottom: 20,
        child: Container(
        height: 50, width: 50, color: Colors.green))

flutter stack widget bottom

Height and Width Constructors

These are used to specify the height and width of its child widget. See below code:

 Positioned(
     height: 30, width:80
        child: Container(color: Colors.green))

flutter stack widget height width

So you can use the Flutter positioned widget to specify the exact position of your widget.

I hope you now have a proper idea of how to use and customize Flutter positioned widget. I also have implemented a more customized example which is available in the below section.

flutter positioned widget

Conclusion

To conclude, we have used multiple examples to properly demonstrate how to use and customize Flutter positioned widget. Hope you now have learnt how to properly use it. Thank you for reading this Flutter post.

Leave a Comment

Your email address will not be published. Required fields are marked *