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!

Miguel Chavez
  • 161
  • 1
  • 2
  • 14

4 Answers4

0

Try wrapping your entire homepage in a builder widget.

ng5002
  • 477
  • 2
  • 4
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.

GrahamD
  • 2,952
  • 3
  • 15
  • 26
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.

Pranav Manoj
  • 69
  • 1
  • 6
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
)