1 | initial version |
There are several ways to adjust font size in Flutter to fit different screen sizes:
Example:
double screenWidth = MediaQuery.of(context).size.width;
double fontSize = screenWidth * 0.05; // 5% of screen width
Text("Hello World", style: TextStyle(fontSize: fontSize));
Example:
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
double fontSize = constraints.maxWidth * 0.05; // 5% of box width
return Text("Hello World", style: TextStyle(fontSize: fontSize));
},
),
Example:
import 'package:flutter_screenutil/flutter_screenutil.dart';
Text("Hello World", style: TextStyle(fontSize: ScreenUtil().setSp(30)));
In this example, the setSp()
method scales the font size to be 30 SP (scaled pixels) for the current screen size and pixel density.
Overall, choosing the best approach depends on your particular use case and design requirements.