flutter - 如何在 MaterialApp 级别定义媒体查询,以便可以将其应用于 Flutter 中的所有屏幕/脚手架?
问题描述
在我的启用 web 的颤振应用程序中,为了添加媒体查询,我在 index.html 文件的标签中添加了css
类。body
<head>
....
<style>
.mobile{
max-width:auto;
}
@media (min-width: 600px) {
.mobile{
max-width:400px;
}
}
</style>
</head>
<body class="mobile">
<script src="main.dart.js" type="application/javascript"></script>
</body>
但是这里的颤动是在flt-glass-pane
标签内创建标签body
,它正在渲染 UI 溢出body
标签。
在我的第二种方法中,我必须在屏幕级别添加 MediaQuery,但我不想在每个scaffold/screen
.
那么有没有其他方法可以让我只MateriaApp
在文件中或文件中应用媒体查询index.html
一次。
解决方案
在 Material App 中使用“builder”
MaterialApp(
...
...
builder: (context, child) {
double screenWidth = MediaQuery.of(context).size.width;
return Center(
child: Container(
width: screenWidth > 600 ? 400 : screenWidth,
child: child,
),
);
},
);
推荐阅读
- python - 熊猫:分组并通过连接结果创建一个新列
- python - pandas - 减去 2 个相似的数据框数据透视表
- javascript - 从 TextField 选择组件、Material UI 和 React 手动失去焦点
- php - PHP变量在第二页上没有正确返回
- powershell - 如何在 PowerShell 中将参数作为 -file 的一部分传递
- gcc - 如何让 gcc 为 -fpatchable-function-entry 发出多字节 NOP?
- javascript - 在 Firebase 中创建带有评论的帖子
- java - Spring Boot 无法将非字符串类型字段的 POST 请求中的 JSON 转换为 pojo
- ionic-framework - 将 ion-col 内的 div 对齐到列的底部
- terraform - 从 terraform 中的 csv 构建地图列表