Home > Software engineering >  can the top, bottom etc of Positioned widget in a stack be specified as percentage?
can the top, bottom etc of Positioned widget in a stack be specified as percentage?

Time:09-17

When using the Positioned widget in flutter, can I specify the top and bottom parameter as a percentage of the stack instead of exact pixels?

CodePudding user response:

I have 2 suggestions :

first : in the Stack Widget, there is an alignment property, where you can choose a value from the Alignment enum to align the Positioned widget you can also wrap Positioned widget with Align widget

if this doesn't fit in your case in that alignment property you can use Alignment constructor to specify a y and x values to align your widget, so in example :

alignment: Alignment(0, 0)

will center your widget

alignment: Alignment(-1, -1)

will align your widget to the left bottom

alignment: Alignment(1, 1)

will align your widget to the right bottom

with this, you can use those between those values to get into a specific alignment with multiplying by your calculated percentage to get into specific align based on %

CodePudding user response:

I have an Idea! I don't know, how do you like it.

You can't use percentages directly for exact pixels. but you can use MediaQuery to use percentages.

final size = MediaQuery.of(context).size; //this var has 100% of your screen

now you can use a percentage of your screenSize in the top, bottom, right, and left properties.

like top: size.height / 2, //it will be divide your screenHeight by 2 means 50%

  • Related