selfprivacy.org.app/lib/ui/helpers/widget_size.dart

45 lines
926 B
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
class WidgetSize extends StatefulWidget {
const WidgetSize({
required this.onChange,
required this.child,
super.key,
});
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;
void postFrameCallback(final _) {
final context = widgetKey.currentContext;
if (context == null) {
return;
}
final newSize = context.size;
if (oldSize == newSize) {
return;
}
oldSize = newSize;
widget.onChange(newSize);
}
}