经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
Flutter中获取屏幕及Widget的宽高示例代码
来源:jb51  时间:2019/3/11 8:36:43  对本文有异议

前言

我们平时在开发中的过程中通常都会获取屏幕或者 widget 的宽高用来做一些事情,在 Flutter 中,我们有两种方法来获取 widget 的宽高。

MediaQuery

一般情况下,我们会使用如下方式去获取 widget 的宽高:

  1. final size =MediaQuery.of(context).size;
  2. final width =size.width;
  3. final height =size.height;

但是如果不注意,这种写法很容易报错,例如下面的写法就会报错:

  1. import 'package:flutter/material.dart';
  2.  
  3. class GetWidgetWidthAndHeiget extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. final size =MediaQuery.of(context).size;
  7. final width =size.width;
  8. final height =size.height;
  9. print('width is $width; height is $height');
  10. return MaterialApp(
  11. home: Scaffold(
  12. appBar: AppBar(
  13. title: Text('Width & Height'),
  14. ),
  15. body: Container(
  16. width: width / 2,
  17. height: height / 2,
  18. ),
  19. ),
  20. );
  21. }
  22. }

在代码中,我们是想获取屏幕的宽和高,然后将屏幕宽高的一半分别赋值给 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 内,如下:

  1. import 'package:flutter/material.dart';
  2.  
  3. class GetWidgetWidthAndHeiget extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. home: HomePage(),
  8. );
  9. }
  10. }
  11.  
  12. class HomePage extends StatelessWidget {
  13. @override
  14. Widget build(BuildContext context) {
  15. final size = MediaQuery.of(context).size;
  16. final width = size.width;
  17. final height = size.height;
  18. print('width is $width; height is $height');
  19. return Scaffold(
  20. appBar: AppBar(
  21. title: Text('Width & Height'),
  22. ),
  23. body: Center(
  24. child: Container(
  25. color: Colors.redAccent,
  26. width: width / 2,
  27. height: height / 2,
  28. ),
  29. ),
  30. );
  31. }
  32. }

运行效果及输出如下:

flutter: width is 414.0; height is 896.0

上述代码中,我们获取的是 MaterialApp 的宽高,也就是屏幕的宽高


那么如果我们要需要知道上述红色的 Container 容器的宽高怎么办呢?这里我们可以使用 GlobalKey

GlobalKey

使用 GlobalKey 的步骤如下:

  • 声明一个 GlobalKey final GlobalKey globalKey = GlobalKey();
  • 给 widget 设置 GlobalKey key: globalKey
  • 通过 globalKey 来获取该 widget 的 size
  1. final containerWidth = globalKey.currentContext.size.width;
  2. final containerHeight = globalKey.currentContext.size.height;
  3. print('Container widht is $containerWidth, height is $containerHeight');

修改过后的 HomePage 代码如下:

  1. class HomePage extends StatelessWidget {
  2.  
  3. final GlobalKey globalKey = GlobalKey();
  4.  
  5. void _getWH() {
  6. final containerWidth = globalKey.currentContext.size.width;
  7. final containerHeight = globalKey.currentContext.size.height;
  8. print('Container widht is $containerWidth, height is $containerHeight');
  9. }
  10.  
  11. @override
  12. Widget build(BuildContext context) {
  13. final size = MediaQuery.of(context).size;
  14. final width = size.width;
  15. final height = size.height;
  16. print('width is $width; height is $height');
  17. return Scaffold(
  18. appBar: AppBar(
  19. title: Text('Width & Height'),
  20. ),
  21. body: Center(
  22. child: Container(
  23. key: globalKey,
  24. color: Colors.redAccent,
  25. width: width / 2,
  26. height: height / 2,
  27. ),
  28. ),
  29. floatingActionButton: FloatingActionButton(
  30. onPressed: _getWH,
  31. child: Icon(Icons.adjust),
  32. ),
  33. );
  34. }
  35. }

上述代码中,我们将声明的 globalKey 设置给了 Container , 当我们点击页面中的 FloatingActionButton 的时候,就会使用 globalKey 来获取 Container 的宽高,也就是_getWH() 中执行的代码。

运行结果及输出如下:

flutter: Container widht is 207.0, height is 448.0

如果错误,还请指出,谢谢

完整源码

参考链接

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对w3xue的支持。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号