Skip to content

Instantly share code, notes, and snippets.

@mono0926
Last active April 14, 2019 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mono0926/a22484b98bc9217cbe79ea861f0d9896 to your computer and use it in GitHub Desktop.
Save mono0926/a22484b98bc9217cbe79ea861f0d9896 to your computer and use it in GitHub Desktop.
import 'dart:math' as math;
import 'package:flutter/widgets.dart';
class TextScaleFactor extends StatelessWidget {
const TextScaleFactor({
Key key,
@required this.child,
this.min = 1,
this.max,
}) : super(key: key);
final Widget child;
final double min;
final double max;
@override
Widget build(BuildContext context) {
final mediaQuery = MediaQuery.of(context);
if (mediaQuery == null) {
assert(false, 'TextScaleFactor should be placed inside of MediaQuery');
return child;
}
final textScaleFactor = mediaQuery.textScaleFactor ?? 1;
final adjustedFactor =
math.min(math.max(min ?? 0, textScaleFactor), max ?? double.infinity);
return MediaQuery(
data: mediaQuery.copyWith(textScaleFactor: adjustedFactor),
child: child,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment