Unverified Commit b27b3d74 authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

Improve Tabs documentation (#31502)

Added info regarding matching lengths requirement and included links to sample implementation
parent e00f1a3c
...@@ -71,13 +71,15 @@ import 'constants.dart'; ...@@ -71,13 +71,15 @@ import 'constants.dart';
/// ``` /// ```
/// {@end-tool} /// {@end-tool}
class TabController extends ChangeNotifier { class TabController extends ChangeNotifier {
/// Creates an object that manages the state required by [TabBar] and a [TabBarView]. /// Creates an object that manages the state required by [TabBar] and a
/// [TabBarView].
/// ///
/// The [length] must not be null or negative. Typically its a value greater than one, i.e. /// The [length] must not be null or negative. Typically its a value greater
/// typically there are two or more tabs. /// than one, i.e. typically there are two or more tabs. The [length] must
/// match [TabBar.tabs]'s and [TabBarView.children]'s length.
/// ///
/// The `initialIndex` must be valid given [length] and must not be null. If [length] is /// The `initialIndex` must be valid given [length] and must not be null. If
/// zero, then `initialIndex` must be 0 (the default). /// [length] is zero, then `initialIndex` must be 0 (the default).
TabController({ int initialIndex = 0, @required this.length, @required TickerProvider vsync }) TabController({ int initialIndex = 0, @required this.length, @required TickerProvider vsync })
: assert(length != null && length >= 0), : assert(length != null && length >= 0),
assert(initialIndex != null && initialIndex >= 0 && (length == 0 || initialIndex < length)), assert(initialIndex != null && initialIndex >= 0 && (length == 0 || initialIndex < length)),
...@@ -103,7 +105,8 @@ class TabController extends ChangeNotifier { ...@@ -103,7 +105,8 @@ class TabController extends ChangeNotifier {
Animation<double> get animation => _animationController?.view ?? kAlwaysCompleteAnimation; Animation<double> get animation => _animationController?.view ?? kAlwaysCompleteAnimation;
final AnimationController _animationController; final AnimationController _animationController;
/// The total number of tabs. Typically greater than one. /// The total number of tabs. Typically greater than one. Must match
/// [TabBar.tabs]'s and [TabBarView.children]'s length.
final int length; final int length;
void _changeIndex(int value, { Duration duration, Curve curve }) { void _changeIndex(int value, { Duration duration, Curve curve }) {
...@@ -249,7 +252,8 @@ class _TabControllerScope extends InheritedWidget { ...@@ -249,7 +252,8 @@ class _TabControllerScope extends InheritedWidget {
class DefaultTabController extends StatefulWidget { class DefaultTabController extends StatefulWidget {
/// Creates a default tab controller for the given [child] widget. /// Creates a default tab controller for the given [child] widget.
/// ///
/// The [length] argument is typically greater than one. /// The [length] argument is typically greater than one. The [length] must
/// match [TabBar.tabs]'s and [TabBarView.children]'s length.
/// ///
/// The [initialIndex] argument must not be null. /// The [initialIndex] argument must not be null.
const DefaultTabController({ const DefaultTabController({
...@@ -260,7 +264,8 @@ class DefaultTabController extends StatefulWidget { ...@@ -260,7 +264,8 @@ class DefaultTabController extends StatefulWidget {
}) : assert(initialIndex != null), }) : assert(initialIndex != null),
super(key: key); super(key: key);
/// The total number of tabs. Typically greater than one. /// The total number of tabs. Typically greater than one. Must match
/// [TabBar.tabs]'s and [TabBarView.children]'s length.
final int length; final int length;
/// The initial index of the selected tab. /// The initial index of the selected tab.
......
...@@ -530,12 +530,15 @@ class _TabBarScrollController extends ScrollController { ...@@ -530,12 +530,15 @@ class _TabBarScrollController extends ScrollController {
/// ///
/// If a [TabController] is not provided, then a [DefaultTabController] ancestor /// If a [TabController] is not provided, then a [DefaultTabController] ancestor
/// must be provided instead. The tab controller's [TabController.length] must /// must be provided instead. The tab controller's [TabController.length] must
/// equal the length of the [tabs] list. /// equal the length of the [tabs] list and the length of the
/// [TabBarView.children] list.
/// ///
/// Requires one of its ancestors to be a [Material] widget. /// Requires one of its ancestors to be a [Material] widget.
/// ///
/// Uses values from [TabBarTheme] if it is set in the current context. /// Uses values from [TabBarTheme] if it is set in the current context.
/// ///
/// To see a sample implementation, visit the [TabController] documentation.
///
/// See also: /// See also:
/// ///
/// * [TabBarView], which displays page views that correspond to each tab. /// * [TabBarView], which displays page views that correspond to each tab.
...@@ -580,7 +583,8 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget { ...@@ -580,7 +583,8 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget {
/// Typically a list of two or more [Tab] widgets. /// Typically a list of two or more [Tab] widgets.
/// ///
/// The length of this list must match the [controller]'s [TabController.length]. /// The length of this list must match the [controller]'s [TabController.length]
/// and the length of the [TabBarView.children] list.
final List<Widget> tabs; final List<Widget> tabs;
/// This widget's selection and animation state. /// This widget's selection and animation state.
...@@ -1069,6 +1073,11 @@ class _TabBarState extends State<TabBar> { ...@@ -1069,6 +1073,11 @@ class _TabBarState extends State<TabBar> {
/// ///
/// If a [TabController] is not provided, then there must be a [DefaultTabController] /// If a [TabController] is not provided, then there must be a [DefaultTabController]
/// ancestor. /// ancestor.
///
/// The tab controller's [TabController.length] must equal the length of the
/// [children] list and the length of the [TabBar.tabs] list.
///
/// To see a sample implementation, visit the [TabController] documentation.
class TabBarView extends StatefulWidget { class TabBarView extends StatefulWidget {
/// Creates a page view with one child per tab. /// Creates a page view with one child per tab.
/// ///
...@@ -1089,7 +1098,8 @@ class TabBarView extends StatefulWidget { ...@@ -1089,7 +1098,8 @@ class TabBarView extends StatefulWidget {
/// will be used. /// will be used.
final TabController controller; final TabController controller;
/// One widget per tab. /// One widget per tab. Its length must match the length of the [TabBar.tabs]
/// list, as well as the [controller]'s [TabController.length].
final List<Widget> children; final List<Widget> children;
/// How the page view should respond to user input. /// How the page view should respond to user input.
......
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