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/controller/focus_controller.dart';
5 : import 'package:app_finance/_classes/herald/app_design.dart';
6 : import 'package:app_finance/_configs/custom_color_scheme.dart';
7 : import 'package:app_finance/_configs/custom_text_theme.dart';
8 : import 'package:app_finance/_configs/theme_helper.dart';
9 : import 'package:app_finance/_ext/build_context_ext.dart';
10 : import 'package:app_finance/design/wrapper/focus_wrapper.dart';
11 : import 'package:material_ui/material_ui.dart';
12 : import 'package:flutter/semantics.dart';
13 : import 'package:flutter/services.dart';
14 :
15 : abstract class SimpleInputFormatter {
16 4 : static TextInputFormatter get filterDouble => TextInputFormatter.withFunction((oldValue, newValue) {
17 0 : final text = newValue.text.replaceAll(',', '.');
18 0 : if (!RegExp(r'^\d*\.?\d*$').hasMatch(text)) {
19 : return oldValue;
20 : }
21 0 : return newValue.copyWith(text: text);
22 : });
23 :
24 0 : static get filterInt => FilteringTextInputFormatter.allow(RegExp(r'\d+'));
25 : }
26 :
27 : class SimpleInput extends StatelessWidget {
28 : final Function? setState;
29 : final String? tooltip;
30 : final TextInputType type;
31 : final FocusController? focusController;
32 : final FocusNode? focusNode;
33 : final List<TextInputFormatter>? formatter;
34 : final TextEditingController controller;
35 : final bool obscure;
36 : final bool withLabel;
37 : final bool forceFocus;
38 : final Color? hintColor;
39 : final Function(String)? onFieldSubmitted;
40 :
41 7 : SimpleInput({
42 : super.key,
43 : required this.controller,
44 : this.onFieldSubmitted,
45 : this.focusController,
46 : this.focusNode,
47 : this.setState,
48 : this.tooltip,
49 : this.formatter,
50 : this.hintColor,
51 : this.type = TextInputType.text,
52 : this.obscure = false,
53 : this.withLabel = false,
54 : this.forceFocus = false,
55 : }) {
56 7 : if (setState != null) {
57 0 : controller.addListener(() => setState!(controller.text));
58 : }
59 : }
60 :
61 7 : @override
62 : Widget build(BuildContext context) {
63 7 : final indent = ThemeHelper.getIndent(1.8);
64 7 : final textTheme = context.textTheme;
65 14 : final focusController = this.focusController ?? FocusWrapper.of(context);
66 7 : return Directionality(
67 7 : textDirection: AppDesign.getAlignment<TextDirection>(),
68 7 : child: Semantics(
69 : container: true,
70 : textField: true,
71 7 : label: tooltip,
72 21 : attributedHint: tooltip != null ? AttributedString(tooltip!) : null,
73 7 : child: TextFormField(
74 7 : controller: controller,
75 7 : inputFormatters: formatter,
76 7 : style: textTheme.numberMedium,
77 14 : keyboardAppearance: context.colorScheme.brightness,
78 7 : obscureText: obscure,
79 : obscuringCharacter: '*',
80 7 : keyboardType: type,
81 27 : focusNode: focusNode ?? focusController?.bind(controller, context: context, value: controller.text),
82 10 : textInputAction: focusController?.getAction(controller),
83 3 : onTap: () => focusController?.onFocus(controller),
84 0 : onEditingComplete: () => focusController?.onEditingComplete(controller),
85 7 : onFieldSubmitted: onFieldSubmitted,
86 18 : autofocus: forceFocus ? forceFocus : focusController?.isFocused(controller, context) ?? false,
87 7 : decoration: InputDecoration(
88 : filled: true,
89 : border: InputBorder.none,
90 14 : fillColor: context.colorScheme.fieldBackground,
91 21 : contentPadding: EdgeInsets.fromLTRB(indent / 1.5, withLabel ? 1 : indent, 0, indent),
92 14 : hintText: withLabel ? null : tooltip,
93 7 : hintStyle: textTheme.tooltipMedium,
94 8 : labelText: withLabel ? tooltip : null,
95 7 : labelStyle: textTheme.tooltipMedium,
96 : ),
97 : ),
98 : ),
99 : );
100 : }
101 : }
|