本文介绍了如何在Flutter中创建和使用自适应主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm getting MediaQuery.of() called with a context that does not contain a MediaQuery. when trying to access MediaQuery from MaterialApp:

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

推荐答案

请改用MaterialAppbuilder.这样您就可以在Theme中使用由MatetialApp实例化的MediaQuery.

Use builder of MaterialApp instead. So that you can use MediaQuery instantiated by MatetialApp inside your Theme.

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

这篇关于如何在Flutter中创建和使用自适应主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 03:15