当前位置:网站首页>flutter通过 GlobalKey 获取界面任意元素坐标尺寸
flutter通过 GlobalKey 获取界面任意元素坐标尺寸
2022-06-28 21:41:00 【一叶飘舟】
最初:
需求开发中遇到要获取屏幕任意元素坐标尺寸的功能,最终实现如下:
1. example:
//...
GlobalKey _globalKey = GlobalKey();
@override
Widget build(BuildContext context) {
//...
ElevatedButton.icon(
icon: Icon(Icons.send),
label: Text("发送"),
// onPressed: () => ddlog('pressed'),
key: _globalKey,
onPressed: (){
// _showCustomPopView();
ddlog(_globalKey.position());// Offset(143.5, 117.0)
ddlog(_globalKey.size);// Size(88.0, 48.0)
},
),
//...
}
2. extension 封装:
import 'package:flutter/material.dart';
extension BuildContextExt on BuildContext {
/// 获取当前组件的 RenderBox
RenderBox? renderBox() {
return this.findRenderObject() is RenderBox ? (this.findRenderObject() as RenderBox) : null;
}
/// 获取当前组件的 position
Offset? position({Offset offset = Offset.zero}) {
return this.renderBox()?.localToGlobal(offset);
}
}
extension GlobalKeyExt on GlobalKey{
/// 获取当前组件的 RenderBox
RenderBox? renderBox() => this.currentContext?.renderBox();
/// 获取当前组件的 position
Offset? position({Offset offset = Offset.zero}) => this.currentContext?.position(offset: offset);
/// 获取当前组件的 Size
Size? get size => this.currentContext?.size;
}边栏推荐
- Wechat applet realizes left sliding deletion
- LeetCode226. 翻转二叉树
- Leetcode: merge two ordered linked lists_ twenty-one
- QT 一个控件的坐标怎么相对固定显示在另一个控件上(坐标系)
- Live broadcast preview | can SQL also play industrial machine learning? Mlops meetup V3 takes you to the bottom!
- Leetcode: merge K ascending linked lists_ twenty-three
- Study on luminiprobe non fluorescent azide -- 3-azido propanol
- 构建实战化防御体系之立体防渗透
- Binomial distribution (a discrete distribution)
- 为什么要使用 Rust 语言?
猜你喜欢

CVPR 2022|极具创意&美感的文字生成方法!支持任意输入

docker下载Mysql镜像创建数据库链接时候发生密码错误问题

Security dilemma of NFT liquidity agreement - Analysis of the hacked event of NFT loan agreement xcarnival
![[webapi] return dynamic list dynamic](/img/83/b0b36ddab6d74ccd89811cbb58d051.jpg)
[webapi] return dynamic list dynamic
![[width first search note] BFS output shortest path](/img/b5/553ca241ae571daecabd282f33f20a.jpg)
[width first search note] BFS output shortest path

华为云的AI深潜之旅

Survival of beaver: post-90s female doctors and AI developers

PE file-

Lumiprobe protein labeling research scheme

E-commerce is popular, how to improve the store conversion rate?
随机推荐
#yyds干货盘点# 解决剑指offer: 连续子数组的最大和(二)
Understanding web automated testing
docker下载Mysql镜像创建数据库链接时候发生密码错误问题
Hardware development notes (VII): basic process of hardware development, making a USB to RS232 module (VI): creating 0603 package and associating principle graphic devices
[width first search note] BFS output shortest path
Definition and precautions of genuine St link/v2 j-link jtag/swd pin
Manual backup and restore of DHCP server
LeetCode188. 买卖股票的最佳时机IV
PE file-
Anti rabbit dylight 488 abbkine universal immunofluorescence (if) toolbox
Zero foundation self-study SQL course | complete collection of date functions in SQL
Progress in visual weakly supervised learning
直播预告|SQL也能玩转工业级机器学习?MLOps meetup V3带你一探究竟!
QJsonObject的使用示例
LeetCode122. The best time to buy and sell stocks II
华为云的AI深潜之旅
Multinomial distribution (a discrete distribution)
Ctrip will push the "3+2" working mode: 3 days a week on duty and 2 days of free choice of office space. Do you envy it?
安全 创新 实践|海泰方圆受邀参加“数字时代的网信创新与价值共创”技术交流研讨会
After reading the list of global patent and chip buyers, I understand that high innovation can lead to high profits