From 27a12efabfe57aab9d77f1eb95cb946c7863e802 Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Thu, 3 Oct 2019 06:42:22 +0800 Subject: [PATCH] Update DefaultTabController to allow for zero tabs (#41625) * Update DefaultTabController to allow for zero tabs --- .../lib/src/material/tab_controller.dart | 2 +- packages/flutter/test/material/tabs_test.dart | 73 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/tab_controller.dart b/packages/flutter/lib/src/material/tab_controller.dart index c7da646571..9da56889be 100644 --- a/packages/flutter/lib/src/material/tab_controller.dart +++ b/packages/flutter/lib/src/material/tab_controller.dart @@ -313,7 +313,7 @@ class DefaultTabController extends StatefulWidget { @required this.child, }) : assert(initialIndex != null), assert(length >= 0), - assert(initialIndex >= 0 && initialIndex < length), + assert(length == 0 || (initialIndex >= 0 && initialIndex < length)), super(key: key); /// The total number of tabs. diff --git a/packages/flutter/test/material/tabs_test.dart b/packages/flutter/test/material/tabs_test.dart index cc95e72b3e..7f46b98cb8 100644 --- a/packages/flutter/test/material/tabs_test.dart +++ b/packages/flutter/test/material/tabs_test.dart @@ -2350,4 +2350,77 @@ void main() { expect(find.text('Tab1'), findsOneWidget); expect(find.text('Tab2'), findsOneWidget); }); + + testWidgets('DefaultTabController should allow for a length of zero', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/20292. + List tabTextContent = []; + + await tester.pumpWidget( + MaterialApp( + home: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return DefaultTabController( + length: tabTextContent.length, + child: Scaffold( + appBar: AppBar( + title: const Text('Default TabBar Preview'), + bottom: tabTextContent.isNotEmpty + ? TabBar( + isScrollable: true, + tabs: tabTextContent.map((String textContent) => Tab(text: textContent)).toList(), + ) + : null, + ), + body: tabTextContent.isNotEmpty + ? TabBarView( + children: tabTextContent.map((String textContent) => Tab(text: '$textContent\'s view')).toList() + ) + : const Center(child: Text('No tabs')), + bottomNavigationBar: BottomAppBar( + child: Row( + mainAxisSize: MainAxisSize.max, + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + IconButton( + key: const Key('Add tab'), + icon: const Icon(Icons.add), + onPressed: () { + setState(() { + tabTextContent = List.from(tabTextContent) + ..add('Tab ${tabTextContent.length + 1}'); + }); + }, + ), + IconButton( + key: const Key('Delete tab'), + icon: const Icon(Icons.delete), + onPressed: () { + setState(() { + tabTextContent = List.from(tabTextContent) + ..removeLast(); + }); + }, + ), + ], + ), + ), + ), + ); + }, + ), + ), + ); + + // Initializes with zero tabs properly + expect(find.text('No tabs'), findsOneWidget); + await tester.tap(find.byKey(const Key('Add tab'))); + await tester.pumpAndSettle(); + expect(find.text('Tab 1'), findsOneWidget); + expect(find.text('Tab 1\'s view'), findsOneWidget); + + // Dynamically updates to zero tabs properly + await tester.tap(find.byKey(const Key('Delete tab'))); + await tester.pumpAndSettle(); + expect(find.text('No tabs'), findsOneWidget); + }); }