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;
|
||||
}
|
||||
Reference in New Issue
Block a user