【发布时间】:2021-11-30 20:27:16
【问题描述】:
主要
final query = MediaQuery.of(context);
return MediaQuery(
data: query.copyWith(textScaleFactor: 1.4),
child: MaterialApp(
title: "Flutter Demo",
initialRoute: '/loginPage',
)
);
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (13775): The following assertion was thrown building MyApp(dirty, state: _MyAppState#38690):
I/flutter (13775): MediaQuery.of() called with a context that does not contain a MediaQuery.
I/flutter (13775): No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of().
I/flutter (13775): This can happen because you do not have a WidgetsApp or MaterialApp widget (those widgets introduce
I/flutter (13775): a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.
使用 MediaQuery 类及其 data 属性,我想为整个应用程序设置 textScaleFactor,但我无法获得MaterialApp...
之前的上下文所以我不想为每个页面都添加像 parent 这样的类,我怎么能只为整个应用程序添加一次类?
LoginPage(它有效,但我不想要这个)
final query = MediaQuery.of(context);
return MediaQuery(
data: query.copyWith(textScaleFactor: 1.4),
child: Scaffold(
[...]
);
谢谢,
【问题讨论】:
-
不行,因为我每个页面都要做,比如我有10个主页面,我不会做10次,有没有办法全局做?跨度>