Protect details screen from bad tabIndex params

This commit is contained in:
Shawn 2024-01-17 11:55:37 -07:00
parent 59af5d9e3e
commit 5fea5fc017

View File

@ -21,7 +21,7 @@ class _WonderDetailsScreenState extends State<WonderDetailsScreen>
late final _tabController = TabController(
length: 4,
vsync: this,
initialIndex: widget.tabIndex,
initialIndex: _clampIndex(widget.tabIndex),
)..addListener(_handleTabChanged);
AnimationController? _fade;
@ -31,7 +31,7 @@ class _WonderDetailsScreenState extends State<WonderDetailsScreen>
@override
void didUpdateWidget(covariant WonderDetailsScreen oldWidget) {
if (oldWidget.tabIndex != widget.tabIndex) {
_tabController.index = widget.tabIndex;
_tabController.index = _clampIndex(widget.tabIndex);
}
super.didUpdateWidget(oldWidget);
}
@ -42,6 +42,8 @@ class _WonderDetailsScreenState extends State<WonderDetailsScreen>
super.dispose();
}
int _clampIndex(int index) => index.clamp(0, 3);
void _handleTabChanged() {
_fade?.forward(from: 0);
setState(() {});