RDateTime.dart 5.3 KB

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