Flutter开发:获取屏幕及 Widget 宽高两种方法

2019-03-2717:19:27APP与小程序开发Comments4,857 views字数 3030阅读模式

开发中的过程中通常都会获取屏幕或者 widget 的宽高用来做一些事情,在 Flutter 中,我们有两种方法来获取 widget 的宽高。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

MediaQuery

一般情况下,我们会使用如下方式去获取 widget 的宽高:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

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

但是如果不注意,这种写法很容易报错,例如下面的写法就会报错:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

import 'package:flutter/';

class GetWidgetWidthAndHeiget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final size =(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 的宽和高,但上述代码并不能成功运行,会报如下错误:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

flutter: The following assertion was thrown building GetWidgetWidthAndHeiget(dirty):
flutter: () 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 ().
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.

从错误异常中我们可以大概了解到有两种情况会导致上述异常:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

  1. 当没有 WidgetsApp or MaterialApp 的时候,我们使用 (context) 来获取数据。
  2. 当我们在当前小部件中使用了上一个小部件的 context,来使用 (context) 获取数据的时候。

我们上述的代码很显然是属于第一种情况,也就是说我们在使用 (context) 的地方并没有一个 WidgetsApp or MaterialApp 来提供数据。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

解决方法就是将 (context) 挪到 MaterialApp 内,如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

import 'package:flutter/';

class GetWidgetWidthAndHeiget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final size = (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,
        ),
      ),
    );
  }
}

运行效果及输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

flutter: width is 414.0; height is 896.0

上述代码中,我们获取的是 MaterialApp 的宽高,也就是屏幕的宽高文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

image文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

那么如果我们要需要知道上述红色的 Container 容器的宽高怎么办呢?这里我们可以使用 GlobalKey文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

GlobalKey

使用 GlobalKey 的步骤如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

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

修改过后的 HomePage 代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

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 = (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(),
      ),
    );
  }
}

上述代码中,我们将声明的 globalKey 设置给了 Container , 当我们点击页面中的 FloatingActionButton 的时候,就会使用 globalKey 来获取 Container 的宽高,也就是
_getWH() 中执行的代码。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

运行结果及输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html

flutter: Container widht is 207.0, height is 448.0
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10471.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/xcx/10471.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定