0

Hi I am trying to create several TextTheme and change the fonts sizes using MediaQuery.of(context) based on this article:

Flutter — Effectively scale UI according to different screen sizes

But I am getting this error:

MediaQuery.of() called with a context that does not contain a MediaQuery.

I know based on this post: Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery

I should use MediaQuery on my HomePage but then I cannot create themes using MediaQuery then?

Here is my code:

 child: MaterialApp( theme: ThemeData( /// TextFields Handlers transparent textSelectionHandleColor: Colors.transparent, pageTransitionsTheme: const PageTransitionsTheme( builders: <TargetPlatform, PageTransitionsBuilder>{ TargetPlatform.android: ZoomPageTransitionsBuilder(), }, ), textTheme: TextTheme( /// Pages Titles headline1: textTheme( fontSize: (MediaQuery.of(context).size.width / 100) * 1.5, fontWeight: FontWeight.w600, color: Globals.allColors['celeste'], ), headline2: textTheme( fontSize: 15, fontWeight: FontWeight.w600, color: Globals.allColors['cetaceanBlue']), ... 

The error is at:

(MediaQuery.of(context).size.width / 100) * 1.5, 

Thanks in advance!

1

5 Answers 5

0

No, you cannot create your material theme with values retrieved from Mediaquery. You set 'default' font size, etc. in your material app theme data. Then in your home page build method you call Mediaquery and modify your font size for that page if necessary.

Sign up to request clarification or add additional context in comments.

2 Comments

That is a shame, it would be useful to resize the fonts based on each device resolution, using themes instead of changing them with .copyWith all over the code. Thanks for your answer Graham
Glad I could help.
0

Try wrapping your entire homepage in a builder widget.

1 Comment

Got the same error trying to use Builder Widget above MaterialApp, thanks for your answer
0

You could do this like a one-off kind of thing. You could save your text themes in a themes.dart file or something like that..and then subsequently, you could have a loading page, where you make a MediaQuery and calculate your theme values there.

I think you might have to run setState after that, maybe not, because when you navigate to your home screen from there, the values of the theme would have updated.

Comments

0

MediaQuery needs the MaterialApp widget as an ancestor, you can't use it when constructing the MaterialApp itself. A workaround is not using the theme property but wrapping the descendants with a Theme widget using the builder property. It will have the MaterialApp as an ancestor so you can use MediaQuery there.

MaterialApp( builder: (context, child) { return Theme( data: ThemeData( // your theme // can use e.g. MediaQuery.of(context).size.width here ), child: child, ); }, home: SomeScreen(), // etc, no theme here ) 

Comments

0

There is a work around this and it is by providing a new instance of the component you want to give a theme to using mediaQuery I think it's the least hassle for the most outcome like in here

The theme property in the material app

 theme: ThemeClass.lightTheme.copyWith( elevatedButtonTheme: elevatedButtonTheme(), outlinedButtonTheme: outlinedButtonTheme(), ), 

A function to return the button theme for example

 elevatedButtonTheme() { return ElevatedButtonThemeData( style: ElevatedButton.styleFrom( minimumSize: Size( double.maxFinite, getProportionateScreenHeight(49), ), elevation: 0, textStyle: textThemeLight.labelLarge!.copyWith( fontWeight: FontWeight.w600, color: Colors.white, ), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), foregroundColor: Colors.white, backgroundColor: primaryColor, ), ); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.