Unverified Commit 0d90014b authored by Bruno Leroux's avatar Bruno Leroux Committed by GitHub

_TabBarViewState should not recreate page controller (#135500)

## Description

This PR replaces the unconditional instantiation of `PageController` in `_TabBarViewState.didChangeDependencies` as suggested in https://github.com/flutter/flutter/pull/134091#discussion_r1319177744.

## Related Issue

Fixes https://github.com/flutter/flutter/issues/134253.

## Tests

Adds 1 test.
parent da0cd696
......@@ -1851,12 +1851,14 @@ class _TabBarViewState extends State<TabBarView> {
super.didChangeDependencies();
_updateTabController();
_currentIndex = _controller!.index;
// TODO(chunhtai): https://github.com/flutter/flutter/issues/134253
_pageController?.dispose();
if (_pageController == null) {
_pageController = PageController(
initialPage: _currentIndex!,
viewportFraction: widget.viewportFraction,
);
} else {
_pageController!.jumpToPage(_currentIndex!);
}
}
@override
......
......@@ -4186,6 +4186,44 @@ void main() {
expect(tester.getCenter(find.byKey(lastTabKey)).dx, equals(750.0));
});
testWidgets('DefaultTabController changes does not recreate PageController', (WidgetTester tester) async {
// This is a regression test for https://github.com/flutter/flutter/issues/134253.
Widget buildFrame(int length) {
return boilerplate(
child: DefaultTabController(
length: length,
initialIndex: length - 1,
child: TabBarView(
physics: const TestScrollPhysics(),
children: List<Widget>.generate(
length,
(int index) {
return Center(child: Text('Page $index'));
},
),
),
),
);
}
await tester.pumpWidget(buildFrame(15));
PageView pageView = tester.widget(find.byType(PageView));
final PageController pageController1 = pageView.controller;
TabController tabController = DefaultTabController.of(tester.element(find.text('Page 14')));
expect(tabController.index, 14);
expect(pageController1.page, 14);
// Rebuild with a new default tab controller with more tabs.
await tester.pumpWidget(buildFrame(10));
pageView = tester.widget(find.byType(PageView));
final PageController pageController2 = pageView.controller;
tabController = DefaultTabController.of(tester.element(find.text('Page 9')));
expect(tabController.index, 9);
expect(pageController2.page, 9);
expect(pageController1, equals(pageController2));
});
testWidgets('Do not throw when switching between a scrollable TabBar and a non-scrollable TabBar', (WidgetTester tester) async {
// This is a regression test for https://github.com/flutter/flutter/issues/120649
final TabController controller1 = _tabController(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment