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/_configs/theme_helper.dart'; 9 : import 'package:app_finance/_ext/build_context_ext.dart'; 10 : import 'package:app_finance/design/form/abstract_selector.dart'; 11 : import 'package:app_finance/design/wrapper/text_wrapper.dart'; 12 : import 'package:flutter/material.dart'; 13 : import 'package:intl/intl.dart'; 14 : import 'package:month_picker_dialog/month_picker_dialog.dart'; 15 : 16 : class MonthYearInput extends AbstractSelector { 17 : final Function setState; 18 : @override 19 : // ignore: overridden_fields 20 : final DateTime? value; 21 : final bool withLabel; 22 : final String? labelText; 23 : 24 0 : const MonthYearInput({ 25 : super.key, 26 : required this.setState, 27 : required this.value, 28 : this.withLabel = false, 29 : this.labelText, 30 : }); 31 : 32 0 : @override 33 0 : MonthYearInputState createState() => MonthYearInputState(); 34 : } 35 : 36 : class MonthYearInputState extends AbstractSelectorState<MonthYearInput> { 37 0 : @override 38 : Future<void> onTap(context) async { 39 0 : DateTime currentDate = DateTime.now(); 40 : const Duration dateRange = Duration(days: 20 * 365); 41 0 : DateTime firstDate = currentDate.subtract(dateRange); 42 0 : DateTime lastDate = currentDate.add(dateRange); 43 0 : final DateTime? selectedDate = await showMonthPicker( 44 : context: context, 45 0 : initialDate: widget.value ?? currentDate, 46 : firstDate: firstDate, 47 : lastDate: lastDate, 48 : ); 49 : if (selectedDate != null) { 50 0 : widget.setState(selectedDate); 51 0 : focusController.onEditingComplete(this); 52 : } 53 : } 54 : 55 0 : @override 56 : Widget buildContent(BuildContext context) { 57 0 : final indent = ThemeHelper.getIndent(); 58 0 : Widget title = widget.value != null 59 0 : ? TextWrapper( 60 0 : DateFormat.yM(AppLocale.code).format(widget.value!), 61 0 : style: context.textTheme.numberMedium, 62 : ) 63 0 : : TextWrapper( 64 0 : AppLocale.labels.dateTooltip, 65 0 : style: context.textTheme.tooltipMedium, 66 : ); 67 0 : return Container( 68 0 : color: context.colorScheme.fieldBackground, 69 0 : child: ListTile( 70 : minVerticalPadding: 0, 71 0 : contentPadding: EdgeInsets.fromLTRB( 72 : indent, 73 0 : widget.value != null && widget.withLabel ? 0 : 1, 74 : 0, 75 0 : widget.value != null && widget.withLabel ? 2 : 1, 76 : ), 77 0 : title: widget.value != null 78 0 : ? widget.withLabel 79 0 : ? Column( 80 0 : mainAxisAlignment: AppDesign.getAlignment<MainAxisAlignment>(), 81 0 : crossAxisAlignment: AppDesign.getAlignment(), 82 0 : children: [ 83 0 : TextWrapper(widget.labelText ?? '...', style: context.textTheme.tooltipSmall), 84 : title, 85 : ], 86 : ) 87 : : title 88 : : title, 89 0 : onTap: () => onTap(context), 90 : ), 91 : ); 92 : } 93 : }