前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flutter中获取屏幕及Widget的宽高示例代码

Flutter中获取屏幕及Widget的宽高示例代码

作者头像
砸漏
发布2020-11-04 10:04:52
3.1K0
发布2020-11-04 10:04:52
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

前言

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

MediaQuery

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

代码语言:javascript
复制
final size =MediaQuery.of(context).size;
final width =size.width;
final height =size.height; 

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

代码语言:javascript
复制
import 'package:flutter/material.dart';

class GetWidgetWidthAndHeiget 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 MaterialApp(
  home: Scaffold(
  appBar: AppBar(
   title: Text('Width & Height'),
  ),
  body: Container(
   width: width / 2,
   height: height / 2,
  ),
  ),
 );
 }
}

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

代码语言:javascript
复制
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 的宽高,也就是屏幕的宽高

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

GlobalKey

使用 GlobalKey 的步骤如下:

  • 声明一个 GlobalKey final GlobalKey globalKey = GlobalKey();
  • 给 widget 设置 GlobalKey key: globalKey
  • 通过 globalKey 来获取该 widget 的 size
代码语言:javascript
复制
final containerWidth = globalKey.currentContext.size.width;
final containerHeight = globalKey.currentContext.size.height;
print('Container widht is $containerWidth, height is $containerHeight');

修改过后的 HomePage 代码如下:

代码语言:javascript
复制
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,
   color: Colors.redAccent,
   width: width / 2,
   height: height / 2,
  ),
  ),
  floatingActionButton: FloatingActionButton(
  onPressed: _getWH,
  child: Icon(Icons.adjust),
  ),
 );
 }
}

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

运行结果及输出如下:

flutter: Container widht is 207.0, height is 448.0

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

完整源码

参考链接

总结

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档