add centering and focus capabilities

This commit is contained in:
2025-07-06 03:53:18 +00:00
parent b18ef006af
commit 10bb69a15f
8 changed files with 224 additions and 46 deletions
+28
View File
@@ -0,0 +1,28 @@
import 'package:flutter/material.dart';
/// Utility to synchronously measure the size of a widget by building it offscreen in an Overlay.
/// Only works for widgets with deterministic, constraint-based sizing (no async/layout dependencies).
Size measureWidgetSize(BuildContext context, WidgetBuilder builder, {BoxConstraints? constraints}) {
final key = GlobalKey();
final overlay = Overlay.of(context);
Size? measuredSize;
final widget = Offstage(
child: ConstrainedBox(
constraints: constraints ?? const BoxConstraints(),
child: Container(key: key, child: builder(context)),
),
);
final entry = OverlayEntry(builder: (_) => widget);
overlay.insert(entry);
WidgetsBinding.instance.handleDrawFrame();
final ctx = key.currentContext;
if (ctx != null) {
measuredSize = (ctx.findRenderObject() as RenderBox).size;
}
entry.remove();
return measuredSize ?? Size.zero;
}
+19
View File
@@ -8,10 +8,29 @@ extension OffsetCartesionOps on Offset {
return dx * dx + dy * dy;
}
Offset operator +(Offset other) {
return Offset(dx + other.dx, dy + other.dy);
}
Offset operator -(Offset other) {
return Offset(dx - other.dx, dy - other.dy);
}
Offset operator *(double scalar) {
return Offset(dx * scalar, dy * scalar);
}
Offset makeAtleast(double value) {
return Offset(dx < value ? value : dx, dy < value ? value : dy);
}
Offset operator /(double scalar) {
if (scalar == 0) {
throw ArgumentError('Division by zero is not allowed.');
}
return Offset(dx / scalar, dy / scalar);
}
Point toPoint() {
return Point(dx, dy);
}
+18
View File
@@ -0,0 +1,18 @@
import 'package:flutter/material.dart';
extension SizeOps on Size {
Size operator /(double scalar) {
if (scalar == 0) {
throw ArgumentError('Division by zero is not allowed.');
}
return Size(width / scalar, height / scalar);
}
Size operator *(double scalar) {
return Size(width * scalar, height * scalar);
}
Offset toOffset() {
return Offset(width, height);
}
}