Skip to content
This repository was archived by the owner on May 20, 2023. It is now read-only.

Commit c3d410c

Browse files
TedSandernshahan
authored andcommitted
Migrate away from QueryList in tab panel.
PiperOrigin-RevId: 186536023
1 parent 5d4d560 commit c3d410c

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

lib/material_tab/material_tab_panel.dart

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ import 'tab_change_event.dart';
3535
// TODO(google): Change to `Visibility.local` to reduce code size.
3636
visibility: Visibility.all,
3737
)
38-
class MaterialTabPanelComponent {
38+
class MaterialTabPanelComponent implements AfterContentInit {
3939
final ChangeDetectorRef _changeDetector;
40+
bool _initialzed = false;
41+
Tab _previousActiveTab;
4042

4143
/// Stream of [TabChangeEvent] instances, published before the tab has
4244
/// changed.
@@ -78,10 +80,22 @@ class MaterialTabPanelComponent {
7880

7981
MaterialTabPanelComponent(this._changeDetector);
8082

83+
@override
84+
void ngAfterContentInit() {
85+
_initialzed = true;
86+
_initTabs();
87+
}
88+
8189
@ContentChildren(Tab)
82-
set tabs(QueryList<Tab> tabQuery) {
83-
final previousActiveTab = (_tabs != null) ? _activeTab : null;
84-
_tabs = new List.from(tabQuery);
90+
set tabs(List<Tab> tabs) {
91+
_previousActiveTab = (_tabs != null) ? _activeTab : null;
92+
_tabs = tabs;
93+
// TODO(google): Remove if setting of content children occur after
94+
// child is initialized.
95+
if (_initialzed) _initTabs();
96+
}
97+
98+
void _initTabs() {
8599
_tabLabels = _tabs.map((t) => t.label).toList();
86100
_tabIds = _tabs.map((t) => t.tabId).toList();
87101

@@ -90,8 +104,9 @@ class MaterialTabPanelComponent {
90104
scheduleMicrotask(() {
91105
_changeDetector.markForCheck(); // call early so we can return early.
92106
// Look for the previously active tab.
93-
if (previousActiveTab != null) {
94-
_activeTabIndex = _tabs.indexOf(previousActiveTab);
107+
if (_previousActiveTab != null) {
108+
_activeTabIndex = _tabs.indexOf(_previousActiveTab);
109+
_previousActiveTab = null;
95110
if (_activeTabIndex == -1) {
96111
// Couldn't find previous tab. Just activate the first tab.
97112
_activeTabIndex = 0;

0 commit comments

Comments
 (0)