flutterflutter-positioned

In Flutter, how to position a widget "x" pixels from the top of the stack but also have it horizontally centered?


I am using Flutter to create an app. I have a stack and want to position one of its child widgets (an svg picture) to be exactly 270 pixels from the top of the screen, and be centered horizontally at the same time. I have tried the Positioned() widget to set the absolute position form the top, but cannot center my widget horizontally.

Stack(
        children: [
          ListView(
            children: [ ],
          ),
          // horizontal alignment does not work here
          Align(
            alignment: Alignment.center,
            child: Positioned(
              top: 270,
              child: SvgPicture.asset(),
            ),
          ),
          // same issue here
          Positioned(
              top: 270,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SvgPicture.asset(),
                ],
              )),
        ],
      ),

I used a combination of Positioned(), Align() and Row widgets to get what I wanted but it did not work.


Solution

  • Just use

    Stack(
        alignment: Alignment.center,
    

    like this

    Stack(
      alignment: Alignment.center,
      children: [
        ListView(
          children: [ ],
        ),
        Positioned(
          top: 270,
          child: SvgPicture.asset(),
        ),
      ],
    ),