当前位置:网站首页>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;
}边栏推荐
- 零基础自学SQL课程 | SQL中的日期函数大全
- HTTP Caching Protocol practice
- 关于杠杆的思考
- Binomial distribution (a discrete distribution)
- AI deep dive of Huawei cloud
- How do independent site sellers efficiently manage complex Facebook pages?
- Recommend two high-quality Wallpaper software
- 在亿学学堂开通证券账户是安全可靠的吗?
- Leetcode: merge two ordered linked lists_ twenty-one
- wrk压力测试工具介绍
猜你喜欢

An artifact extracted from a well-known software and paid by a group of people

Webrtc audio and video development - experience

BOE was brilliant for the Winter Olympics, but revealed another Chinese technology enterprise dominating the world

16 `bs object Node name Div. attribute contents ` children descendants get child nodes and descendants
![Sword finger offer:[day 1 stack and queue (simple)] --- > stack containing min function](/img/16/2edfc478a56e5b5e7299621ac778c2.jpg)
Sword finger offer:[day 1 stack and queue (simple)] --- > stack containing min function
![Sword finger offer:[day 2 linked list (simple)] --- > print the linked list from end to end](/img/d6/824985b74b27a1bee456c2cebbac26.jpg)
Sword finger offer:[day 2 linked list (simple)] --- > print the linked list from end to end
![[software test] 2022 national unified college enrollment examination](/img/9a/d76d7eb30a097d364fef28c2230e1a.png)
[software test] 2022 national unified college enrollment examination

Alist+raidrive gives the computer a complete 8billion GB hard disk drive

Construction and application of urban brain knowledge map

Binomial distribution (a discrete distribution)
随机推荐
LeetCode1114. 按序打印
电商秒杀系统架构设计
Why use the rust language?
Deep interpretation of WiFi security vulnerability krack
Sword finger offer:[day 2 linked list (simple)] --- > print the linked list from end to end
Postman introduction and installation steps
LeetCode122. The best time to buy and sell stocks II
Real time transformer: meituan's research on single image depth estimation
Golang JSON serializing and deserializing strings deserializing to map[string]interface{}
[linq] execute SQL like in statements using EF to LINQ
为什么要使用 Rust 语言?
Interface test process
LeetCode121. 买卖股票的最佳时机
Smarca2 antibody study: abnova smarca2 monoclonal antibody protocol
Binomial distribution (a discrete distribution)
CVPR 2022|极具创意&美感的文字生成方法!支持任意输入
After reading the list of global patent and chip buyers, I understand that high innovation can lead to high profits
[software test] 2022 national unified college enrollment examination
Architecture design of e-commerce secsha system
小样本利器2.文本对抗+半监督 FGSM & VAT & FGM代码实现