As a beginner in the development field, I've been endeavoring to grasp the concepts of creating responsive and adaptive apps. However, I'm encountering some confusion. One particular question arises: Should the text resize when the device size changes? I've sought clarification through various YouTube videos where different font sizes are employed for distinct devices such as mobile, tablet, and desktop, like this.
LayoutBuilder(
builder: (context, constraints) {
// mobile ...................................................................
if (constraints.maxWidth < 600) {
return const Text(
'Example',
style: TextStyle(
fontSize: 20,
),
);
// tablet .................................................................
} else if (constraints.maxWidth > 600 && constraints.maxWidth < 800) {
return const Text(
'Example',
style: TextStyle(
fontSize: 25,
),
);
// mobile .................................................................
} else {
return const Text(
'Example',
style: TextStyle(
fontSize: 30,
),
);
}
},
);
But, mobile devices can have many screen sizes, for example. However, they use a constant size for all the mobile devices (fontSize: 20). I cannot understand this. Please clear my doubt and tell me which is the best method to achieve responsiveness?
You're on the right track, but you'll have to use a mix of solutions, including your example, to achieve the results you want.
There are a couple of more classes you should add to your toolbox:
1. FractionallySizedbox
Allows you to size based off a
factorsize e.g. percent.More info: https://api.flutter.dev/flutter/widgets/FractionallySizedBox-class.html
2. MediaQuery
MediaQuerygives you a LOT of info about the device such as size, orientation, if the user changed the default font size, etc.More info: https://api.flutter.dev/flutter/widgets/MediaQuery-class.html