Files
submanager/lib/providers/navigation_provider.dart
2025-09-07 19:33:11 +09:00

107 lines
2.5 KiB
Dart

import 'package:flutter/material.dart';
class NavigationProvider extends ChangeNotifier {
int _currentIndex = 0;
final List<int> _navigationHistory = [0];
String _currentRoute = '/';
String _currentTitle = 'home';
int get currentIndex => _currentIndex;
List<int> get navigationHistory => List.unmodifiable(_navigationHistory);
String get currentRoute => _currentRoute;
String get currentTitle => _currentTitle;
static const Map<String, int> routeToIndex = {
'/': 0,
'/add-subscription': -1,
'/sms-scanner': 3,
'/analysis': 1,
'/settings': 4,
'/subscription-detail': -1,
};
static const Map<int, String> indexToRoute = {
0: '/',
1: '/analysis',
3: '/sms-scanner',
4: '/settings',
};
static const Map<int, String> indexToTitle = {
0: 'home',
1: 'analysis',
3: 'smsScanLabel',
4: 'settings',
};
void updateCurrentIndex(int index, {bool addToHistory = true}) {
if (_currentIndex == index) return;
_currentIndex = index;
_currentRoute = indexToRoute[index] ?? '/';
_currentTitle = indexToTitle[index] ?? 'home';
if (addToHistory && index >= 0) {
_navigationHistory.add(index);
if (_navigationHistory.length > 10) {
_navigationHistory.removeAt(0);
}
}
notifyListeners();
}
void updateByRoute(String route) {
final index = routeToIndex[route] ?? 0;
_currentRoute = route;
if (index >= 0) {
_currentIndex = index;
_currentTitle = indexToTitle[index] ?? 'home';
} else {
switch (route) {
case '/add-subscription':
_currentTitle = 'addSubscription';
break;
case '/subscription-detail':
_currentTitle = 'subscriptionDetail';
break;
default:
_currentTitle = 'home';
}
}
notifyListeners();
}
bool canPop() {
return _navigationHistory.length > 1;
}
void pop() {
if (_navigationHistory.length > 1) {
_navigationHistory.removeLast();
final previousIndex = _navigationHistory.last;
updateCurrentIndex(previousIndex, addToHistory: false);
}
}
void reset() {
_currentIndex = 0;
_currentRoute = '/';
_currentTitle = 'home';
_navigationHistory.clear();
_navigationHistory.add(0);
notifyListeners();
}
void clearHistoryAndGoHome() {
_currentIndex = 0;
_currentRoute = '/';
_currentTitle = 'home';
_navigationHistory.clear();
_navigationHistory.add(0);
notifyListeners();
}
}