Commit 51d06647 authored by Adam Barth's avatar Adam Barth

Merge pull request #1122 from abarth/fix_tabs

Tabs don't display items other than 0 and 1
parents 1cee346a 5e2badad
...@@ -782,7 +782,7 @@ class TabBarView<T> extends PageableList { ...@@ -782,7 +782,7 @@ class TabBarView<T> extends PageableList {
}) : items = items, itemBuilder = itemBuilder, super( }) : items = items, itemBuilder = itemBuilder, super(
key: key, key: key,
scrollDirection: ScrollDirection.horizontal, scrollDirection: ScrollDirection.horizontal,
children: items.map((T item) => itemBuilder(item)), children: items.map((T item) => itemBuilder(item)).toList(),
itemsWrap: false itemsWrap: false
) { ) {
assert(items != null); assert(items != null);
...@@ -798,7 +798,7 @@ class TabBarView<T> extends PageableList { ...@@ -798,7 +798,7 @@ class TabBarView<T> extends PageableList {
class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements TabBarSelectionPerformanceListener { class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements TabBarSelectionPerformanceListener {
TabBarSelectionState _selection; TabBarSelectionState _selection;
List<int> _itemIndices = [0, 1]; List<Widget> _items;
AnimationDirection _scrollDirection = AnimationDirection.forward; AnimationDirection _scrollDirection = AnimationDirection.forward;
int get _tabCount => config.items.length; int get _tabCount => config.items.length;
...@@ -810,7 +810,6 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta ...@@ -810,7 +810,6 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta
return _boundedBehavior; return _boundedBehavior;
} }
void _initSelection(TabBarSelectionState<T> selection) { void _initSelection(TabBarSelectionState<T> selection) {
_selection = selection; _selection = selection;
if (_selection != null) { if (_selection != null) {
...@@ -833,17 +832,24 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta ...@@ -833,17 +832,24 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta
_selection = null; _selection = null;
} }
void _updateItems(int first, int second, [int third]) {
List<Widget> widgets = config.children;
_items = <Widget>[widgets[first], widgets[second]];
if (third != null)
_items.add(widgets[third]);
}
void _initItemIndicesAndScrollPosition() { void _initItemIndicesAndScrollPosition() {
assert(_selection != null); assert(_selection != null);
final int selectedIndex = _selection.index; final int selectedIndex = _selection.index;
if (selectedIndex == 0) { if (selectedIndex == 0) {
_itemIndices = <int>[0, 1]; _updateItems(0, 1);
scrollTo(0.0); scrollTo(0.0);
} else if (selectedIndex == _tabCount - 1) { } else if (selectedIndex == _tabCount - 1) {
_itemIndices = <int>[selectedIndex - 1, selectedIndex]; _updateItems(selectedIndex - 1, selectedIndex);
scrollTo(1.0); scrollTo(1.0);
} else { } else {
_itemIndices = <int>[selectedIndex - 1, selectedIndex, selectedIndex + 1]; _updateItems(selectedIndex - 1, selectedIndex, selectedIndex + 1);
scrollTo(1.0); scrollTo(1.0);
} }
} }
...@@ -870,10 +876,10 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta ...@@ -870,10 +876,10 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta
final int previousSelectedIndex = _selection.previousIndex; final int previousSelectedIndex = _selection.previousIndex;
if (selectedIndex < previousSelectedIndex) { if (selectedIndex < previousSelectedIndex) {
_itemIndices = <int>[selectedIndex, previousSelectedIndex]; _updateItems(selectedIndex, previousSelectedIndex);
_scrollDirection = AnimationDirection.reverse; _scrollDirection = AnimationDirection.reverse;
} else { } else {
_itemIndices = <int>[previousSelectedIndex, selectedIndex]; _updateItems(previousSelectedIndex, selectedIndex);
_scrollDirection = AnimationDirection.forward; _scrollDirection = AnimationDirection.forward;
} }
...@@ -883,8 +889,6 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta ...@@ -883,8 +889,6 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta
scrollTo(1.0 - performance.progress); scrollTo(1.0 - performance.progress);
} }
int get itemCount => _itemIndices.length;
void dispatchOnScroll() { void dispatchOnScroll() {
if (_selection == null || _selection.valueIsChanging) if (_selection == null || _selection.valueIsChanging)
return; return;
...@@ -904,14 +908,16 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta ...@@ -904,14 +908,16 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta
if (scrollVelocity.dx.abs() > _kMinFlingVelocity) { if (scrollVelocity.dx.abs() > _kMinFlingVelocity) {
final int selectionDelta = scrollVelocity.dx > 0 ? -1 : 1; final int selectionDelta = scrollVelocity.dx > 0 ? -1 : 1;
_selection.value = _selection.values[(_selection.index + selectionDelta).clamp(0, _tabCount - 1)]; final int targetIndex = (_selection.index + selectionDelta).clamp(0, _tabCount - 1);
_selection.value = _selection.values[targetIndex];
return new Future.value(); return new Future.value();
} }
final int selectionIndex = _selection.index; final int selectionIndex = _selection.index;
final int settleIndex = snapScrollOffset(scrollOffset).toInt(); final int settleIndex = snapScrollOffset(scrollOffset).toInt();
if (selectionIndex > 0 && settleIndex != 1) { if (selectionIndex > 0 && settleIndex != 1) {
_selection.value = _selection.values[selectionIndex + (settleIndex == 2 ? 1 : -1)]; final int targetIndex = (selectionIndex + (settleIndex == 2 ? 1 : -1)).clamp(0, _tabCount - 1);
_selection.value = _selection.values[targetIndex];
return new Future.value(); return new Future.value();
} else if (selectionIndex == 0 && settleIndex == 1) { } else if (selectionIndex == 0 && settleIndex == 1) {
_selection.value = _selection.values[1]; _selection.value = _selection.values[1];
...@@ -924,6 +930,12 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta ...@@ -924,6 +930,12 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta
TabBarSelectionState<T> newSelection = TabBarSelection.of(context); TabBarSelectionState<T> newSelection = TabBarSelection.of(context);
if (_selection != newSelection) if (_selection != newSelection)
_initSelection(newSelection); _initSelection(newSelection);
return super.buildContent(context); return new PageViewport(
itemsWrap: config.itemsWrap,
scrollDirection: config.scrollDirection,
startOffset: scrollOffset,
overlayPainter: config.scrollableListPainter,
children: _items
);
} }
} }
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