Skip to content

Commit 3c112d0

Browse files
authored
Fix TabController index update (#2753)
Hi, I noticed this bug in the navigation_and_routing sample. This PR fixes the tab controller state update issue in BooksScreen.
1 parent 6f4e07c commit 3c112d0

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

navigation_and_routing/lib/src/screens/books.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ class _BooksScreenState extends State<BooksScreen>
3131
..addListener(_handleTabIndexChanged);
3232
}
3333

34+
@override
35+
void didUpdateWidget(covariant BooksScreen oldWidget) {
36+
super.didUpdateWidget(oldWidget);
37+
if (oldWidget.selectedIndex != widget.selectedIndex) {
38+
_tabController.index = widget.selectedIndex;
39+
}
40+
}
41+
3442
@override
3543
void dispose() {
3644
_tabController.removeListener(_handleTabIndexChanged);
@@ -39,7 +47,6 @@ class _BooksScreenState extends State<BooksScreen>
3947

4048
@override
4149
Widget build(BuildContext context) {
42-
_tabController.index = widget.selectedIndex;
4350
return Scaffold(
4451
appBar: AppBar(
4552
title: const Text('Books'),

0 commit comments

Comments
 (0)