Last active
April 14, 2019 14:26
-
-
Save mono0926/a22484b98bc9217cbe79ea861f0d9896 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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