Merge pull request #878 from HansMuller/pageable_list_tabs

TabBarView is-a PageableList, doesn't need itemExtent

Base TabBarView on PageableList so that itemExtent and its SizeObserver aren't needed.

TabBarView scrolling is still TBD.
This commit is contained in:
Hans Muller 2015-12-10 12:34:32 -08:00
commit 54f2d4cb94
3 changed files with 35 additions and 64 deletions

View File

@ -22,21 +22,10 @@ class TabsDemo extends StatefulComponent {
}
class _TabsDemoState extends State<TabsDemo> {
double _viewWidth = 100.0;
void _handleSizeChanged(Size newSize) {
setState(() {
_viewWidth = newSize.width;
});
}
Widget build(_) {
return new SizeObserver(
onSizeChanged: _handleSizeChanged,
child: new TabBarView<String>(
return new TabBarView<String>(
selection: _selection,
items: _iconNames,
itemExtent: _viewWidth,
itemBuilder: (BuildContext context, String iconName, int index) {
return new Container(
key: new ValueKey<String>(iconName),
@ -46,7 +35,6 @@ class _TabsDemoState extends State<TabsDemo> {
)
);
}
)
);
}
}

View File

@ -272,25 +272,15 @@ class StockHomeState extends State<StockHome> {
);
}
double _viewWidth = 100.0;
void _handleSizeChanged(Size newSize) {
setState(() {
_viewWidth = newSize.width;
});
}
Widget build(BuildContext context) {
return new Scaffold(
key: _scaffoldKey,
toolBar: _isSearching ? buildSearchBar() : buildToolBar(),
floatingActionButton: buildFloatingActionButton(),
drawer: _buildDrawer(context),
body: new SizeObserver(
onSizeChanged: _handleSizeChanged,
child: new TabBarView<StockHomeTab>(
body: new TabBarView<StockHomeTab>(
selection: _tabBarSelection,
items: <StockHomeTab>[StockHomeTab.market, StockHomeTab.portfolio],
itemExtent: _viewWidth,
itemBuilder: (BuildContext context, StockHomeTab tab, _) {
switch (tab) {
case StockHomeTab.market:
@ -302,7 +292,6 @@ class StockHomeState extends State<StockHome> {
}
}
)
)
);
}
}

View File

@ -28,7 +28,7 @@ const double _kMinTabWidth = 72.0;
const double _kMaxTabWidth = 264.0;
const EdgeDims _kTabLabelPadding = const EdgeDims.symmetric(horizontal: 12.0);
const double _kTabBarScrollDrag = 0.025;
const Duration _kTabBarScroll = const Duration(milliseconds: 200);
const Duration _kTabBarScroll = const Duration(milliseconds: 300);
class _TabBarParentData extends ContainerBoxParentDataMixin<RenderBox> { }
@ -612,19 +612,17 @@ class _TabBarState extends ScrollableState<TabBar> {
}
}
class TabBarView<T> extends ScrollableList<T> {
class TabBarView<T> extends PageableList<T> {
TabBarView({
Key key,
this.selection,
List<T> items,
ItemBuilder<T> itemBuilder,
double itemExtent
ItemBuilder<T> itemBuilder
}) : super(
key: key,
scrollDirection: ScrollDirection.horizontal,
items: items,
itemBuilder: itemBuilder,
itemExtent: itemExtent,
itemsWrap: false
) {
assert(selection != null);
@ -640,9 +638,11 @@ class _NotScrollable extends BoundedBehavior {
bool get isScrollable => false;
}
class _TabBarViewState<T> extends ScrollableListState<T, TabBarView<T>> {
class _TabBarViewState<T> extends PageableListState<T, TabBarView<T>> {
ScrollBehavior createScrollBehavior() => new _NotScrollable();
final _NotScrollable _notScrollable = new _NotScrollable();
ScrollBehavior createScrollBehavior() => _notScrollable;
ExtentScrollBehavior get scrollBehavior => _notScrollable;
List<int> _itemIndices = [0, 1];
AnimationDirection _scrollDirection = AnimationDirection.forward;
@ -655,10 +655,10 @@ class _TabBarViewState<T> extends ScrollableListState<T, TabBarView<T>> {
scrollTo(0.0);
} else if (selectedIndex == config.items.length - 1) {
_itemIndices = <int>[selectedIndex - 1, selectedIndex];
scrollTo(config.itemExtent);
scrollTo(1.0);
} else {
_itemIndices = <int>[selectedIndex - 1, selectedIndex, selectedIndex + 1];
scrollTo(config.itemExtent);
scrollTo(1.0);
}
}
@ -680,12 +680,6 @@ class _TabBarViewState<T> extends ScrollableListState<T, TabBarView<T>> {
super.dispose();
}
void didUpdateConfig(TabBarView oldConfig) {
super.didUpdateConfig(oldConfig);
if (oldConfig.itemExtent != config.itemExtent && !_performance.isAnimating)
_initItemIndicesAndScrollPosition();
}
void _handleStatusChange(PerformanceStatus status) {
final int selectedIndex = config.selection.index;
final int previousSelectedIndex = config.selection.previousIndex;
@ -705,9 +699,9 @@ class _TabBarViewState<T> extends ScrollableListState<T, TabBarView<T>> {
void _handleProgressChange() {
if (_scrollDirection == AnimationDirection.forward)
scrollTo(config.itemExtent * _performance.progress);
scrollTo(_performance.progress);
else
scrollTo(config.itemExtent * (1.0 - _performance.progress));
scrollTo(1.0 - _performance.progress);
}
int get itemCount => _itemIndices.length;