0

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?

The Amateur Coder
  • 789
  • 3
  • 11
  • 33
  • 2
    they are "logical pixels", see [devicePixelRatio](https://api.flutter.dev/flutter/widgets/MediaQueryData/devicePixelRatio.html) for more info – pskink Sep 25 '21 at 05:15

1 Answers1

2

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.

  • Oh, thanks a lot for the code! Is there any way to get the screen size? I tried using MediaQuery but didn't work. I returned a MaterialApp but still I'm not able to use the build context. – The Amateur Coder Sep 27 '21 at 12:05