我在 ThemeData 的 textTheme 中指定了 textSize。
textTheme: const TextTheme(
displayLarge: TextStyle(
color: Colors.black,
letterSpacing: 4,
fontSize: 25,
),
displayMedium: TextStyle(
color: Colors.black,
letterSpacing: 2,
fontSize: 18,
),
),
但是,使用这种方法,文本在某些设备上可能会变得非常小。因此,您需要获取设备的大小并指定fontSize,如下所示。
fontSize: MediaQuery.of(context).size.width * 0.01,
由于需要上下文,因此无法在 ThemeData 中获取设备的大小。因此,似乎在 ThemeData 中使用 TextTheme 指定 fontSize 可能不是最佳实践。
你们都是怎么指定fontSize的?
您可以检查 flutter_screenutil 包,或编写将使用上下文的实用函数:
TextStyle myTextStyle(BuildContext context) {
return TextStyle(
fontSize: MediaQuery.of(context).size.width * ... ,
);
}