@@ -28,7 +28,7 @@ abstract class NavigationHub<T extends StatefulWidget> extends NyState<T> {
28
28
NavigationHubLayout ? layout;
29
29
30
30
/// The current index of the page
31
- int currentIndex = 0 ;
31
+ int ? currentIndex;
32
32
33
33
/// The navigator keys
34
34
Map <int , UniqueKey > navigatorKeys = {};
@@ -39,10 +39,13 @@ abstract class NavigationHub<T extends StatefulWidget> extends NyState<T> {
39
39
/// The reset map
40
40
Map <int , bool > reset = {};
41
41
42
+ /// Get the current index
43
+ int get getCurrentIndex => currentIndex ?? 0 ;
44
+
42
45
@override
43
46
get init => () {
44
47
int ? activeTab = data (defaultValue: {"tab-index" : 0 })['tab-index' ];
45
- currentIndex = activeTab ?? 0 ;
48
+ currentIndex ?? = activeTab ?? 0 ;
46
49
47
50
if (pages is Future Function ()) {
48
51
awaitData (perform: () async {
@@ -128,73 +131,86 @@ abstract class NavigationHub<T extends StatefulWidget> extends NyState<T> {
128
131
);
129
132
}
130
133
134
+ /// Helper to build the bottom nav widget
135
+ Widget bottomNavBuilder (
136
+ BuildContext context, Widget body, Widget ? bottomNavigationBar) {
137
+ throw UnimplementedError ();
138
+ }
139
+
131
140
@override
132
141
Widget view (BuildContext context) {
133
142
Map <int , NavigationTab > pages = orderedPages;
134
143
if (layout? .kind == "bottomNav" ) {
135
- return Scaffold (
136
- body: maintainState
137
- ? IndexedStack (index: currentIndex, children: [
138
- for (var page in pages.entries)
139
- Navigator (
140
- key: getNavigationKey (page),
141
- onGenerateRoute: (settings) => MaterialPageRoute (
142
- builder: (context) =>
143
- page.value.page ?? SizedBox .shrink (),
144
- settings: settings,
145
- ),
146
- )
147
- ])
148
- : Navigator (
149
- key: getNavigationKey (pages.entries.elementAt (currentIndex)),
150
- onGenerateRoute: (settings) => MaterialPageRoute (
151
- builder: (context) =>
152
- (pages.entries.elementAt (currentIndex).value.page ??
153
- SizedBox .shrink ()),
154
- settings: settings,
155
- ),
156
- ),
157
- bottomNavigationBar: BottomNavigationBar (
158
- currentIndex: currentIndex,
159
- onTap: onTap,
160
- selectedLabelStyle: layout? .selectedLabelStyle ??
161
- TextStyle (
162
- color: Colors .black,
163
- fontWeight: FontWeight .w600,
144
+ Widget body = maintainState
145
+ ? IndexedStack (index: currentIndex, children: [
146
+ for (var page in pages.entries)
147
+ Navigator (
148
+ key: getNavigationKey (page),
149
+ onGenerateRoute: (settings) => MaterialPageRoute (
150
+ builder: (context) => page.value.page ?? SizedBox .shrink (),
151
+ settings: settings,
152
+ ),
153
+ )
154
+ ])
155
+ : Navigator (
156
+ key: getNavigationKey (pages.entries.elementAt (getCurrentIndex)),
157
+ onGenerateRoute: (settings) => MaterialPageRoute (
158
+ builder: (context) =>
159
+ (pages.entries.elementAt (getCurrentIndex).value.page ??
160
+ SizedBox .shrink ()),
161
+ settings: settings,
164
162
),
165
- unselectedLabelStyle: layout? .unselectedLabelStyle ??
166
- TextStyle (
167
- color: Colors .black,
168
- fontWeight: FontWeight .w400,
169
- ),
170
- showSelectedLabels: layout? .showSelectedLabels ?? true ,
171
- showUnselectedLabels: layout? .showUnselectedLabels ?? true ,
172
- selectedItemColor: layout? .selectedItemColor ?? Colors .black,
173
- unselectedItemColor: layout? .unselectedItemColor ?? Colors .black,
174
- selectedFontSize: layout? .selectedFontSize ?? 14.0 ,
175
- unselectedFontSize: layout? .unselectedFontSize ?? 12.0 ,
176
- iconSize: layout? .iconSize ?? 24.0 ,
177
- elevation: layout? .elevation ?? 8.0 ,
178
- backgroundColor: layout? .backgroundColor,
179
- type: layout? .type ?? BottomNavigationBarType .fixed,
180
- items: [
181
- for (MapEntry <int , NavigationTab > page in pages.entries)
182
- _getBottomNavigationBarItem (page),
183
- ],
184
- ),
163
+ );
164
+
165
+ Widget ? bottomNavigationBar = BottomNavigationBar (
166
+ currentIndex: getCurrentIndex,
167
+ onTap: onTap,
168
+ selectedLabelStyle: layout? .selectedLabelStyle ??
169
+ TextStyle (
170
+ color: Colors .black,
171
+ fontWeight: FontWeight .w600,
172
+ ),
173
+ unselectedLabelStyle: layout? .unselectedLabelStyle ??
174
+ TextStyle (
175
+ color: Colors .black,
176
+ fontWeight: FontWeight .w400,
177
+ ),
178
+ showSelectedLabels: layout? .showSelectedLabels ?? true ,
179
+ showUnselectedLabels: layout? .showUnselectedLabels ?? true ,
180
+ selectedItemColor: layout? .selectedItemColor ?? Colors .black,
181
+ unselectedItemColor: layout? .unselectedItemColor ?? Colors .black,
182
+ selectedFontSize: layout? .selectedFontSize ?? 14.0 ,
183
+ unselectedFontSize: layout? .unselectedFontSize ?? 12.0 ,
184
+ iconSize: layout? .iconSize ?? 24.0 ,
185
+ elevation: layout? .elevation ?? 8.0 ,
186
+ backgroundColor: layout? .backgroundColor,
187
+ type: layout? .type ?? BottomNavigationBarType .fixed,
188
+ items: [
189
+ for (MapEntry <int , NavigationTab > page in pages.entries)
190
+ _getBottomNavigationBarItem (page),
191
+ ],
185
192
);
193
+ try {
194
+ return bottomNavBuilder (context, body, bottomNavigationBar);
195
+ } on UnimplementedError catch (_) {
196
+ return Scaffold (
197
+ body: body,
198
+ bottomNavigationBar: bottomNavigationBar,
199
+ );
200
+ }
186
201
}
202
+
187
203
if (layout? .kind == "topNav" ) {
188
204
return DefaultTabController (
189
205
length: pages.length,
190
- initialIndex: currentIndex ,
206
+ initialIndex: getCurrentIndex ,
191
207
animationDuration: layout? .animationDuration,
192
208
child: Scaffold (
193
209
appBar: AppBar (
194
210
backgroundColor: layout? .backgroundColor,
195
- title: pages[currentIndex ]? .title == null
211
+ title: pages[getCurrentIndex ]? .title == null
196
212
? null
197
- : Text (pages[currentIndex ]? .title ?? "" ),
213
+ : Text (pages[getCurrentIndex ]? .title ?? "" ),
198
214
toolbarHeight: (layout? .hideAppBarTitle ?? true ) ? 0 : null ,
199
215
bottom: TabBar (
200
216
isScrollable: layout? .isScrollable ?? false ,
@@ -232,14 +248,14 @@ abstract class NavigationHub<T extends StatefulWidget> extends NyState<T> {
232
248
icon: page.value.kind == "badge"
233
249
? BadgeTab .fromNavigationTab (page.value,
234
250
index: page.key,
235
- icon: currentIndex == page.key
251
+ icon: getCurrentIndex == page.key
236
252
? page.value.icon == null
237
253
? Text (page.value.title)
238
254
: page.value.activeIcon
239
255
: page.value.icon ?? Text (page.value.title),
240
256
stateName:
241
257
"${stateName }_navigation_tab_${page .key }" )
242
- : currentIndex == page.key
258
+ : getCurrentIndex == page.key
243
259
? page.value.activeIcon
244
260
: page.value.icon,
245
261
child: page.value.icon == null && page.value.kind != "badge"
@@ -251,7 +267,7 @@ abstract class NavigationHub<T extends StatefulWidget> extends NyState<T> {
251
267
),
252
268
),
253
269
body: maintainState
254
- ? IndexedStack (index: currentIndex , children: [
270
+ ? IndexedStack (index: getCurrentIndex , children: [
255
271
for (var page in pages.entries)
256
272
Navigator (
257
273
key: getNavigationKey (page),
0 commit comments