-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindexed_stack_lazy.dart
183 lines (170 loc) · 5.55 KB
/
indexed_stack_lazy.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
TabBar tabBar = TabBar(
controller: tabController,
isScrollable: true,
indicator: const UnderlineTabIndicator(),
tabs: openedTabPageList.map<Tab>((TabPage? tabPage) {
var tabContent = Row(
children: <Widget>[
Text(Utils.isLocalEn(context)
? tabPage!.nameEn ?? ''
: tabPage!.name ?? ''),
if (!defaultTabs.contains(tabPage))
Material(
type: MaterialType.transparency,
child: SizedBox(
width: 25,
child: IconButton(
iconSize: 10,
splashRadius: 10,
onPressed: () => Utils.closeTab(tabPage),
icon: Icon(Icons.close),
),
),
)
],
);
return Tab(
child: CryMenu(
child: tabContent,
onSelected: (dynamic v) {
switch (v) {
case TabMenuOption.close:
Utils.closeTab(tabPage);
break;
case TabMenuOption.closeAll:
Utils.closeAllTab();
break;
case TabMenuOption.closeOthers:
Utils.closeOtherTab(tabPage);
break;
case TabMenuOption.closeAllToTheRight:
Utils.closeAllToTheRightTab(tabPage);
break;
case TabMenuOption.closeAllToTheLeft:
Utils.closeAllToTheLeftTab(tabPage);
break;
}
},
// itemBuilder: (context) => <PopupMenuEntry<TabMenuOption>>[
// if (!defaultTabs.contains(tabPage))
// PopupMenuItem(
// value: TabMenuOption.close,
// child: ListTile(
// title: Text('Close'),
// ),
// ),
// PopupMenuItem(
// value: TabMenuOption.closeAll,
// child: ListTile(
// title: Text('Close All'),
// ),
// ),
// PopupMenuItem(
// value: TabMenuOption.closeOthers,
// child: ListTile(
// title: Text('Close Others'),
// ),
// ),
// PopupMenuItem(
// value: TabMenuOption.closeAllToTheRight,
// child: ListTile(
// title: Text('Close All to the Right'),
// ),
// ),
// PopupMenuItem(
// value: TabMenuOption.closeAllToTheLeft,
// child: ListTile(
// title: Text('Close All to the Left'),
// ),
// ),
// ],
),
);
}).toList(),
);
var content = Container(
child: Expanded(
child: IndexedStackLazy(
index: currentIndex,
children: openedTabPageList.map((TabPage? tabPage) {
var page = tabPage!.url != null
? Routes.layoutPagesMap[tabPage.url!] ?? Container()
: tabPage.widget ?? Container();
return KeyedSubtree(
child: page,
key: Key('page-${tabPage.id}'),
);
}).toList(),
),
),
);
var result = Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
color: context.theme.primaryColor,
child: Row(
children: <Widget>[
Expanded(child: tabBar),
IconButton(
onPressed: () => layoutController.toggleMaximize(),
icon: Icon(layoutController.isMaximize
? Icons.close_fullscreen
: Icons.open_in_full),
iconSize: 20,
color: Colors.white,
)
],
),
),
content,
],
),
);
return result;
}
}
import 'package:flutter/material.dart';
class IndexedStackLazy extends StatefulWidget {
final int index;
final List<Widget> children;
IndexedStackLazy({Key? key, this.index = 0, required this.children}) : super(key: key);
@override
_IndexedStackLazyState createState() => _IndexedStackLazyState();
}
class _IndexedStackLazyState extends State<IndexedStackLazy> {
List<Widget> children = <Widget>[];
int index = 0;
@override
void initState() {
children.add(widget.children[widget.index]);
super.initState();
}
@override
void didUpdateWidget(covariant IndexedStackLazy oldWidget) {
List<Widget> newChildren = <Widget>[];
for (int i = 0; i < widget.children.length; i++) {
var w = widget.children[i];
var existIndex = children.indexWhere((element) => element.key == w.key);
if (existIndex > -1) {
newChildren.add(children[existIndex]);
if (widget.index == i) {
index = newChildren.length - 1;
}
} else if (widget.index == i) {
newChildren.add(w);
index = newChildren.length - 1;
}
}
children = newChildren;
super.didUpdateWidget(oldWidget);
}
@override
Widget build(BuildContext context) {
return IndexedStack(
index: index,
children: children,
);
}
}