import 'package:flutter/cupertino.dart';

/// 组件位置工具类
class WidgetLocationUtil {
  /// 获取组件的位置信息
  static WidgetInfo getWidgetLocation(GlobalKey key) {
    final context = key.currentContext;

    if (context == null) {
      throw Exception('Context is null');
    }

    final renderObject = context.findRenderObject();
    if (renderObject == null || renderObject is! RenderBox) {
      throw Exception('RenderObject is null or not a RenderBox');
    }

    final offset = renderObject.localToGlobal(Offset.zero);
    final size = renderObject.size;

    return WidgetInfo(offset, size);
  }
}

/// 组件信息
class WidgetInfo {
  // 位置
  final Offset position;

  // 大小
  final Size size;

  WidgetInfo(this.position, this.size);

  @override
  String toString() {
    return 'WidgetInfo{position: $position, size: $size}';
  }
}
