thomas.s
thomas.s

Reputation: 348

how to set a widget place according to screen size flutter

I have a widget and I want it to have a specific space from the edge of the screen, according to screen size. I have already tried MediaQuery but that is not helpful. Because I want to have a space according to resolution. Here is what I have tried :

MediaQuery.of(context).size.width - 50,

And here is what I'm trying to do

enter image description here

How I'm gonna do that?

Upvotes: 2

Views: 661

Answers (2)

Saeed Fekri
Saeed Fekri

Reputation: 1135

You can store width of screen to field to use it easily anywhere. And to have dynamic spaces, you can doing like Hamza says in comments:

double mWidth;
  @override
  Widget build(BuildContext context) {
    mWidth= MediaQuery.of(context).size.width;
    return Scaffold(
      body: Container(
        width: mWidth,
        margin: EdgeInsets.all(mWidth * 0.01),
       ),
    );
}

Upvotes: 3

Akif
Akif

Reputation: 7660

As @Hamza mentioned, you can use a percentage to get a relative size. There is a blog in here about this issue.

    Container(
        height: MediaQuery.of(context).size.height * 0.01, // like this.
     );

Upvotes: 1

Related Questions