From c9d31a2b7974bf4e2a9083dfd03f8825ac2c586c Mon Sep 17 00:00:00 2001 From: mzelldev <64581392+mzelldev@users.noreply.github.com> Date: Thu, 15 Feb 2024 09:30:18 -0800 Subject: [PATCH] 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. --- lib/src/size_detector_widget.dart | 7 +------ lib/src/sized_pageview.dart | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/src/size_detector_widget.dart b/lib/src/size_detector_widget.dart index e4f7c09..f3117dd 100644 --- a/lib/src/size_detector_widget.dart +++ b/lib/src/size_detector_widget.dart @@ -18,14 +18,9 @@ class SizeDetectorWidget extends StatefulWidget { class _SizeDetectorWidgetState extends State { Size? _oldSize; - @override - void initState() { - super.initState(); - SchedulerBinding.instance?.addPostFrameCallback((_) => _detectSize()); - } - @override Widget build(BuildContext context) { + SchedulerBinding.instance?.addPostFrameCallback((_) => _detectSize()); return widget.child; } diff --git a/lib/src/sized_pageview.dart b/lib/src/sized_pageview.dart index 4c609f0..a98c9f9 100644 --- a/lib/src/sized_pageview.dart +++ b/lib/src/sized_pageview.dart @@ -21,8 +21,7 @@ class SizedPageView extends StatefulWidget { _SizedPageViewState createState() => _SizedPageViewState(); } -class _SizedPageViewState extends State - with TickerProviderStateMixin { +class _SizedPageViewState extends State with TickerProviderStateMixin { late List _heights; int _currentIndex = 0; @@ -32,14 +31,13 @@ class _SizedPageViewState extends State void initState() { super.initState(); _heights = List.generate(widget.children.length, (index) => 0.0); + final int initialIndex = widget.pageController.initialPage; + _setCurrentIndex(initialIndex); widget.pageController.addListener(() { - final _newIndex = widget.pageController.page?.round(); - if (_currentIndex != _newIndex) { - if (!mounted) { - return; - } - setState(() => _currentIndex = _newIndex!); + final int? newIndex = widget.pageController.page?.round(); + if (newIndex != null) { + _setCurrentIndex(newIndex); } }); } @@ -79,4 +77,13 @@ class _SizedPageViewState extends State widget.pageController.dispose(); super.dispose(); } + + void _setCurrentIndex(int newIndex) { + if (_currentIndex != newIndex) { + if (!mounted) { + return; + } + setState(() => _currentIndex = newIndex); + } + } }