Skip to content

A flutter demo project to detect offset and size by context or global key

Notifications You must be signed in to change notification settings

sabikrahat/offset_and_size_calculate

Repository files navigation

offset_and_size_calculate

A new Flutter project of indentify the offset and size of the widget.

Sample UI

Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter development, view the online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

  • Code Snippet
import 'package:flutter/material.dart';

class WidgetPosition {
  final Size size;
  final Offset offset;

  WidgetPosition({
    required this.size,
    required this.offset,
  });

  double get dx => offset.dx;

  double get dy => offset.dy;

  double get width => size.width;

  double get height => size.height;
}

WidgetPosition getWidgetPositionByGlobalKey(GlobalKey key) {
  final box = key.currentContext!.findRenderObject() as RenderBox;
  final position = box.localToGlobal(Offset.zero);
  return WidgetPosition(size: box.size, offset: position);
}

WidgetPosition getWidgetPositionFromContext(BuildContext context) {
  final box = context.findRenderObject() as RenderBox;
  final position = box.localToGlobal(Offset.zero);
  return WidgetPosition(size: box.size, offset: position);
}

About

A flutter demo project to detect offset and size by context or global key

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published