LCOV - code coverage report
Current view: top level - lib/design/form - date_input.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 17 38 44.7 %
Date: 2024-10-04 11:12:13 Functions: 0 0 -

          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:flutter/material.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           1 :   const DateInput({
      22             :     super.key,
      23             :     required this.setState,
      24             :     required this.value,
      25             :     this.withLabel = false,
      26           1 :   }) : super(value: value);
      27             : 
      28           1 :   @override
      29           1 :   DateInputState createState() => DateInputState();
      30             : }
      31             : 
      32             : class DateInputState extends AbstractSelectorState<DateInput> {
      33           0 :   @override
      34             :   Future<void> onTap(BuildContext context) async {
      35           0 :     DateTime currentDate = DateTime.now();
      36             :     const Duration dateRange = Duration(days: 20 * 365);
      37           0 :     DateTime firstDate = currentDate.subtract(dateRange);
      38           0 :     DateTime lastDate = currentDate.add(dateRange);
      39           0 :     DateTime? selectedDate = await showDatePicker(
      40             :       context: context,
      41           0 :       initialDate: widget.value ?? currentDate,
      42             :       firstDate: firstDate,
      43             :       lastDate: lastDate,
      44             :       initialEntryMode: DatePickerEntryMode.input,
      45             :     );
      46             :     if (selectedDate != null) {
      47           0 :       if (widget.value != null) {
      48           0 :         selectedDate = selectedDate.add(Duration(
      49           0 :           hours: widget.value?.hour ?? 0,
      50           0 :           minutes: widget.value?.minute ?? 0,
      51           0 :           seconds: widget.value?.second ?? 0,
      52             :         ));
      53             :       }
      54           0 :       widget.setState(selectedDate);
      55           0 :       focusController.onEditingComplete(this);
      56             :     }
      57             :   }
      58             : 
      59           1 :   @override
      60             :   Widget buildContent(BuildContext context) {
      61           2 :     final style = context.textTheme.numberMedium;
      62           2 :     final DateFormat formatterDate = DateFormat.yMd(AppLocale.code);
      63           5 :     final labelStyle = context.textTheme.tooltipSmall.copyWith(color: style.color?.withOpacity(0.4));
      64           3 :     final hintStyle = context.textTheme.tooltipMedium.copyWith(overflow: TextOverflow.ellipsis);
      65           1 :     return Container(
      66           2 :       color: context.colorScheme.fieldBackground,
      67           1 :       child: ListTile(
      68           2 :         title: widget.value != null
      69           2 :             ? widget.withLabel
      70           0 :                 ? Column(
      71           0 :                     mainAxisAlignment: AppDesign.getAlignment<MainAxisAlignment>(),
      72           0 :                     crossAxisAlignment: AppDesign.getAlignment(),
      73           0 :                     children: [
      74           0 :                       TextWrapper(AppLocale.labels.dateTooltip, style: labelStyle),
      75           0 :                       TextWrapper(formatterDate.format(widget.value!), style: style),
      76             :                     ],
      77             :                   )
      78           4 :                 : TextWrapper(formatterDate.format(widget.value!), style: style)
      79           0 :             : TextWrapper(AppLocale.labels.dateTooltip, style: hintStyle),
      80           1 :         focusNode: focus,
      81           2 :         autofocus: focusController.isFocused(this),
      82           0 :         onTap: () => onTap(context),
      83             :       ),
      84             :     );
      85             :   }
      86             : }

Generated by: LCOV version 1.14