I’m trying to set the font size of the text in my app through the themeData using media Query.
eg. if screen size is smaller the x, font size should be x.
is this even possible to achieve? and if so, how would I go about it?
here is my MyApp code in my main.dart:
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: Colors.black,
textTheme: TextTheme(
bodyText1: TextStyle(
fontWeight: FontWeight.bold,
fontFamily: 'SourceSansPro',
),
),
),
debugShowCheckedModeBanner: false,
initialRoute: '/second',
routes: {
'/': (context) => pageone(),
'/first': (context) => pagetwo(),
'/second': (context) => pagethree(),
});
}
}
When I try and use var screenSize = MediaQuery.of(context).size; as a variable into MyApp, i’m getting an error on context.
thanks so much and any help would be greatly appreciated.
2
Answers
I would create multiple TextThemes, one for each screen size you want to support:
Then in MaterialApp, you can determine which TextTheme you use based on the screen size:
See this answer for how to use the device size in the MaterialApp widget. If you try to use MediaQuery, you will get an error like this:
You need to separate the context(initialize on top level). then you will be able to read.
You can do
But I think it is not necessary to increase the font size.