我尝试从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(),
);
答案 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(),
),