How to set left and top in a Positioned widget in pixels or any other units in Flutter/Dart?

Issue

How can I set the left and top of a Positioned widget in pixels or any other units in Flutter/Dart?

What unit are the left and top values in? Are they in percents or independent pixels or something? If not, what unit does it use?

Solution

Example:

Stack(
  alignment: Alignment.center,
  children: [
    Container(
       height: 200,
       width: 200,
       color: Colors.red,
    ),
    // postion will be based on up Container Widget
    // position top left
    const Positioned(
       left: 0,
       top: 0,
       child: CircleAvatar(
           backgroundColor: Colors.blue,
       ),
    ),
    // position bottom right
    const Positioned(
        bottom: 0,
        right: 0,
        child: CircleAvatar(
           backgroundColor: Colors.blue,
        ),
     ),
  ],
),

If you don’t give any position. The position will be based on Stack alignment.

Answered By – Benjamin Mahmic

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

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