2022-10-06 07:38:29 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/scheduler.dart';
|
|
|
|
|
|
|
|
class WidgetSize extends StatefulWidget {
|
|
|
|
const WidgetSize({
|
|
|
|
required this.onChange,
|
|
|
|
required this.child,
|
2022-10-26 16:26:09 +00:00
|
|
|
super.key,
|
2022-10-06 07:38:29 +00:00
|
|
|
});
|
|
|
|
final Widget child;
|
|
|
|
final Function onChange;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<WidgetSize> createState() => _WidgetSizeState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _WidgetSizeState extends State<WidgetSize> {
|
|
|
|
@override
|
|
|
|
Widget build(final BuildContext context) {
|
|
|
|
SchedulerBinding.instance.addPostFrameCallback(postFrameCallback);
|
|
|
|
return Container(
|
|
|
|
key: widgetKey,
|
|
|
|
child: widget.child,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
var widgetKey = GlobalKey();
|
|
|
|
Size? oldSize;
|
|
|
|
|
2022-10-26 16:26:09 +00:00
|
|
|
void postFrameCallback(final _) {
|
2022-10-06 07:38:29 +00:00
|
|
|
final context = widgetKey.currentContext;
|
|
|
|
if (context == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final newSize = context.size;
|
|
|
|
if (oldSize == newSize) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
oldSize = newSize;
|
|
|
|
widget.onChange(newSize);
|
|
|
|
}
|
|
|
|
}
|