166 lines
4.3 KiB
Dart
166 lines
4.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../components/home/home_content.dart';
|
|
import '../components/settings/settings_content.dart';
|
|
import '../components/map/map_content.dart';
|
|
import '../components/group/group_content.dart';
|
|
import '../components/count/count_content.dart';
|
|
|
|
class HomePage extends StatefulWidget {
|
|
const HomePage({super.key});
|
|
|
|
@override
|
|
State<HomePage> createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends State<HomePage> {
|
|
int _currentIndex = 0;
|
|
|
|
// Cache pour les pages créées
|
|
final Map<int, Widget> _pageCache = {};
|
|
|
|
final List<String> titles = [
|
|
'Mes voyages', // 0
|
|
'Paramètres', // 1
|
|
'Carte', // 2
|
|
'Chat de groupe', // 3
|
|
'Comptes', // 4
|
|
];
|
|
|
|
Widget _buildPage(int index) {
|
|
// Vérifier si la page est déjà en cache
|
|
if (_pageCache.containsKey(index)) {
|
|
return _pageCache[index]!;
|
|
}
|
|
|
|
// Créer la page seulement quand elle est demandée
|
|
Widget page;
|
|
switch (index) {
|
|
case 0:
|
|
page = const HomeContent();
|
|
break;
|
|
case 1:
|
|
page = const SettingsContent();
|
|
break;
|
|
case 2:
|
|
page = const MapContent();
|
|
break;
|
|
case 3:
|
|
page = const GroupContent();
|
|
break;
|
|
case 4:
|
|
page = const CountContent();
|
|
break;
|
|
default:
|
|
page = const HomeContent();
|
|
}
|
|
|
|
// Mettre en cache la page créée
|
|
_pageCache[index] = page;
|
|
return page;
|
|
}
|
|
|
|
void _onNavigationTap(int index) {
|
|
setState(() {
|
|
_currentIndex = index;
|
|
});
|
|
Navigator.pop(context); // Fermer le drawer
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(titles[_currentIndex]),
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
foregroundColor: Colors.white,
|
|
),
|
|
drawer: Drawer(
|
|
child: ListView(
|
|
padding: EdgeInsets.zero,
|
|
children: [
|
|
DrawerHeader(
|
|
decoration: BoxDecoration(
|
|
color: Theme.of(context).colorScheme.inversePrimary,
|
|
),
|
|
child: const Text(
|
|
"Travel Mate",
|
|
style: TextStyle(color: Colors.white, fontSize: 24),
|
|
),
|
|
),
|
|
_buildDrawerItem(
|
|
icon: Icons.home,
|
|
title: "Mes voyages",
|
|
index: 0,
|
|
),
|
|
_buildDrawerItem(
|
|
icon: Icons.settings,
|
|
title: "Paramètres",
|
|
index: 1,
|
|
),
|
|
_buildDrawerItem(
|
|
icon: Icons.map,
|
|
title: "Carte",
|
|
index: 2,
|
|
),
|
|
_buildDrawerItem(
|
|
icon: Icons.group,
|
|
title: "Chat de groupe",
|
|
index: 3,
|
|
),
|
|
_buildDrawerItem(
|
|
icon: Icons.account_balance_wallet,
|
|
title: "Comptes",
|
|
index: 4,
|
|
),
|
|
const Divider(),
|
|
ListTile(
|
|
leading: const Icon(Icons.logout, color: Colors.red),
|
|
title: const Text("Déconnexion", style: TextStyle(color: Colors.red)),
|
|
onTap: () {
|
|
Navigator.pop(context);
|
|
Navigator.pushNamedAndRemoveUntil(
|
|
context,
|
|
'/login',
|
|
(route) => false,
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
body: IndexedStack(
|
|
index: _currentIndex,
|
|
children: [
|
|
// Créer les pages seulement si elles sont sélectionnées
|
|
for (int i = 0; i < titles.length; i++)
|
|
if (_currentIndex == i || _pageCache.containsKey(i))
|
|
_buildPage(i)
|
|
else
|
|
Container(), // Placeholder vide
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildDrawerItem({
|
|
required IconData icon,
|
|
required String title,
|
|
required int index,
|
|
}) {
|
|
return ListTile(
|
|
leading: Icon(icon),
|
|
title: Text(title),
|
|
selected: _currentIndex == index,
|
|
selectedTileColor: Theme.of(context).colorScheme.primary.withValues(alpha: 0.1),
|
|
onTap: () => _onNavigationTap(index),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
// Nettoyer le cache si nécessaire
|
|
_pageCache.clear();
|
|
super.dispose();
|
|
}
|
|
}
|