如何在Flutter中获取小部件在屏幕上的绝对坐标?

Iho*_*mov 5 dart flutter

如何在Flutter中获取小部件在屏幕上的绝对坐标?

或其在父级中的偏移量

例:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Simple app',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SimpleScreen(
        title: 'Simple screen',
      ),
    );
  }
}

class SimpleScreen extends StatefulWidget {
  final String title;

  SimpleScreen({Key key, this.title}) : super(key: key);

  @override
  _SimpleScreenState createState() => new _SimpleScreenState();
}

class _SimpleScreenState extends State<SimpleScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Container(
          width: 48.0,
          height: 48.0,
          color: Colors.blue,
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

目标是获得Container在父级中的偏移量。如果Center的大小为48.0,则Container在其父级/根目录中的偏移量

条件:您不知道包装器的布局(它是一个库窗口小部件),应该灵活,没有硬编码的值

谢谢

vov*_*ost 25

你可以使用我写的这个扩展(需要 Dart 2.6):

extension GlobalKeyExtension on GlobalKey {
  Rect get globalPaintBounds {
    final renderObject = currentContext?.findRenderObject();
    var translation = renderObject?.getTransformTo(null)?.getTranslation();
    if (translation != null && renderObject.paintBounds != null) {
      return renderObject.paintBounds
          .shift(Offset(translation.x, translation.y));
    } else {
      return null;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

示例如何使用它:

final containerKey = GlobalKey();

Container(
  key: containerKey,
  width: 100,
  height: 50,
)

void printWidgetPosition() {
  print('absolute coordinates on screen: ${containerKey.globalPaintBounds}');
}
Run Code Online (Sandbox Code Playgroud)


Kar*_*yan 9

我更改了 @vovahost 扩展函数,因为它在转换后的小部件中不起作用(例如内部的小部件RotatedBox)。这适用于所有小部件。

extension GlobalKeyExtension on GlobalKey {
  Rect? get globalPaintBounds {
    final renderObject = currentContext?.findRenderObject();
    final matrix = renderObject?.getTransformTo(null);

    if (matrix != null && renderObject?.paintBounds != null) {
      final rect = MatrixUtils.transformRect(matrix, renderObject!.paintBounds);
      return rect;
    } else {
      return null;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 8

根据文档,使用GlobalKey相对昂贵。你可以只使用 Element( BuildContext) 来计算它的全局边界。我改进了@vovahost上面的解决方案:

extension GlobalPaintBounds on BuildContext {
  Rect? get globalPaintBounds {
    final renderObject = findRenderObject();
    final translation = renderObject?.getTransformTo(null).getTranslation();
    if (translation != null && renderObject?.paintBounds != null) {
      final offset = Offset(translation.x, translation.y);
      return renderObject!.paintBounds.shift(offset);
    } else {
      return null;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)