),
);
}
}
在代码中,我们是想获取屏幕的宽和高,然后将屏幕宽高的一半分别赋值给 Container
的宽和高,但上述代码并不能成功运行,会报如下错误:
flutter: The following assertion was thrown building GetWidgetWidthAndHeiget(dirty):
flutter: MediaQuery.of() called with a context that does not contain a MediaQuery.
flutter: No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of().
flutter: This can happen because you do not have a WidgetsApp or MaterialApp widget (those widgets introduce
flutter: a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.
从错误异常中我们可以大概了解到有两种情况会导致上述异常:
- 当没有
WidgetsApp or MaterialApp
的时候,我们使用MediaQuery.of(context)
来获取数据。 - 当我们在当前小部件中使用了上一个小部件的 context,来使用
MediaQuery.of(context)
获取数据的时候。
我们上述的代码很显然是属于第一种情况,也就是说我们在使用 MediaQuery.of(context)
的地方并没有一个 WidgetsApp or MaterialApp
来提供数据。
解决方法就是将 MediaQuery.of(context)
挪到 MaterialApp
内,如下:
import ‘package:flutter/material.dart’;
class GetWidgetWidthAndHeiget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
final width = size.width;
final height = size.height;
print(‘width is $width; height is $height’);
return Scaffold(
appBar: AppBar(
title: Text(‘Width & Height’),
),
body: Center(
child: Container(
color: Colors.redAccent,
width: width / 2,
height: height / 2,
),
),
);
}
}
运行效果及输出如下:
flutter: width is 414.0; height is 896.0
上述代码中,我们获取的是 MaterialApp 的宽高,也就是屏幕的宽高
还有一种是直接使用 dart:ui
包中的 window
对象(这里非常感谢 XuYanjun Android @ 苏宁 提出的方法),这种方法使用起来也比较简单,如下:
import ‘dart:ui’;
final width = window.physicalSize.width;
final height = window.physicalSize.height;
那么如果我们要需要知道上述红色的 Container 容器的宽高怎么办呢?这里我们可以使用 GlobalKey
GlobalKey
使用 GlobalKey 的步骤如下:
-
声明一个 GlobalKey
final GlobalKey globalKey = GlobalKey();
-
给 widget 设置 GlobalKey
key: globalKey
-
通过
globalKey
来获取该 widget 的 size
final containerWidth = globalKey.currentContext.size.width;
final containerHeight = globalKey.currentContext.size.height;
print(‘Container widht is $containerWidth, height is $containerHeight’);
修改过后的 HomePage
代码如下:
class HomePage extends StatelessWidget {
final GlobalKey globalKey = GlobalKey();
void _getWH() {
final containerWidth = globalKey.currentContext.size.width;
final containerHeight = globalKey.currentContext.size.height;
print(‘Container widht is $containerWidth, height is $containerHeight’);
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
final width = size.width;
final height = size.height;
print(‘width is $width; height is $height’);
return Scaffold(
appBar: AppBar(
title: Text(‘Width & Height’),
),
body: Center(
child: Container(
key: globalKey,
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618165277)
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!