withClampedTextScaling static method

Widget withClampedTextScaling({
  1. Key? key,
  2. double minScaleFactor = 0.0,
  3. double maxScaleFactor = double.infinity,
  4. required Widget child,
})

Wraps the child in a MediaQuery and applies TextScaler.clamp on the current MediaQueryData.textScaler.

The returned widget must be inserted in a widget tree below an existing MediaQuery widget.

This is a convenience function to restrict the range of the scaled text size to [minScaleFactor * fontSize, maxScaleFactor * fontSize] (to prevent excessive text scaling that would break the UI, for example). When minScaleFactor equals maxScaleFactor, the scaler becomes TextScaler.linear(minScaleFactor).

Implementation

static Widget withClampedTextScaling({
  Key? key,
  double minScaleFactor = 0.0,
  double maxScaleFactor = double.infinity,
  required Widget child,
}) {
  assert(maxScaleFactor >= minScaleFactor);
  assert(!maxScaleFactor.isNaN);
  assert(minScaleFactor.isFinite);
  assert(minScaleFactor >= 0);

  return Builder(
    builder: (BuildContext context) {
      assert(debugCheckHasMediaQuery(context));
      final MediaQueryData data = MediaQuery.of(context);
      return MediaQuery(
        data: data.copyWith(
          textScaler: data.textScaler.clamp(
            minScaleFactor: minScaleFactor,
            maxScaleFactor: maxScaleFactor,
          ),
        ),
        child: child,
      );
    },
  );
}