当前位置: 移动技术网 > IT编程>移动开发>Android > Flutter中获取屏幕及Widget的宽高示例代码

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

2019年07月23日  | 移动技术网IT编程  | 我要评论

丁俊晖vs特鲁姆普2016,狮子山下菊带霜,3U8815航班

前言

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

mediaquery

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

final size =mediaquery.of(context).size;
final width =size.width;
final height =size.height; 

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

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 内,如下:

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
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,
   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

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


总结

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网