Listen to window size changes

Flutter tips Published on

Sometimes in your Flutter app, you need to know when the window size changes. This is important for making your app look good on different screen sizes or when the user rotates their phone.

How to catch size changes

The key to listening for these changes is a special helper called WidgetsBindingObserver. It's like a spy that watches for things happening in the Flutter engine.

Setting up the listener

To start listening, your widget's State needs to with the WidgetsBindingObserver. This gives it the ability to receive updates.

In your initState method, you need to tell the system you want to start listening. You do this by adding your widget's State as an observer to WidgetsBinding.instance.

Reacting to changes

The WidgetsBindingObserver requires you to implement a method called didChangeMetrics. This method is automatically called whenever the screen size, device orientation, or other related things change.

Inside didChangeMetrics, you can update your widget's state, for example, storing the new window size and rebuilding the UI to match.

Cleaning up

It's crucial to stop listening when your widget is removed. In the dispose method, you must remove your widget's State as an observer. If you don't, your app might have problems or use too much memory.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

Why do I need to listen to window size changes?

Listening to window size changes helps your app adapt to different screen sizes, handle device rotation (portrait vs. landscape), and work well on desktop or web platforms where windows can be resized.

What is WidgetsBindingObserver?

WidgetsBindingObserver is a class that allows your Flutter widget's State to receive notifications about changes from the Flutter engine, including window size changes.

How do I start listening for size changes?

You need to make your widget's State `with WidgetsBindingObserver`, then call `WidgetsBinding.instance.addObserver(this)` inside your `initState` method.

Where do I put the code to handle the size change?

You implement the `didChangeMetrics` method provided by `WidgetsBindingObserver`. This method is called whenever the window metrics change.

Is it important to stop listening?

Yes, you must remove the observer by calling `WidgetsBinding.instance.removeObserver(this)` in your `dispose` method. Failing to do so can lead to memory leaks and other issues.

Read more
You may also be interested in
Flutter tips: how to create a responsive layout  blog card image
Flutter tips: how to create a responsive layout
Published on 2025-05-03
Guide Users Easily: Tutorial Overlays in Flutter with pal_widgets  blog card image
Guide Users Easily: Tutorial Overlays in Flutter with pal_widgets
Published on 2025-05-12T08:10:13.136Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved