Flutter 拓展之GlobalKey

本文介绍了一个针对Flutter框架中GlobalKey类的拓展方法集合,通过这些拓展方法可以更方便地获取元素的位置和尺寸信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//  @Description: GlobalKey拓展
//  @Author: 陈胜辉
//  @Date: 2019-12-26 15:37:34
//  @Version: 版本号, YYYY-MM-DD
//  @LastEditors: 陈胜辉
//  @LastEditTime: 2019-12-26 16:07:28
//  @Deprecated: 否
//  备注
import 'package:flutter/material.dart';

extension SHExtension on GlobalKey {
  static Offset offset = Offset.zero;

  void setOffset(Offset off) {
    offset = off;
  }

  RenderBox _render() {
    if (this.currentContext != null) {
      return this.currentContext.findRenderObject();
    }
    return null;
  }

  double x() {
    return this.origin().dx;
  }

  double y() {
    return this.origin().dy;
  }

  double width() {
    return this.size().width;
  }

  double height() {
    return this.size().height;
  }

  Offset origin() {
    return this._render().localToGlobal(offset);
  }

  Size size() {
    return this._render().size;
  }

  double maxX() {
    return this.x() + this.width();
  }

  double maxY() {
    return this.y() + this.height();
  }

  double centerX() {
    return this.x() + (this.width() / 2);
  }

  double centerY() {
    return this.y() + (this.height() / 2);
  }

  String debugLabel() {
    String temp = this.toString();
    if (temp.contains(' ')) {
      List list = temp.split(' ');
      String str = list.last;
      return str.split(']').first;
    }
    return '';
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值