Karan Owalekar
Karan Owalekar

Reputation: 967

Flutter: How to rebuild an app on a certain event

I have an flutter web-app, it works for all screen sizes. I assigned an variable "width" to get the screen width and based on that entire app is built.

When I Zoom in or out on chrome, it requires me to refresh the page to get the desired sizes. Unless I refresh the widgets don't resize.

I want a certain trigger like functionality, where if there is change in width, the app must rebuild.

How can I achieve this ?

Upvotes: 1

Views: 784

Answers (1)

Christopher Moore
Christopher Moore

Reputation: 17141

You can use a LayoutBuilder to achieve this. This widget has a builder parameter that is called as the layout constraints change.

LayoutBuilder(
    builder: (context, constraints) {

    },
)

The constraints provide options that allow your application to understand how much space is available

constraints.maxWidth; // get maximum available width
constraints.maxHeight; // get maximum available height
constraints.minWidth; // get minimum available width
constraints.minHeight; // get minimum available height

These can be used to conditionally change the layout based on the available space and rebuilds at layout time.

Alternatively, you can use a simply MediaQuery. You can get width:

MediaQuery.of(context).size.width;

and height:

MediaQuery.of(context).size.height;

However, these calls must be done in the build method for the sizes to change with each rebuild.

Upvotes: 2

Related Questions