datetime.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import 'package:fis_lib_report/converts/date_to_string_converter.dart';
  2. import 'package:fis_lib_report/converts/pt_to_px_converter.dart';
  3. import 'package:fis_lib_report/converts/text_size_converter.dart';
  4. import 'package:fis_lib_report/converts/alignment_convert.dart';
  5. import 'package:fis_lib_report/report/dateTimeElement.dart';
  6. import 'package:fis_lib_report/report_info/date_time_info.dart';
  7. import 'package:fis_lib_report/report_info/element_tag_names.dart';
  8. import 'package:fis_lib_report/report_info/report_info.dart';
  9. import 'package:flutter/cupertino.dart';
  10. import 'package:flutter/material.dart';
  11. class RDateTime extends StatefulWidget {
  12. final DateTimeElement dateTimeElement;
  13. RDateTime(this.dateTimeElement);
  14. @override
  15. State<StatefulWidget> createState() {
  16. return _RDateTimeState();
  17. }
  18. }
  19. class _RDateTimeState extends State<RDateTime> {
  20. DateTime selectedDate = DateTime.now();
  21. _RDateTimeState();
  22. String _dateTime = '';
  23. double _fontSize = 15.0;
  24. double _width = 0;
  25. TextStyle _style = const TextStyle();
  26. Color _fontColor = Colors.black;
  27. Color _backgroundColor = const Color.fromARGB(255, 255, 255, 255);
  28. EdgeInsets _margin = const EdgeInsets.all(0);
  29. DateTimeInfo? _dateTimeInfo;
  30. @override
  31. initState() {
  32. super.initState();
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. final dateTimeElement = widget.dateTimeElement;
  37. _initDatas();
  38. return Container(
  39. margin: _margin,
  40. decoration: BoxDecoration(
  41. border: Border.all(
  42. width: 1,
  43. color:
  44. dateTimeElement.isReadOnly! ? Colors.transparent : Colors.grey,
  45. ),
  46. ),
  47. width: _width,
  48. alignment: AlignmentConvert.verticalLayoutConvert(
  49. dateTimeElement.verticalAlignment!),
  50. child: Row(
  51. mainAxisAlignment: AlignmentConvert.horizontalToMainConvert(
  52. dateTimeElement.horizontalAlignment),
  53. children: [
  54. Container(
  55. margin: _margin,
  56. alignment: AlignmentConvert.verticalLayoutConvert(
  57. dateTimeElement.verticalAlignment!),
  58. child: Text(
  59. _dateTime,
  60. style: _style,
  61. textAlign: AlignmentConvert.horizontalAlignmentConvert(
  62. dateTimeElement.horizontalAlignment),
  63. ),
  64. ),
  65. if (!dateTimeElement.isReadOnly!) ...[
  66. const Expanded(child: SizedBox()),
  67. GestureDetector(
  68. onTap: () => _selectDate(context),
  69. child: const Icon(
  70. Icons.date_range_outlined,
  71. size: 20,
  72. ),
  73. ),
  74. ],
  75. ],
  76. ));
  77. }
  78. _selectDate(BuildContext context) async {
  79. final DateTime? picked = await showDatePicker(
  80. context: context,
  81. initialDate: selectedDate, // Refer step 1
  82. firstDate: DateTime(2000),
  83. lastDate: DateTime(2025),
  84. );
  85. if (picked != null && picked != selectedDate) {
  86. final format = widget.dateTimeElement.dateTimeFormat;
  87. setState(() {
  88. selectedDate = picked;
  89. _dateTimeConvert(format!, selectedDate.millisecondsSinceEpoch);
  90. });
  91. }
  92. }
  93. void _initDatas() {
  94. final dateTimeElement = widget.dateTimeElement;
  95. final format = dateTimeElement.dateTimeFormat;
  96. final dateTimeInfo =
  97. ReportInfo.instance.getElementInfo(dateTimeElement) as DateTimeInfo?;
  98. if (format != null &&
  99. dateTimeElement.tag!.name == TagNames.REPORTTIME &&
  100. dateTimeInfo!.text.isEmpty) {
  101. _dateTimeConvert(
  102. format, ReportInfo.instance.reportDate!.millisecondsSinceEpoch);
  103. }
  104. if (dateTimeInfo != null && dateTimeInfo != _dateTimeInfo) {
  105. _dateTimeInfo = dateTimeInfo;
  106. _dateTimeInfo!.onTextChange.addListener((sender, e) {
  107. setState(() {
  108. final date = DateTime.parse(_dateTimeInfo!.text);
  109. _dateTimeConvert(format!, date.millisecondsSinceEpoch);
  110. });
  111. });
  112. }
  113. _fontSize = dateTimeElement.fontSize ?? 15.0;
  114. //TODO(Loki):常规模板暂未设置fontStyles,后续再支持
  115. final fontStyles = dateTimeElement.fontStyles;
  116. final fontColor = dateTimeElement.fontColor;
  117. if (fontColor != null) {
  118. _fontColor = Color.fromARGB(
  119. fontColor.a!, fontColor.r!, fontColor.g!, fontColor.b!);
  120. }
  121. final backgroundColor = dateTimeElement.background;
  122. if (backgroundColor != null) {
  123. _backgroundColor = Color.fromARGB(backgroundColor.a!, backgroundColor.r!,
  124. backgroundColor.g!, backgroundColor.b!);
  125. }
  126. final margin = dateTimeElement.margin;
  127. if (margin != null) {
  128. _margin = EdgeInsets.only(
  129. top: margin.top ?? 0,
  130. bottom: margin.bottom ?? 0,
  131. left: margin.left ?? 0,
  132. right: margin.right ?? 0);
  133. }
  134. _style = TextStyle(
  135. fontSize: PtToPxConverter.ptToPx(_fontSize),
  136. color: _fontColor,
  137. backgroundColor: _backgroundColor,
  138. );
  139. _width = PtToPxConverter.ptToPx(
  140. TextSizeConvert.getTextSize(_dateTime, _style).width);
  141. }
  142. void _dateTimeConvert(String format, int timestamp) {
  143. if (format == 'yyyy-MM-dd') {
  144. _dateTime = DateToStringConverter.dateAndTimeToString(
  145. timestamp, {"y-m": "-", "m-d": "-"});
  146. } else if (format == 'yyyy年M月d日 HH:mm') {
  147. _dateTime = DateToStringConverter.dateAndTimeToString(
  148. timestamp, {"y-m": "年", "m-d": "月", "d-h": "日", "h-m": ":"});
  149. } else if (format == 'yyyy-MM-dd HH:mm') {
  150. _dateTime = DateToStringConverter.dateAndTimeToString(
  151. timestamp, {"y-m": "-", "m-d": "-", "h-m": ":"});
  152. } else if (format == "yyyy年M月d日") {
  153. _dateTime = DateToStringConverter.dateAndTimeToString(
  154. timestamp, {"y-m": "年", "m-d": "月", "d-h": "日"});
  155. } else if (format == 'yyyy.MM.dd') {
  156. _dateTime = DateToStringConverter.dateAndTimeToString(
  157. timestamp, {"y-m": ".", "m-d": "."});
  158. } else if (format == 'yyyy/MM/dd') {
  159. _dateTime = DateToStringConverter.dateAndTimeToString(
  160. timestamp, {"y-m": "/", "m-d": "/"});
  161. }
  162. }
  163. }