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/herald/app_locale.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/_ext/build_context_ext.dart';
9 : import 'package:app_finance/design/form/abstract_selector.dart';
10 : import 'package:app_finance/design/wrapper/text_wrapper.dart';
11 : import 'package:material_ui/material_ui.dart';
12 : import 'package:intl/intl.dart';
13 :
14 : class DateInput extends AbstractSelector {
15 : final Function setState;
16 : @override
17 : // ignore: overridden_fields
18 : final DateTime? value;
19 : final bool withLabel;
20 :
21 4 : const DateInput({
22 : super.key,
23 : required this.setState,
24 : required this.value,
25 : this.withLabel = false,
26 4 : }) : super(value: value);
27 :
28 4 : @override
29 4 : DateInputState createState() => DateInputState();
30 : }
31 :
32 : class DateInputState<T extends DateInput> extends AbstractSelectorState<T> {
33 0 : @override
34 : Future<void> onTap(BuildContext context) async {
35 0 : FocusManager.instance.primaryFocus?.unfocus();
36 0 : DateTime currentDate = DateTime.now();
37 : const Duration dateRange = Duration(days: 20 * 365);
38 0 : DateTime firstDate = currentDate.subtract(dateRange);
39 0 : DateTime lastDate = currentDate.add(dateRange);
40 0 : DateTime? selectedDate = await showDatePicker(
41 : context: context,
42 0 : initialDate: widget.value ?? currentDate,
43 : firstDate: firstDate,
44 : lastDate: lastDate,
45 : initialEntryMode: DatePickerEntryMode.calendar,
46 0 : locale: AppLocale.get(),
47 : );
48 : if (selectedDate != null) {
49 0 : if (widget.value != null) {
50 0 : selectedDate = selectedDate.add(Duration(
51 0 : hours: widget.value?.hour ?? 0,
52 0 : minutes: widget.value?.minute ?? 0,
53 0 : seconds: widget.value?.second ?? 0,
54 : ));
55 : }
56 0 : widget.setState(selectedDate);
57 0 : focusController.onEditingComplete(this);
58 : }
59 : }
60 :
61 4 : String formatDate(DateTime? date) {
62 4 : final DateFormat formatter = DateFormat.yMd(AppLocale.code);
63 4 : return formatter.format(date!);
64 : }
65 :
66 4 : @override
67 : Widget buildContent(BuildContext context) {
68 8 : final style = context.textTheme.numberMedium;
69 20 : final labelStyle = context.textTheme.tooltipSmall.copyWith(color: style.color?.withValues(alpha: 0.4));
70 12 : final hintStyle = context.textTheme.tooltipMedium.copyWith(overflow: TextOverflow.ellipsis);
71 4 : return Material(
72 8 : color: context.colorScheme.fieldBackground,
73 4 : child: ListTile(
74 8 : title: widget.value != null
75 8 : ? widget.withLabel
76 0 : ? Column(
77 0 : mainAxisAlignment: AppDesign.getAlignment<MainAxisAlignment>(),
78 0 : crossAxisAlignment: AppDesign.getAlignment(),
79 0 : children: [
80 0 : TextWrapper(AppLocale.labels.dateTooltip, style: labelStyle),
81 0 : TextWrapper(formatDate(widget.value!), style: style),
82 : ],
83 : )
84 16 : : TextWrapper(formatDate(widget.value!), style: style)
85 0 : : TextWrapper(AppLocale.labels.dateTooltip, style: hintStyle),
86 4 : focusNode: focus,
87 8 : autofocus: focusController.isFocused(this),
88 0 : onTap: () => onTap(context),
89 : ),
90 : );
91 : }
92 : }
|