久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Flutter中獲取屏幕及Widget的寬高示例代碼

這篇文章主要給大家介紹了關(guān)于Flutter中如何獲取屏幕及Widget的寬高的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用Flutter具有一定的參考學習價值,需要的朋友們

前言

我們平時在開發(fā)中的過程中通常都會獲取屏幕或者 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) 來獲取數(shù)據(jù)。
  • 當我們在當前小部件中使用了上一個小部件的 context,來使用 MediaQuery.of(context) 獲取數(shù)據(jù)的時候。

我們上述的代碼很顯然是屬于第一種情況,也就是說我們在使用 MediaQuery.of(context) 的地方并沒有一個 WidgetsApp or MaterialApp 來提供數(shù)據(jù)。

解決方法就是將 MediaQuery.of(context) 挪到 MaterialApp 內(nèi),如下:


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 設(shè)置 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 設(shè)置給了 Container , 當我們點擊頁面中的 FloatingActionButton 的時候,就會使用 globalKey 來獲取 Container 的寬高,也就是_getWH() 中執(zhí)行的代碼。

運行結(jié)果及輸出如下:

flutter: Container widht is 207.0, height is 448.0

如果錯誤,還請指出,謝謝

完整源碼

參考鏈接

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對html5模板網(wǎng)的支持。

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

主站蜘蛛池模板: 久久久婷婷 | 综合色婷婷 | 欧美一卡二卡在线观看 | 91高清在线视频 | 青青草综合| 亚洲精品视频在线看 | 成人激情视频在线观看 | 亚洲人成人一区二区在线观看 | 亚洲一区中文字幕 | 欧美一区二区三区久久精品 | 青青草精品 | 人人艹人人爽 | 色女人天堂| 国产精品色哟哟网站 | 国产精品视频网站 | 91高清在线观看 | 九九九久久国产免费 | 国产精品日日夜夜 | 日韩高清一区 | 日韩欧美网 | 欧美日韩看片 | 欧美综合自拍 | 青青久久| 精品欧美一区二区在线观看欧美熟 | 久久久不卡网国产精品一区 | 999精品视频在线观看 | 午夜免费在线电影 | 欧美亚洲综合久久 | 精品亚洲第一 | 99视频在线 | www精品美女久久久tv | 欧美1区 | 婷婷色网| 日韩高清在线 | 欧美一级片在线 | 中文字幕精品一区二区三区在线 | 久久久久久国产精品久久 | 精品少妇一区二区三区日产乱码 | 欧美在线一区二区视频 | 精品欧美一区二区精品久久 | 成人久久久久 |