From 74761265bc73e51a7855b6a05b0f0ef480027eda Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 23 Jun 2016 13:43:16 -0700 Subject: [PATCH] Add dartdoc for tabs and text selection handles (#4715) --- packages/flutter/lib/src/material/tabs.dart | 78 +++++++++++++++++-- .../flutter/lib/src/scheduler/binding.dart | 16 ++++ .../lib/src/widgets/text_selection.dart | 70 ++++++++++++++--- 3 files changed, 147 insertions(+), 17 deletions(-) diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 042a9f8808..81a24490a6 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -519,9 +519,15 @@ class TabBarSelection extends StatefulWidget { } } +/// State for a [TabBarSelection] widget. +/// +/// Subclasses of [TabBarSelection] typically use [State] objects that extend +/// this class. class TabBarSelectionState extends State> { + /// An animation that updates as the selected tab changes. Animation get animation => _controller.view; + // Both the TabBar and TabBarView classes access _controller because they // alternately drive selection progress between tabs. final AnimationController _controller = new AnimationController(duration: _kTabBarScroll, value: 1.0); @@ -559,18 +565,38 @@ class TabBarSelectionState extends State> { PageStorage.of(context)?.writeState(context, _value); } + /// The list of possible values that the selection can obtain. List get values => config.values; + /// The previously selected value. + /// + /// When the tab selection changes, the tab selection animates from the + /// previously selected value to the new value. T get previousValue => _previousValue; T _previousValue; - bool _valueIsChanging = false; + /// Whether the tab selection is in the process of animating from one value to + /// another. + // TODO(abarth): Try computing this value from _controller.state so we don't + // need to keep a separate bool in sync. bool get valueIsChanging => _valueIsChanging; + bool _valueIsChanging = false; + /// The index of a given value in [values]. + /// + /// Runs in constant time. int indexOf(T tabValue) => _valueToIndex[tabValue]; + + /// The index of the currently selected value. int get index => _valueToIndex[value]; + + /// The index of the previoulsy selected value. int get previousIndex => indexOf(_previousValue); + /// The currently selected value. + /// + /// Writing to this field will cause the tab selection to animate from the + /// previous value to the new value. T get value => _value; T _value; set value(T newValue) { @@ -607,6 +633,8 @@ class TabBarSelectionState extends State> { _controller ..value = value ..forward().then((_) { + // TODO(abarth): Consider using a status listener and checking for + // AnimationStatus.completed. if (_controller.value == 1.0) { if (config.onChanged != null) config.onChanged(_value); @@ -617,6 +645,9 @@ class TabBarSelectionState extends State> { final List _animationListeners = []; + /// Calls listener methods every time the value or status of the selection animation changes. + /// + /// Listeners can be removed with [unregisterAnimationListener]. void registerAnimationListener(TabBarSelectionAnimationListener listener) { _animationListeners.add(listener); _controller @@ -624,6 +655,9 @@ class TabBarSelectionState extends State> { ..addListener(listener.handleProgressChange); } + /// Stop calling listener methods every time the value or status of the animation changes. + /// + /// Listeners can be added with [registerAnimationListener]. void unregisterAnimationListener(TabBarSelectionAnimationListener listener) { _animationListeners.remove(listener); _controller @@ -649,7 +683,7 @@ class TabBarSelectionState extends State> { } } -/// Displays a horizontal row of tabs, one per label. +/// A widget that displays a horizontal row of tabs, one per label. /// /// Requires one of its ancestors to be a [TabBarSelection] widget to enable /// saving and monitoring the selected tab. @@ -663,15 +697,23 @@ class TabBarSelectionState extends State> { /// * [AppBar.tabBar] /// * class TabBar extends Scrollable implements AppBarBottomWidget { + /// Creates a widget that displays a horizontal row of tabs, one per label. + /// + /// The [labels] argument must not be null. TabBar({ Key key, - this.labels, + @required this.labels, this.isScrollable: false, this.indicatorColor, this.labelColor - }) : super(key: key, scrollDirection: Axis.horizontal); + }) : super(key: key, scrollDirection: Axis.horizontal) { + assert(labels != null); + } /// The labels to display in the tabs. + /// + /// The [TabBarSelection.values] are used as keys for this map to determine + /// which tab label is selected. final Map labels; /// Whether this tab bar can be scrolled horizontally. @@ -978,10 +1020,23 @@ class _TabBarState extends ScrollableState> implements TabBarSelect } } +/// A widget that displays the contents of a tab. +/// +/// Requires one of its ancestors to be a [TabBarSelection] widget to enable +/// saving and monitoring the selected tab. +/// +/// See also: +/// +/// * [TabBarSelection] +/// * [TabBar] +/// * class TabBarView extends PageableList { + /// Creates a widget that displays the contents of a tab. + /// + /// The [children] argument must not be null and must not be empty. TabBarView({ Key key, - List children + @required List children }) : super( key: key, scrollDirection: Axis.horizontal, @@ -1165,7 +1220,20 @@ class _TabBarViewState extends PageableListState> implements Ta } } +/// A widget that displays a visual indicator of which tab is selected. +/// +/// Requires one of its ancestors to be a [TabBarSelection] widget to enable +/// saving and monitoring the selected tab. +/// +/// See also: +/// +/// * [TabBarSelection] +/// * [TabBarView] class TabPageSelector extends StatelessWidget { + /// Creates a widget that displays a visual indicator of which tab is selected. + /// + /// Requires one of its ancestors to be a [TabBarSelection] widget to enable + /// saving and monitoring the selected tab. const TabPageSelector({ Key key }) : super(key: key); Widget _buildTabIndicator(TabBarSelectionState selection, T tab, Animation animation, ColorTween selectedColor, ColorTween previousColor) { diff --git a/packages/flutter/lib/src/scheduler/binding.dart b/packages/flutter/lib/src/scheduler/binding.dart index 0f1a1ad12b..01851da517 100644 --- a/packages/flutter/lib/src/scheduler/binding.dart +++ b/packages/flutter/lib/src/scheduler/binding.dart @@ -381,6 +381,22 @@ abstract class SchedulerBinding extends BindingBase { Duration _epochStart = Duration.ZERO; Duration _lastRawTimeStamp = Duration.ZERO; + /// Prepares the scheduler for a non-monotonic change to how time stamps are calcuated. + /// + /// Callbacks received from the scheduler assume that their time stamps are + /// monotonically increasing. The raw time stamp passed to [handleBeginFrame] + /// is monotonic, but the scheduler might adjust those time stamps to provide + /// [timeDilation]. Without careful handling, these adjusts could cause time + /// to appear to run backwards. + /// + /// The [resetEpoch] function ensures that the time stamps are monotonic by + /// reseting the base time stamp used for future time stamp adjustments to the + /// current value. For example, if the [timeDilation] decreases, rather than + /// scaling down the [Duration] since the beginning of time, [resetEpoch] will + /// ensure that we only scale down the duration since [resetEpoch] was called. + /// + /// Note: Setting [timeDilation] calls [resetEpoch] automatically. You don't + /// need to call [resetEpoch] yourself. void resetEpoch() { _epochStart = _adjustForEpoch(_lastRawTimeStamp); _firstRawTimeStampInEpoch = null; diff --git a/packages/flutter/lib/src/widgets/text_selection.dart b/packages/flutter/lib/src/widgets/text_selection.dart index f963e9b6d9..86760ae782 100644 --- a/packages/flutter/lib/src/widgets/text_selection.dart +++ b/packages/flutter/lib/src/widgets/text_selection.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:flutter/rendering.dart'; +import 'package:meta/meta.dart'; import 'basic.dart'; import 'container.dart'; @@ -12,21 +13,36 @@ import 'gesture_detector.dart'; import 'overlay.dart'; // TODO(mpcomplete): Need one for [collapsed]. -/// Which type of selection handle to be displayed. With mixed-direction text, -/// both handles may be the same type. Examples: -/// LTR text: 'the fox' +/// Which type of selection handle to be displayed. +/// +/// With mixed-direction text, both handles may be the same type. Examples: +/// +/// * LTR text: 'the fox': /// The '<' is drawn with the [left] type, the '>' with the [right] -/// RTL text: 'xof eht' +/// +/// * RTL text: 'xof eht': /// Same as above. -/// mixed text: ' onSelectionOverlayChanged; + + /// Builds the selection handles. + /// + /// The selection handles let the user adjust which portion of the text is + /// selected. final TextSelectionHandleBuilder handleBuilder; + + /// Builds a tool bar to display near the selection. + /// + /// The tool bar typically contains buttons for copying and pasting text. final TextSelectionToolbarBuilder toolbarBuilder; + InputValue _input; /// A pair of handles. If this is non-null, there are always 2, though the