didChangeTextScaleFactor method Null safety

void didChangeTextScaleFactor()

Called when the platform's text scale factor changes.

This typically happens as the result of the user changing system preferences, and it should affect all of the text sizes in the application.

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

class TextScaleFactorReactor extends StatefulWidget {
  const TextScaleFactorReactor({ super.key });

  @override
  State<TextScaleFactorReactor> createState() => _TextScaleFactorReactorState();
}

class _TextScaleFactorReactorState extends State<TextScaleFactorReactor> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  late double _lastTextScaleFactor;

  @override
  void didChangeTextScaleFactor() {
    setState(() { _lastTextScaleFactor = WidgetsBinding.instance.window.textScaleFactor; });
  }

  @override
  Widget build(BuildContext context) {
    return Text('Current scale factor: $_lastTextScaleFactor');
  }
}

See also:

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

Implementation

void didChangeTextScaleFactor() { }