Fix switching from scrollable and non-scrollable tab bars throws (#120771)

Co-authored-by: Bruno Leroux <bruno.leroux@gmail.com>
This commit is contained in:
Bruno Leroux 2023-02-15 17:26:50 +01:00 committed by GitHub
parent 7865713687
commit 541a8bfd9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -1142,7 +1142,7 @@ class _TabBarState extends State<TabBar> {
_updateTabController();
_initIndicatorPainter();
// Adjust scroll position.
if (_scrollController != null) {
if (_scrollController != null && _scrollController!.hasClients) {
final ScrollPosition position = _scrollController!.position;
if (position is _TabBarScrollPosition) {
position.markNeedsPixelsCorrection();

View File

@ -3568,6 +3568,46 @@ void main() {
expect(tester.getCenter(find.byKey(lastTabKey)).dx, equals(750.0));
});
testWidgets('Do not throw when switching beetween 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(
vsync: const TestVSync(),
length: 2,
);
final TabController controller2 = TabController(
vsync: const TestVSync(),
length: 2,
);
Widget buildFrame(TabController controller, bool isScrollable) {
return boilerplate(
child: Container(
alignment: Alignment.topLeft,
child: TabBar(
controller: controller,
isScrollable: isScrollable,
tabs: const <Tab>[
Tab(text: 'LEFT'),
Tab(text: 'RIGHT'),
],
),
),
);
}
// Show both controllers once.
await tester.pumpWidget(buildFrame(controller1, false));
await tester.pumpWidget(buildFrame(controller2, true));
// Switch back to the first controller.
await tester.pumpWidget(buildFrame(controller1, false));
expect(tester.takeException(), null);
// Switch back to the second controller.
await tester.pumpWidget(buildFrame(controller2, true));
expect(tester.takeException(), null);
});
testWidgets('Default tab indicator color is white', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/15958
final List<String> tabs = <String>['LEFT', 'RIGHT'];