From 432b17939588b0a97f5084bac2a8a72d27b8a48c Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Sun, 10 Jan 2016 22:09:51 -0800 Subject: [PATCH] TabView fails to update tab contents When the TabView widget's children change, we don't change the children we give to the PageViewport because we fail to update _items. This patch watches didChangeConfig and updates _items appropriately. Fixes #1134 Fixes #1168 --- packages/flutter/lib/src/material/tabs.dart | 40 +++++++++++++++------ 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 7420938f2d..c8942cabdd 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -799,7 +799,7 @@ class _TabBarViewState extends PageableListState implements TabBarSe _selection = selection; if (_selection != null) { _selection.registerPerformanceListener(this); - _initItemsAndScrollOffset(); + _updateItemsAndScrollBehavior(); } } @@ -808,6 +808,12 @@ class _TabBarViewState extends PageableListState implements TabBarSe _initSelection(TabBarSelection.of(context)); } + void didUpdateConfig(TabBarView oldConfig) { + super.didUpdateConfig(oldConfig); + if (_selection != null && config.children != oldConfig.children) + _updateItemsForSelectedIndex(_selection.index); + } + void dispose() { _selection?.unregisterPerformanceListener(this); super.dispose(); @@ -817,28 +823,40 @@ class _TabBarViewState extends PageableListState implements TabBarSe _selection = null; } - void _updateItems(int first, int second, [int third]) { + void _updateItemsFromChildren(int first, int second, [int third]) { List widgets = config.children; _items = [widgets[first], widgets[second]]; if (third != null) _items.add(widgets[third]); } - void _initItemsAndScrollOffset() { - assert(_selection != null); - final int selectedIndex = _selection.index; + void _updateItemsForSelectedIndex(int selectedIndex) { + if (selectedIndex == 0) { + _updateItemsFromChildren(0, 1); + } else if (selectedIndex == _tabCount - 1) { + _updateItemsFromChildren(selectedIndex - 1, selectedIndex); + } else { + _updateItemsFromChildren(selectedIndex - 1, selectedIndex, selectedIndex + 1); + } + } + + void _updateScrollBehaviorForSelectedIndex(int selectedIndex) { if (selectedIndex == 0) { - _updateItems(0, 1); scrollTo(scrollBehavior.updateExtents(contentExtent: 2.0, containerExtent: 1.0, scrollOffset: 0.0)); } else if (selectedIndex == _tabCount - 1) { - _updateItems(selectedIndex - 1, selectedIndex); scrollTo(scrollBehavior.updateExtents(contentExtent: 2.0, containerExtent: 1.0, scrollOffset: 1.0)); } else { - _updateItems(selectedIndex - 1, selectedIndex, selectedIndex + 1); scrollTo(scrollBehavior.updateExtents(contentExtent: 3.0, containerExtent: 1.0, scrollOffset: 1.0)); } } + void _updateItemsAndScrollBehavior() { + assert(_selection != null); + final int selectedIndex = _selection.index; + _updateItemsForSelectedIndex(selectedIndex); + _updateScrollBehaviorForSelectedIndex(selectedIndex); + } + void handleStatusChange(PerformanceStatus status) { } @@ -850,7 +868,7 @@ class _TabBarViewState extends PageableListState implements TabBarSe final Performance performance = _selection.performance; if (performance.status == PerformanceStatus.completed) { - _initItemsAndScrollOffset(); + _updateItemsAndScrollBehavior(); return; } @@ -861,10 +879,10 @@ class _TabBarViewState extends PageableListState implements TabBarSe final int previousSelectedIndex = _selection.previousIndex; if (selectedIndex < previousSelectedIndex) { - _updateItems(selectedIndex, previousSelectedIndex); + _updateItemsFromChildren(selectedIndex, previousSelectedIndex); _scrollDirection = AnimationDirection.reverse; } else { - _updateItems(previousSelectedIndex, selectedIndex); + _updateItemsFromChildren(previousSelectedIndex, selectedIndex); _scrollDirection = AnimationDirection.forward; }