Update DefaultTabController to allow for zero tabs (#41625)
* Update DefaultTabController to allow for zero tabs
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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<String> tabTextContent = <String>[];
|
||||
|
||||
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: <Widget>[
|
||||
IconButton(
|
||||
key: const Key('Add tab'),
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
tabTextContent = List<String>.from(tabTextContent)
|
||||
..add('Tab ${tabTextContent.length + 1}');
|
||||
});
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
key: const Key('Delete tab'),
|
||||
icon: const Icon(Icons.delete),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
tabTextContent = List<String>.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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user