You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.3 KiB
Dart
54 lines
1.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/scheduler.dart';
|
|
|
|
class MeasurableWidget extends StatefulWidget {
|
|
final Widget child;
|
|
|
|
Function? triggerMeasure;
|
|
final Function(Size? size, Offset? position) onChange;
|
|
|
|
MeasurableWidget({super.key, required this.onChange, required this.child});
|
|
|
|
@override
|
|
_WidgetSizeState createState() => _WidgetSizeState();
|
|
}
|
|
|
|
class _WidgetSizeState extends State<MeasurableWidget> {
|
|
var widgetKey = GlobalKey();
|
|
Offset? oldPosition;
|
|
@override
|
|
void initState() {
|
|
// TODO: implement initState
|
|
super.initState();
|
|
}
|
|
|
|
void postFrameCallback(_) {
|
|
final context = widgetKey.currentContext;
|
|
if (context == null) return;
|
|
|
|
final newSize = context.size;
|
|
|
|
final RenderBox? box =
|
|
widgetKey.currentContext?.findRenderObject() as RenderBox?;
|
|
final Offset? position = box?.localToGlobal(Offset.zero);
|
|
|
|
if (oldPosition != null) {
|
|
if (oldPosition!.dx == position!.dx && oldPosition!.dy == position.dy) {
|
|
return;
|
|
}
|
|
}
|
|
oldPosition = position;
|
|
|
|
widget.onChange(newSize, position);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
SchedulerBinding.instance.addPostFrameCallback(postFrameCallback);
|
|
return Container(
|
|
key: widgetKey,
|
|
child: widget.child,
|
|
);
|
|
}
|
|
}
|