如何在 Flutter 中创建和使用响应式主题?

How to create and use responsive themes in Flutter?

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

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

谢谢 Günter :)

改用 builderMaterialApp。这样您就可以在 Theme 中使用由 MatetialApp 实例化的 MediaQuery

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