Flutter 获取控件尺寸和位置

Posted pythonclub

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter 获取控件尺寸和位置相关的知识,希望对你有一定的参考价值。

 

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 获取控件尺寸和位置的主要内容,如果未能解决你的问题,请参考以下文章