-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmeasured_size.dart
56 lines (46 loc) · 1.55 KB
/
measured_size.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
typedef void OnWidgetSizeChange(Size size);
/// [MeasuredSize] Calculated the size of it's child in runtime.
/// Simply wrap your widget with [MeasuredSize] and listen to size changes with [onChange].
class MeasuredSize extends StatefulWidget {
/// Widget to calculate it's size.
final Widget child;
/// [onChange] will be called when the [Size] changes.
/// [onChange] will return the value ONLY once if it didn't change, and it will NOT return a value if it's equals to [Size.zero]
final OnWidgetSizeChange onChange;
const MeasuredSize({
Key? key,
required this.onChange,
required this.child,
}) : super(key: key);
@override
_MeasuredSizeState createState() => _MeasuredSizeState();
}
class _MeasuredSizeState extends State<MeasuredSize> {
@override
void initState() {
SchedulerBinding.instance!.addPostFrameCallback(postFrameCallback);
super.initState();
}
@override
Widget build(BuildContext context) {
SchedulerBinding.instance!.addPostFrameCallback(postFrameCallback);
return Container(
key: widgetKey,
child: widget.child,
);
}
var widgetKey = GlobalKey();
var oldSize;
void postFrameCallback(_) async {
var context = widgetKey.currentContext!;
await Future.delayed(
Duration(milliseconds: 100)); // wait till the image is drawn
Size newSize = context.size!;
if (newSize == Size.zero) return;
if (oldSize == newSize) return;
oldSize = newSize;
widget.onChange(newSize);
}
}