nc-photos/app/lib/widget/measure.dart

77 lines
1.8 KiB
Dart
Raw Normal View History

2021-05-19 21:11:06 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
2021-09-15 08:58:06 +02:00
typedef OnWidgetSizeChanged = void Function(Size size);
2021-05-19 21:11:06 +02:00
/// See: https://stackoverflow.com/a/60868972
class MeasureSize extends SingleChildRenderObjectWidget {
final OnWidgetSizeChanged onChange;
const MeasureSize({
2021-07-23 22:05:57 +02:00
Key? key,
required this.onChange,
required Widget child,
2021-05-19 21:11:06 +02:00
}) : super(key: key, child: child);
@override
RenderObject createRenderObject(BuildContext context) {
return _MeasureSizeRenderObject(onChange);
}
}
class _MeasureSizeRenderObject extends RenderProxyBox {
2021-07-23 22:05:57 +02:00
Size? oldSize;
2021-05-19 21:11:06 +02:00
final OnWidgetSizeChanged onChange;
_MeasureSizeRenderObject(this.onChange);
@override
void performLayout() {
super.performLayout();
2021-07-23 22:05:57 +02:00
var newSize = child?.size;
if (newSize == null || oldSize == newSize) return;
2021-05-19 21:11:06 +02:00
oldSize = newSize;
2022-06-20 13:49:58 +02:00
WidgetsBinding.instance.addPostFrameCallback((_) {
2021-05-19 21:11:06 +02:00
onChange(newSize);
});
}
}
class SliverMeasureExtent extends SingleChildRenderObjectWidget {
const SliverMeasureExtent({
2021-07-23 22:05:57 +02:00
Key? key,
required this.onChange,
required Widget child,
2021-05-19 21:11:06 +02:00
}) : super(key: key, child: child);
@override
RenderObject createRenderObject(BuildContext context) {
return _SliverMeasureExtentRenderObject(onChange);
}
final void Function(double) onChange;
}
class _SliverMeasureExtentRenderObject extends RenderProxySliver {
_SliverMeasureExtentRenderObject(this.onChange);
@override
void performLayout() {
super.performLayout();
2021-07-23 22:05:57 +02:00
var newExent = child?.geometry?.scrollExtent;
if (newExent == null || _oldExtent == newExent) {
2021-05-19 21:11:06 +02:00
return;
}
_oldExtent = newExent;
2022-06-20 13:49:58 +02:00
WidgetsBinding.instance.addPostFrameCallback((_) => onChange(newExent));
2021-05-19 21:11:06 +02:00
}
final void Function(double) onChange;
2021-07-23 22:05:57 +02:00
double? _oldExtent;
2021-05-19 21:11:06 +02:00
}