I was specifying the textSize in the textTheme of ThemeData.
textTheme: const TextTheme(
displayLarge: TextStyle(
color: Colors.black,
letterSpacing: 4,
fontSize: 25,
),
displayMedium: TextStyle(
color: Colors.black,
letterSpacing: 2,
fontSize: 18,
),
),
However, with this approach, the text may become extremely small on some devices. Therefore, you need to obtain the device’s size and specify the fontSize as follows.
fontSize: MediaQuery.of(context).size.width * 0.01,
Since context is required, it’s not possible to obtain the device’s size within ThemeData. Therefore, it seems that specifying fontSize using TextTheme in ThemeData may not be the best practice.
How do you all specify the fontSize?
3
Answers
You can check flutter_screenutil package, or write utility function that will be using context:
Use
textScaleFactor
for adjusting scale globally withMaterialApp.builder
.Caution: Respect Accessbility about Large Font. Dont overuse or fix
textScaleFactor
. It is more natural limit range oftextScaleFactor
and weighted factor on screen size.You can use auto_size_text package.
Flutter widget that automatically resizes text to fit perfectly within its bounds.