Flutter - Positioned Widget
Last Updated :
22 Sep, 2022
Positioned is a widget that comes built-in with flutter SDK. Positioned does exactly what it sounds like, which is it arbitrarily positioned widgets on top of each other. It is usually used to position child widgets in Stack widget or similar. It only works for Stateless and Stateful widgets.
Constructor of Positioned Class:
It is responsible for controlling where a child of Stack is positioned.
const Positioned(
{Key key,
double left,
double top,
double right,
double bottom,
double width,
double height,
@required Widget child}
)
Constructor of Positioned.directional class:
It is responsible for controlling where a child of Stack is positioned. But has additional properties for specifying the Text direction.
Positioned.directional(
{Key key,
@required TextDirection textDirection,
double start,
double top,
double end,
double bottom,
double width,
double height,
@required Widget child}
)
Constructor of Positioned.fill class:
It creates a Positioned object with a default value set to 0.0 for left, top, right, and bottom unless a value for them is passed.
const Positioned.fill(
{Key key,
double left: 0.0,
double top: 0.0,
double right: 0.0,
double bottom: 0.0,
@required Widget child}
)
Constructor of Positioned.fromRect class:
It is used to create a Positioned object with the values from the given Rect.
Positioned.fromRect(
{Key key,
Rect rect,
@required Widget child}
)
Constructor of Positioned.fromRelativeRect class:
It is used to create a Positioned object with the values from the given RelativeRect.
Positioned.fromRelativeRect(
{Key key,
RelativeRect rect,
@required Widget child}
)
Properties of Positioned Widget:
- bottom: This property controls the distance that the child widgets are inset from the bottom of the Stack. It takes in a double value as the object.
- debugTypicalAncestorWidgetClass: This widget takes in a Type class as the object. In the case of an error, this gives information about what widget typically wraps this ParentdataWidget.
- height: This property takes in a double value as the object to decide the height of its child widget.
- left: This property controls the distance that the child widgets are inset from the left of the Stack. It takes in a double value as the object.
- top: The top property also takes in a double value to decide the distance between the top edge of the child widget and the Stack.
- width: This property takes in a double value to decide the width of the child widget.
Example:
Dart
import 'package:flutter/material.dart';
// Material design library
void main() {
runApp(
// widget tree starts here
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('GeeksforGeeks'),
backgroundColor: Colors.greenAccent[400],
centerTitle: true,
), //AppBar
body: Padding(
padding: EdgeInsets.only(top: 300),
child: Stack(
alignment: AlignmentDirectional.center,
children: <Widget>[
/** Positioned WIdget **/
Positioned(
top: 0.0,
child: Icon(Icons.message,
size: 128.0, color: Colors.greenAccent[400]), //Icon
), //Positioned
/** Positioned WIdget **/
Positioned(
top: 0,
right: 285,
child: CircleAvatar(
radius: 16,
backgroundColor: Colors.red,
foregroundColor: Colors.white,
child: Text('24'),
), //CircularAvatar
), //Positioned
], //<Widget>[]
), //Stack
), //Padding
), //Scaffold
), //MaterialApp
);
}
Output:

Explanation: The parent widget in this flutter app is Padding which is employing its only property padding to print an empty space of 300 px on the top of its child widget which is Stack. The alignment is set to center in the Stack widget. Stack, as it does, is taking a list of widgets as children, here it's taking in two Positioned widgets. The first one is containing a green-colored material design message icon, which is 128 px long in width and height. The second Positioned widget is having a red-colored CircleAvatar as its child. The text n the CircleAvatar is 24 with white color. And the result of all this is message icons showing 24 notifications.
Similar Reads
Flutter - SizedBox Widget SizedBox is a built-in widget in flutter SDK. It is a simple box with a specified size. It can be used to set size constraints to the child widget, put an empty SizedBox between the two widgets to get some space in between, or something else. It is somewhat similar to a Container widget with fewer p
3 min read
Flutter - ListTile Widget The ListTile widget is used to populate a ListView in Flutter. It contains a title as well as leading or trailing icons. Let's understand this with the help of an example.Constructor of the ListTile classListTile ListTile({ Key? key, Widget? leading, Widget? title, Widget? subtitle, Widget? trailing
5 min read
Flutter - Inherited Widget If you are a flutter developer then you know how easy is to create Flutter UI. But when you have lots of widgets in your app and you want to pass data from one widget to another widget, this will be a pain for many developers,s especially for the newbie, this will be really hard for them to pass the
6 min read
Flutter - SizeTransition Widget In this article, we will explore the Flutter SizeTransition widget. The Size Transition Widget is a key tool in Flutter that lets you animate the size of a child widget. It can be used to make a widget appear or vanish, create a zoom-in or zoom-out effect, and for many more things. A sample video is
7 min read
Flutter - Padding Widget Padding widget in flutter does exactly what its name says, it adds padding or empty space around a widget or a bunch of widgets. We can apply padding around any widget by placing it as the child of the Padding widget. The size of the child widget inside padding is constrained by how much space is re
3 min read