autoscale_tabbarview/lib/src/size_detector_widget.dart
mzelldev c9d31a2b79 Fix for non-zero initial tab index not sizing children correctly and device orientation change support
This commit fixes two issues:
 - When the tabController.index was initial set to something > 0, the height for the children was not being calculated correctly.
 - When changing the device orientation (portrait/landscape) the height was not being updated.
2024-02-15 09:30:18 -08:00

38 lines
841 B
Dart

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
class SizeDetectorWidget extends StatefulWidget {
final Widget child;
final ValueChanged<Size> onSizeDetect;
const SizeDetectorWidget({
Key? key,
required this.child,
required this.onSizeDetect,
}) : super(key: key);
@override
_SizeDetectorWidgetState createState() => _SizeDetectorWidgetState();
}
class _SizeDetectorWidgetState extends State<SizeDetectorWidget> {
Size? _oldSize;
@override
Widget build(BuildContext context) {
SchedulerBinding.instance?.addPostFrameCallback((_) => _detectSize());
return widget.child;
}
void _detectSize() {
if (!mounted) {
return;
}
final size = context.size;
if (_oldSize != size) {
_oldSize = size;
widget.onSizeDetect(size!);
}
}
}