如何在Flutter中创建和使用自适应主题?

时间:2018-04-18 05:40:15

标签: flutter flutter-layout

我尝试从MediaQuery.of() called with a context that does not contain a MediaQuery.访问MediaQuery时收到MaterialApp

return new MaterialApp(
  title: 'Flutter Demo',
  theme: getTheme(MediaQuery.of(context)),
  home: new Container(),
);

2 个答案:

答案 0 :(得分:2)

return new MaterialApp(
      title: 'Flutter Demo',
      theme: getTheme(new MediaQueryData.fromWindow(ui.window)),
      home: new Container(),
    );

感谢Günter:)

答案 1 :(得分:1)

使用builder代替MaterialApp。这样您就可以在MediaQuery内使用由MatetialApp实例化的Theme

new MaterialApp(
  builder: (context, child) {
    return new Theme(
      data: getTheme(MediaQuery.of(context)),
      child: child,
    );
  },
  home: new ChatScreen(),
),