import 'package:fis_lib_report/converts/date_to_string_converter.dart'; import 'package:fis_lib_report/converts/pt_to_px_converter.dart'; import 'package:fis_lib_report/converts/text_size_converter.dart'; import 'package:fis_lib_report/converts/alignment_convert.dart'; import 'package:fis_lib_report/report/dateTimeElement.dart'; import 'package:fis_lib_report/report_info/date_time_info.dart'; import 'package:fis_lib_report/report_info/element_tag_names.dart'; import 'package:fis_lib_report/report_info/report_info.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class RDateTime extends StatefulWidget { final DateTimeElement dateTimeElement; RDateTime(this.dateTimeElement); @override State createState() { return _RDateTimeState(); } } class _RDateTimeState extends State { DateTime selectedDate = DateTime.now(); _RDateTimeState(); String _dateTime = ''; double _fontSize = 15.0; double _width = 0; TextStyle _style = const TextStyle(); Color _fontColor = Colors.black; Color _backgroundColor = const Color.fromARGB(255, 255, 255, 255); EdgeInsets _margin = const EdgeInsets.all(0); DateTimeInfo? _dateTimeInfo; @override initState() { super.initState(); } @override Widget build(BuildContext context) { final dateTimeElement = widget.dateTimeElement; _initDatas(); return Container( margin: _margin, decoration: BoxDecoration( border: Border.all( width: 1, color: dateTimeElement.isReadOnly! ? Colors.transparent : Colors.grey, ), ), width: _width, alignment: AlignmentConvert.verticalLayoutConvert( dateTimeElement.verticalAlignment!), child: Row( mainAxisAlignment: AlignmentConvert.horizontalToMainConvert( dateTimeElement.horizontalAlignment), children: [ Container( margin: _margin, alignment: AlignmentConvert.verticalLayoutConvert( dateTimeElement.verticalAlignment!), child: Text( _dateTime, style: _style, textAlign: AlignmentConvert.horizontalAlignmentConvert( dateTimeElement.horizontalAlignment), ), ), if (!dateTimeElement.isReadOnly!) ...[ const Expanded(child: SizedBox()), GestureDetector( onTap: () => _selectDate(context), child: const Icon( Icons.date_range_outlined, size: 20, ), ), ], ], )); } _selectDate(BuildContext context) async { final DateTime? picked = await showDatePicker( context: context, initialDate: selectedDate, // Refer step 1 firstDate: DateTime(2000), lastDate: DateTime(2025), ); if (picked != null && picked != selectedDate) { final format = widget.dateTimeElement.dateTimeFormat; setState(() { selectedDate = picked; _dateTimeConvert(format!, selectedDate.millisecondsSinceEpoch); }); } } void _initDatas() { final dateTimeElement = widget.dateTimeElement; final format = dateTimeElement.dateTimeFormat; final dateTimeInfo = ReportInfo.instance.getElementInfo(dateTimeElement) as DateTimeInfo?; if (format != null && dateTimeElement.tag!.name == TagNames.REPORTTIME && dateTimeInfo!.text.isEmpty) { _dateTimeConvert( format, ReportInfo.instance.reportDate!.millisecondsSinceEpoch); } if (dateTimeInfo != null && dateTimeInfo != _dateTimeInfo) { _dateTimeInfo = dateTimeInfo; _dateTimeInfo!.onTextChange.addListener((sender, e) { setState(() { final date = DateTime.parse(_dateTimeInfo!.text); _dateTimeConvert(format!, date.millisecondsSinceEpoch); }); }); } _fontSize = dateTimeElement.fontSize ?? 15.0; //TODO(Loki):常规模板暂未设置fontStyles,后续再支持 final fontStyles = dateTimeElement.fontStyles; final fontColor = dateTimeElement.fontColor; if (fontColor != null) { _fontColor = Color.fromARGB( fontColor.a!, fontColor.r!, fontColor.g!, fontColor.b!); } final backgroundColor = dateTimeElement.background; if (backgroundColor != null) { _backgroundColor = Color.fromARGB(backgroundColor.a!, backgroundColor.r!, backgroundColor.g!, backgroundColor.b!); } final margin = dateTimeElement.margin; if (margin != null) { _margin = EdgeInsets.only( top: margin.top ?? 0, bottom: margin.bottom ?? 0, left: margin.left ?? 0, right: margin.right ?? 0); } _style = TextStyle( fontSize: PtToPxConverter.ptToPx(_fontSize), color: _fontColor, backgroundColor: _backgroundColor, ); _width = PtToPxConverter.ptToPx( TextSizeConvert.getTextSize(_dateTime, _style).width); } void _dateTimeConvert(String format, int timestamp) { if (format == 'yyyy-MM-dd') { _dateTime = DateToStringConverter.dateAndTimeToString( timestamp, {"y-m": "-", "m-d": "-"}); } else if (format == 'yyyy年M月d日 HH:mm') { _dateTime = DateToStringConverter.dateAndTimeToString( timestamp, {"y-m": "年", "m-d": "月", "d-h": "日", "h-m": ":"}); } else if (format == 'yyyy-MM-dd HH:mm') { _dateTime = DateToStringConverter.dateAndTimeToString( timestamp, {"y-m": "-", "m-d": "-", "h-m": ":"}); } else if (format == "yyyy年M月d日") { _dateTime = DateToStringConverter.dateAndTimeToString( timestamp, {"y-m": "年", "m-d": "月", "d-h": "日"}); } else if (format == 'yyyy.MM.dd') { _dateTime = DateToStringConverter.dateAndTimeToString( timestamp, {"y-m": ".", "m-d": "."}); } else if (format == 'yyyy/MM/dd') { _dateTime = DateToStringConverter.dateAndTimeToString( timestamp, {"y-m": "/", "m-d": "/"}); } } }