add centering and focus capabilities
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user