widget_utils.dart 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. part of cool_ui;
  2. class WidgetUtils {
  3. bool _hasMeasured = false;
  4. double _width;
  5. double _height;
  6. /// Widget rendering listener.
  7. /// Widget渲染监听
  8. /// context: Widget context
  9. /// isOnce: true,Continuous monitoring false,Listen only once.
  10. /// onCallBack: Widget Rect CallBack
  11. void asyncPrepare(
  12. BuildContext context, bool isOnce, ValueChanged<Rect> onCallBack) {
  13. if (_hasMeasured) return;
  14. WidgetsBinding.instance.addPostFrameCallback((Duration timeStamp) {
  15. RenderBox box = context.findRenderObject();
  16. if (box != null && box.semanticBounds != null) {
  17. if (isOnce) _hasMeasured = true;
  18. double width = box.semanticBounds.width;
  19. double height = box.semanticBounds.height;
  20. if (_width != width || _height != height) {
  21. _width = width;
  22. _height = height;
  23. if (onCallBack != null) onCallBack(box.semanticBounds);
  24. }
  25. }
  26. });
  27. }
  28. ///get Widget Bounds (width, height, left, top, right, bottom and so on).Widgets must be rendered completely.
  29. ///获取widget Rect
  30. static Rect getWidgetBounds(BuildContext context) {
  31. RenderBox box = context.findRenderObject();
  32. return (box != null && box.semanticBounds != null)
  33. ? box.semanticBounds
  34. : Rect.zero;
  35. }
  36. ///Get the coordinates of the widget on the screen.Widgets must be rendered completely.
  37. ///获取widget在屏幕上的坐标,widget必须渲染完成
  38. static Offset getWidgetLocalToGlobal(BuildContext context) {
  39. RenderBox box = context.findRenderObject();
  40. return box == null ? Offset.zero : box.localToGlobal(Offset.zero);
  41. }
  42. }