|
1 | 1 | import 'package:flutter/material.dart'; |
2 | 2 | import 'design/images.dart'; |
| 3 | +import 'package:vpn_client/models/nav_item.dart'; |
3 | 4 |
|
4 | | -class NavBar extends StatefulWidget { |
| 5 | +class NavBar extends StatelessWidget { |
5 | 6 | final int initialIndex; |
6 | 7 | final Function(int) onItemTapped; |
| 8 | + final Color selectedColor; |
7 | 9 |
|
8 | | - const NavBar({super.key, this.initialIndex = 2, required this.onItemTapped}); |
9 | | - |
10 | | - @override |
11 | | - State<NavBar> createState() => NavBarState(); |
12 | | -} |
13 | | - |
14 | | -class NavBarState extends State<NavBar> { |
15 | | - late int _selectedIndex; |
16 | | - |
17 | | - final List<Widget> _inactiveIcons = [ |
18 | | - appIcon, |
19 | | - serverIcon, |
20 | | - homeIcon, |
21 | | - speedIcon, |
22 | | - settingsIcon, |
23 | | - ]; |
24 | | - |
25 | | - final List<Widget> _activeIcons = [ |
26 | | - activeAppIcon, |
27 | | - activeServerIcon, |
28 | | - activeHomeIcon, |
29 | | - speedIcon, |
30 | | - settingsIcon, |
31 | | - ]; |
32 | | - |
33 | | - @override |
34 | | - void initState() { |
35 | | - super.initState(); |
36 | | - _selectedIndex = widget.initialIndex; |
37 | | - } |
38 | | - |
39 | | - void _onItemTapped(int index) { |
40 | | - setState(() { |
41 | | - _selectedIndex = index; |
42 | | - }); |
43 | | - widget.onItemTapped(index); |
44 | | - } |
| 10 | + const NavBar({ |
| 11 | + super.key, |
| 12 | + this.initialIndex = 2, |
| 13 | + required this.onItemTapped, |
| 14 | + required this.selectedColor, |
| 15 | + }); |
45 | 16 |
|
46 | 17 | @override |
47 | 18 | Widget build(BuildContext context) { |
| 19 | + final List<NavItem> navItems = [ |
| 20 | + NavItem(inactiveIcon: appIcon, activeIcon: activeAppIcon), |
| 21 | + NavItem(inactiveIcon: serverIcon, activeIcon: activeServerIcon), |
| 22 | + NavItem(inactiveIcon: homeIcon, activeIcon: activeHomeIcon), |
| 23 | + NavItem(inactiveIcon: speedIcon, activeIcon: speedIcon), |
| 24 | + NavItem(inactiveIcon: settingsIcon, activeIcon: settingsIcon), |
| 25 | + ]; |
| 26 | + |
48 | 27 | return Container( |
49 | 28 | alignment: Alignment.center, |
50 | 29 | width: MediaQuery.of(context).size.width, |
51 | 30 | height: 60, |
52 | 31 | margin: const EdgeInsets.only(bottom: 30), |
53 | 32 | padding: const EdgeInsets.symmetric(horizontal: 30), |
54 | | - decoration: BoxDecoration(color: Theme.of(context).colorScheme.surface), |
| 33 | + decoration: |
| 34 | + BoxDecoration(color: Theme.of(context).colorScheme.surface), |
55 | 35 | child: Row( |
56 | | - children: List.generate(_inactiveIcons.length, (index) { |
57 | | - bool isActive = _selectedIndex == index; |
| 36 | + mainAxisAlignment: MainAxisAlignment.spaceAround, |
| 37 | + children: List.generate(navItems.length, (index) { |
| 38 | + bool isActive = initialIndex == index; |
58 | 39 | return GestureDetector( |
59 | | - onTap: () => _onItemTapped(index), |
60 | | - child: SizedBox( |
61 | | - width: (MediaQuery.of(context).size.width - 60) / 5, |
62 | | - child: AnimatedContainer( |
63 | | - duration: const Duration(milliseconds: 200), |
64 | | - curve: Curves.easeInOut, |
65 | | - padding: const EdgeInsets.all(8), |
66 | | - child: isActive ? _activeIcons[index] : _inactiveIcons[index], |
67 | | - ), |
| 40 | + onTap: () => onItemTapped(index), |
| 41 | + child: AnimatedContainer( |
| 42 | + duration: const Duration(milliseconds: 200), |
| 43 | + curve: Curves.easeInOut, |
| 44 | + padding: const EdgeInsets.all(8), |
| 45 | + child: isActive |
| 46 | + ? ColorFiltered( |
| 47 | + colorFilter: ColorFilter.mode( |
| 48 | + selectedColor, BlendMode.srcIn), |
| 49 | + child: navItems[index].activeIcon, |
| 50 | + ) |
| 51 | + : navItems[index].inactiveIcon, |
68 | 52 | ), |
69 | 53 | ); |
70 | 54 | }), |
|
0 commit comments