datetime.dart 5.3 KB

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