Line data Source code
1 : // Copyright 2023 The terCAD team. All rights reserved.
2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
3 :
4 : import 'package:app_finance/_classes/herald/app_design.dart';
5 : import 'package:app_finance/_classes/structure/navigation/app_menu.dart';
6 : import 'package:app_finance/_classes/structure/navigation/app_menu_item.dart';
7 : import 'package:app_finance/_classes/structure/navigation/app_route.dart';
8 : import 'package:app_finance/_configs/screen_helper.dart';
9 : import 'package:app_finance/_configs/theme_helper.dart';
10 : import 'package:app_finance/_ext/build_context_ext.dart';
11 : import 'package:app_finance/design/wrapper/text_wrapper.dart';
12 : import 'package:material_ui/material_ui.dart';
13 :
14 : class MenuWidget extends StatelessWidget {
15 : final int index;
16 : final int selectedIndex;
17 : final Function setState;
18 : final FocusNode focus = FocusNode();
19 :
20 1 : MenuWidget({
21 : super.key,
22 : required this.index,
23 : required this.selectedIndex,
24 : required this.setState,
25 : });
26 :
27 1 : void _navigateToPage(String? currentRouteName, NavigatorState nav, String routeName) {
28 2 : setState();
29 2 : if (currentRouteName != AppRoute.homeRoute && nav.canPop()) {
30 1 : nav.pop();
31 : }
32 1 : nav.pushNamed(routeName);
33 : }
34 :
35 0 : void _onHover(bool isHovered) {
36 : if (isHovered) {
37 0 : setState();
38 : }
39 : }
40 :
41 1 : @override
42 : Widget build(BuildContext context) {
43 1 : final indent = ThemeHelper.getIndent();
44 1 : final isWide = ScreenHelper.state().isWide;
45 1 : final ColorScheme colorScheme = context.colorScheme;
46 1 : final TextTheme textTheme = context.textTheme;
47 3 : final isSelected = selectedIndex == index;
48 2 : Color color = isSelected ? colorScheme.primary : colorScheme.secondary;
49 2 : AppMenuItem menu = AppMenu.getByIndex(index);
50 1 : NavigatorState nav = Navigator.of(context);
51 3 : final currentRouteName = ModalRoute.of(context)?.settings.name;
52 :
53 1 : return InkWell(
54 3 : onTap: () => _navigateToPage(currentRouteName, nav, menu.route),
55 3 : focusNode: isSelected && !isWide ? (focus..requestFocus()) : focus,
56 1 : onHover: _onHover,
57 1 : child: Container(
58 1 : padding: EdgeInsets.all(indent),
59 : color: isSelected
60 : ? isWide
61 0 : ? colorScheme.surface
62 2 : : colorScheme.secondary.withValues(alpha: 0.1)
63 : : null,
64 1 : child: Row(
65 : crossAxisAlignment: CrossAxisAlignment.center,
66 1 : textDirection: AppDesign.getAlignment<TextDirection>(),
67 1 : children: [
68 2 : Icon(menu.icon, color: color),
69 1 : Padding(
70 1 : padding: EdgeInsets.only(left: indent, right: indent),
71 1 : child: TextWrapper(
72 1 : menu.name,
73 2 : style: textTheme.headlineMedium?.copyWith(color: color),
74 : ),
75 : ),
76 : ],
77 : ),
78 : ),
79 : );
80 : }
81 : }
|