flutter 听取对系统字体大小所做的更改

y1aodyip  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(130)

我需要UI对系统字体大小的更改做出React。如何订阅这些更改?

vxf3dgd4

vxf3dgd41#

您可以使用WidgetsBindingObserver执行此操作

import 'package:flutter/cupertino.dart';

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

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

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

  @override
  void didChangeMetrics() {
    final currentFontSize = WidgetsBinding.instance.window.textScaleFactor;
    print('Font size changed to $currentFontSize');
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

相关问题