diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 83f104db6b..59adab1e00 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -782,7 +782,7 @@ class TabBarView extends PageableList { }) : items = items, itemBuilder = itemBuilder, super( key: key, scrollDirection: ScrollDirection.horizontal, - children: items.map((T item) => itemBuilder(item)), + children: items.map((T item) => itemBuilder(item)).toList(), itemsWrap: false ) { assert(items != null); @@ -798,7 +798,7 @@ class TabBarView extends PageableList { class _TabBarViewState extends PageableListState> implements TabBarSelectionPerformanceListener { TabBarSelectionState _selection; - List _itemIndices = [0, 1]; + List _items; AnimationDirection _scrollDirection = AnimationDirection.forward; int get _tabCount => config.items.length; @@ -810,7 +810,6 @@ class _TabBarViewState extends PageableListState> implements Ta return _boundedBehavior; } - void _initSelection(TabBarSelectionState selection) { _selection = selection; if (_selection != null) { @@ -833,17 +832,24 @@ class _TabBarViewState extends PageableListState> implements Ta _selection = null; } + void _updateItems(int first, int second, [int third]) { + List widgets = config.children; + _items = [widgets[first], widgets[second]]; + if (third != null) + _items.add(widgets[third]); + } + void _initItemIndicesAndScrollPosition() { assert(_selection != null); final int selectedIndex = _selection.index; if (selectedIndex == 0) { - _itemIndices = [0, 1]; + _updateItems(0, 1); scrollTo(0.0); } else if (selectedIndex == _tabCount - 1) { - _itemIndices = [selectedIndex - 1, selectedIndex]; + _updateItems(selectedIndex - 1, selectedIndex); scrollTo(1.0); } else { - _itemIndices = [selectedIndex - 1, selectedIndex, selectedIndex + 1]; + _updateItems(selectedIndex - 1, selectedIndex, selectedIndex + 1); scrollTo(1.0); } } @@ -870,10 +876,10 @@ class _TabBarViewState extends PageableListState> implements Ta final int previousSelectedIndex = _selection.previousIndex; if (selectedIndex < previousSelectedIndex) { - _itemIndices = [selectedIndex, previousSelectedIndex]; + _updateItems(selectedIndex, previousSelectedIndex); _scrollDirection = AnimationDirection.reverse; } else { - _itemIndices = [previousSelectedIndex, selectedIndex]; + _updateItems(previousSelectedIndex, selectedIndex); _scrollDirection = AnimationDirection.forward; } @@ -883,8 +889,6 @@ class _TabBarViewState extends PageableListState> implements Ta scrollTo(1.0 - performance.progress); } - int get itemCount => _itemIndices.length; - void dispatchOnScroll() { if (_selection == null || _selection.valueIsChanging) return; @@ -904,14 +908,16 @@ class _TabBarViewState extends PageableListState> implements Ta if (scrollVelocity.dx.abs() > _kMinFlingVelocity) { 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(); } final int selectionIndex = _selection.index; final int settleIndex = snapScrollOffset(scrollOffset).toInt(); 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(); } else if (selectionIndex == 0 && settleIndex == 1) { _selection.value = _selection.values[1]; @@ -924,6 +930,12 @@ class _TabBarViewState extends PageableListState> implements Ta TabBarSelectionState newSelection = TabBarSelection.of(context); if (_selection != newSelection) _initSelection(newSelection); - return super.buildContent(context); + return new PageViewport( + itemsWrap: config.itemsWrap, + scrollDirection: config.scrollDirection, + startOffset: scrollOffset, + overlayPainter: config.scrollableListPainter, + children: _items + ); } }