datetime.dart 5.4 KB

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