Advertisement
amonnoris

main_page.dart

Oct 5th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | Source Code | 0 0
  1. // lib/pages/main_page.dart
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'home_page.dart';
  5. import 'configuration_list_page.dart';
  6. import 'connection_history_page.dart';
  7. import 'configuration_edit_page.dart'; // Import if needed
  8. import 'package:provider/provider.dart';
  9. import '../providers/app_state.dart';
  10.  
  11. class MainPage extends StatefulWidget {
  12. const MainPage({super.key});
  13.  
  14. @override
  15. State<MainPage> createState() => _MainPageState();
  16. }
  17.  
  18. class _MainPageState extends State<MainPage> {
  19. int _selectedIndex = 0; // Tracks the selected tab
  20.  
  21. // Initialize all pages here to keep their state
  22. late final List<Widget> _pages = <Widget>[
  23. HomePage(
  24. onNavigate: (int index) {
  25. setState(() {
  26. _selectedIndex = index;
  27. });
  28. },
  29. ),
  30. ConfigurationListPage(
  31. onNavigate: (int index) {
  32. setState(() {
  33. _selectedIndex = index;
  34. });
  35. },
  36. ),
  37. const ConnectionHistoryPage(),
  38. ];
  39.  
  40. void _onItemTapped(int index) {
  41. setState(() {
  42. _selectedIndex = index;
  43. });
  44. }
  45.  
  46. @override
  47. Widget build(BuildContext context) {
  48. final appState = Provider.of<AppState>(context);
  49.  
  50. return Scaffold(
  51. appBar: AppBar(
  52. title: const Text('Flutter VPN App'),
  53. ),
  54. body: IndexedStack(
  55. index: _selectedIndex,
  56. children: _pages,
  57. ),
  58. bottomNavigationBar: BottomNavigationBar(
  59. items: const <BottomNavigationBarItem>[
  60. BottomNavigationBarItem(
  61. icon: Icon(Icons.home),
  62. label: 'Home',
  63. ),
  64. BottomNavigationBarItem(
  65. icon: Icon(Icons.settings),
  66. label: 'Servers',
  67. ),
  68. BottomNavigationBarItem(
  69. icon: Icon(Icons.history),
  70. label: 'History',
  71. ),
  72. ],
  73. currentIndex: _selectedIndex,
  74. selectedItemColor: Colors.blue, // Active icon color
  75. unselectedItemColor: Colors.grey, // Inactive icon color
  76. onTap: _onItemTapped,
  77. ),
  78. floatingActionButton: _selectedIndex == 1 // Show FAB only on Servers tab
  79. ? FloatingActionButton(
  80. onPressed: appState.isConnected
  81. ? null // Disable FAB when connected
  82. : () async {
  83. await Navigator.push(
  84. context,
  85. MaterialPageRoute(
  86. builder: (context) => ConfigurationEditPage(),
  87. ),
  88. );
  89. },
  90. tooltip: appState.isConnected
  91. ? 'Cannot add while connected'
  92. : 'Add Configuration',
  93. child: const Icon(Icons.add),
  94. )
  95. : null,
  96. );
  97. }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement