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