Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Material] Update TabController to support dynamic Tabs #30884

Merged
merged 15 commits into from Apr 29, 2019
6 changes: 5 additions & 1 deletion packages/flutter/lib/src/material/tab_controller.dart
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math';
johnsonmh marked this conversation as resolved.
Show resolved Hide resolved

import 'package:flutter/widgets.dart';

import 'constants.dart';
Expand Down Expand Up @@ -289,6 +291,8 @@ class DefaultTabController extends StatefulWidget {
this.initialIndex = 0,
@required this.child,
}) : assert(initialIndex != null),
assert(length >= 0),
assert(initialIndex >= 0 && initialIndex < length),
super(key: key);

/// The total number of tabs. Typically greater than one. Must match
Expand Down Expand Up @@ -359,7 +363,7 @@ class _DefaultTabControllerState extends State<DefaultTabController> with Single
// automatically update the index of the controller to be the new last tab.
int newIndex;
if (_controller.index >= widget.length) {
newIndex = widget.length - 1;
newIndex = max(0, widget.length - 1);
}
_controller = _controller._copyWith(length: widget.length, index: newIndex);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter/lib/src/material/tabs.dart
Expand Up @@ -954,7 +954,7 @@ class _TabBarState extends State<TabBar> {
if (_controller.length != widget.tabs.length) {
throw FlutterError(
'Controller\'s length property (${_controller.length}) does not match the \n'
'number of tab elements (${widget.tabs.length}) present in TabBar\'s tabs property.'
'number of tabs (${widget.tabs.length}) present in TabBar\'s tabs property.'
);
}
return true;
Expand Down Expand Up @@ -1261,7 +1261,7 @@ class _TabBarViewState extends State<TabBarView> {
if (_controller.length != widget.children.length) {
throw FlutterError(
'Controller\'s length property (${_controller.length}) does not match the \n'
'number of tab elements (${widget.children.length}) present in TabBar\'s tabs property.'
'number of tabs (${widget.children.length}) present in TabBar\'s tabs property.'
);
}
return true;
Expand Down