48 lines
1.5 KiB
Dart
48 lines
1.5 KiB
Dart
import 'package:flutter/rendering.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
/// Reports the laid-out size of its real child.
|
|
///
|
|
/// The definition graph measures the very node and label widgets it renders
|
|
/// instead of assuming a height, so a wrapped key, a larger text scale or a
|
|
/// platform host all produce the honest extent. The child is laid out with the
|
|
/// constraints its caller gives, and [onSize] runs during layout.
|
|
///
|
|
/// Because it runs inside layout, a caller must not rebuild synchronously:
|
|
/// record the size and schedule the rebuild for after the frame. The size is
|
|
/// reported again only when it actually changes, so a caller that keeps its
|
|
/// reported sizes stable does not loop.
|
|
class MeasuredChild extends SingleChildRenderObjectWidget {
|
|
const MeasuredChild({super.key, required this.onSize, required super.child});
|
|
|
|
/// Receives the child's size, in logical pixels, after every size change.
|
|
final ValueChanged<Size> onSize;
|
|
|
|
@override
|
|
MeasuredBox createRenderObject(BuildContext context) =>
|
|
MeasuredBox(onSize: onSize);
|
|
|
|
@override
|
|
void updateRenderObject(BuildContext context, MeasuredBox renderObject) {
|
|
renderObject.onSize = onSize;
|
|
}
|
|
}
|
|
|
|
/// The proxy render object behind [MeasuredChild].
|
|
class MeasuredBox extends RenderProxyBox {
|
|
MeasuredBox({required this.onSize});
|
|
|
|
ValueChanged<Size> onSize;
|
|
|
|
Size? _reported;
|
|
|
|
@override
|
|
void performLayout() {
|
|
super.performLayout();
|
|
final measured = size;
|
|
if (measured == _reported) return;
|
|
_reported = measured;
|
|
onSize(measured);
|
|
}
|
|
}
|