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_locale.dart';
5 : import 'package:app_finance/_configs/theme_helper.dart';
6 : import 'package:app_finance/_ext/build_context_ext.dart';
7 : import 'package:app_finance/design/wrapper/text_wrapper.dart';
8 : import 'package:material_ui/material_ui.dart';
9 : import 'package:flutter/semantics.dart';
10 :
11 : typedef FnCall = void Function();
12 :
13 : class ToolbarButtonWidget extends StatefulWidget {
14 : final Color? color;
15 : final Color? borderColor;
16 : final Color? backgroundColor;
17 : final Color? selectedColor;
18 : final Offset? offset;
19 : final bool isWide;
20 : final FnCall onPressed;
21 : final String tooltip;
22 : final String? semanticLabel;
23 : final IconData icon;
24 : final IconData? selectedIcon;
25 : final bool? isSelected;
26 : final double? maxWidth;
27 :
28 1 : const ToolbarButtonWidget({
29 : super.key,
30 : required this.onPressed,
31 : required this.tooltip,
32 : required this.icon,
33 : this.color,
34 : this.selectedIcon,
35 : this.selectedColor,
36 : this.semanticLabel,
37 : this.isSelected,
38 : this.borderColor,
39 : this.backgroundColor,
40 : this.offset,
41 : this.maxWidth,
42 : this.isWide = false,
43 : });
44 :
45 1 : @override
46 1 : ToolbarButtonWidgetState createState() => ToolbarButtonWidgetState();
47 : }
48 :
49 : class ToolbarButtonWidgetState extends State<ToolbarButtonWidget> {
50 : late Color initColor;
51 2 : late Color color = _getColor();
52 : bool isHover = false;
53 :
54 1 : Color _getColor() {
55 2 : if (widget.color != null) {
56 2 : return widget.color!;
57 : }
58 2 : return widget.isWide ? context.colorScheme.onSurface : Colors.white70;
59 : }
60 :
61 0 : _onEnter(_) => setState(() {
62 0 : color = Colors.white54;
63 0 : isHover = true;
64 : });
65 :
66 0 : _onExit(_) => setState(() {
67 0 : color = _getColor();
68 0 : isHover = false;
69 : });
70 :
71 1 : @override
72 : Widget build(BuildContext context) {
73 1 : final indent = ThemeHelper.getIndent();
74 6 : initColor = widget.backgroundColor ?? context.colorScheme.surface.withValues(alpha: 0.1);
75 4 : final borderColor = widget.isWide ? context.colorScheme.onSurface : Colors.white30;
76 1 : return Semantics(
77 3 : attributedHint: AttributedString(AppLocale.labels.typeButton),
78 5 : attributedLabel: AttributedString(widget.semanticLabel ?? widget.tooltip),
79 1 : child: MouseRegion(
80 1 : onEnter: _onEnter,
81 1 : onExit: _onExit,
82 1 : child: Container(
83 : margin: const EdgeInsets.all(2.0),
84 : padding: const EdgeInsets.all(1.0),
85 : height: 42,
86 1 : decoration: BoxDecoration(
87 : shape: BoxShape.rectangle,
88 2 : color: isHover ? Colors.black54 : initColor,
89 3 : border: Border.all(color: widget.borderColor ?? borderColor, width: 1),
90 : ),
91 1 : child: Material(
92 : elevation: 0,
93 : color: Colors.transparent,
94 : borderRadius: BorderRadius.zero,
95 1 : child: Center(
96 2 : child: widget.isWide
97 1 : ? InkWell(
98 2 : onTap: widget.onPressed,
99 1 : child: Row(
100 1 : children: [
101 4 : Icon(widget.icon, color: color),
102 1 : Container(
103 6 : width: widget.maxWidth != null ? widget.maxWidth! - 2 * indent : null,
104 1 : padding: EdgeInsets.fromLTRB(indent, indent, indent, 0),
105 1 : child: TextWrapper(
106 2 : widget.tooltip,
107 6 : maxWidth: widget.maxWidth != null ? widget.maxWidth! - 3 * indent : null,
108 4 : style: context.textTheme.headlineSmall?.copyWith(color: color),
109 : ),
110 : ),
111 : ],
112 : ),
113 : )
114 1 : : IconButton(
115 2 : tooltip: widget.tooltip,
116 2 : onPressed: widget.onPressed,
117 2 : isSelected: widget.isSelected,
118 : hoverColor: Colors.transparent,
119 4 : icon: Icon(widget.icon, color: color),
120 2 : selectedIcon: widget.selectedIcon != null
121 0 : ? Icon(
122 0 : widget.selectedIcon,
123 0 : color: isHover ? color : widget.selectedColor,
124 : )
125 : : null,
126 : ),
127 : ),
128 : ),
129 : ),
130 : ),
131 : );
132 : }
133 : }
|