码迷,mamicode.com
首页 > 其他好文 > 详细

Flutter 获取控件尺寸和位置

时间:2019-03-26 13:15:18      阅读:1579      评论:0      收藏:0      [点我收藏+]

标签:current   final   python   print   off   示例   xtend   uil   信息   

 

1. 插件必须渲染好,

final RenderBox box = globalKey.currentContext.findRenderObject();
final size = box.size;
final topLeftPosition = box.localToGlobal(Offset.zero);
return topLeftPosition.dy;

  

2. 可以通过context.size获取当前控件的尺寸和位置offset信息

下面是示例,通过contex.size.height可以拿到child控件的高度

 

class HeightReporter extends StatelessWidget {
  final Widget child;

  HeightReporter({this.child});

  @override
  Widget build(BuildContext context) {
    return new GestureDetector(
      child: child,
      onTap: () {
        print(‘Height is ${context.size.height}‘);
      },
    );
  }

  

 

Flutter 获取控件尺寸和位置

标签:current   final   python   print   off   示例   xtend   uil   信息   

原文地址:https://www.cnblogs.com/pythonClub/p/10599324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!