Skip to content

Commit 7379550

Browse files
committed
refactor: simplify current shell index retrieval logic
1 parent 66fa2f7 commit 7379550

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

lib/screens/bottom_navigation_page.dart

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -194,30 +194,22 @@ class _BottomNavigationPageState extends State<BottomNavigationPage> {
194194
}
195195

196196
int _getCurrentIndex(List<_NavigationItem> items, bool isOfflineMode) {
197-
final currentIndex = widget.child.currentIndex;
197+
final currentShellIndex = widget.child.currentIndex;
198198

199-
// Add bounds checking
200-
if (currentIndex < 0 || items.isEmpty) {
201-
return 0;
202-
}
203-
204-
// Map shell index to navigation items index
205-
for (var i = 0; i < items.length; i++) {
206-
if (items[i].shellIndex == currentIndex) {
207-
return i;
208-
}
209-
}
199+
if (items.isEmpty) return 0;
210200

211-
// Handle edge cases more robustly
212-
if (isOfflineMode && currentIndex == 1) {
213-
return 0; // Search -> Home
214-
}
201+
// Try to find the current shell index in the available items
202+
final matchedIndex = items.indexWhere(
203+
(item) => item.shellIndex == currentShellIndex,
204+
);
205+
if (matchedIndex != -1) return matchedIndex;
215206

216-
if (isOfflineMode && currentIndex > 1) {
217-
return (currentIndex - 1).clamp(0, items.length - 1);
218-
}
207+
// If the Search branch (1) is active but Search is hidden in offline mode,
208+
// fall back to the Home tab.
209+
if (isOfflineMode && currentShellIndex == 1) return 0;
219210

220-
return currentIndex.clamp(0, items.length - 1);
211+
// Final fallback: return the first tab to keep UI in a valid state.
212+
return 0;
221213
}
222214
}
223215

0 commit comments

Comments
 (0)