didChangeMetrics method

void didChangeMetrics()

Called when the application's dimensions change. For example, when a phone is rotated.

This method exposes notifications from dart:ui.PlatformDispatcher.onMetricsChanged.

This StatefulWidget implements the parts of the State and WidgetsBindingObserver protocols necessary to react when the device is rotated (or otherwise changes dimensions).
class MetricsReactor extends StatefulWidget {
  const MetricsReactor({ super.key });

  State<MetricsReactor> createState() => _MetricsReactorState();

class _MetricsReactorState extends State<MetricsReactor> with WidgetsBindingObserver {
  late Size _lastSize;

  void initState() {
    // [View.of] exposes the view from `WidgetsBinding.instance.platformDispatcher.views`
    // into which this widget is drawn.
    _lastSize = View.of(context).physicalSize;

  void dispose() {

  void didChangeMetrics() {
    setState(() { _lastSize = View.of(context).physicalSize; });

  Widget build(BuildContext context) {
    return Text('Current size: $_lastSize');

In general, this is unnecessary as the layout system takes care of automatically recomputing the application geometry when the application size changes.

See also:

  • MediaQuery.of, which provides a similar service with less boilerplate.


void didChangeMetrics() { }