exam_date.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import 'package:fis_common/index.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:get/get.dart';
  6. import 'package:intl/intl.dart';
  7. import 'package:vitalapp/components/dialog_date.dart';
  8. import 'package:vitalapp/components/dialog_time.dart';
  9. import 'package:vitalapp/pages/check/models/form.dart';
  10. import 'package:vitalapp/pages/form/form_info.dart';
  11. import 'exam_card.dart';
  12. class ExamDate extends StatefulWidget {
  13. final FormObject currentFormObject;
  14. final String? currentInputValue;
  15. const ExamDate({
  16. required this.currentFormObject,
  17. this.currentInputValue,
  18. });
  19. @override
  20. State<StatefulWidget> createState() {
  21. return ExamDateState();
  22. }
  23. }
  24. class ExamDateState extends State<ExamDate> {
  25. String _currentValue = '';
  26. bool _isDisabledValue = false;
  27. @override
  28. void initState() {
  29. _currentValue = widget.currentInputValue ?? '';
  30. if (FormInfo.instance.formValue.containsKey(widget.currentFormObject.key)) {
  31. _currentValue = FormInfo.instance.formValue[widget.currentFormObject.key];
  32. }
  33. super.initState();
  34. FormInfo.instance.onChangeTargetValue.addListener(_onChangeTargetValue);
  35. }
  36. @override
  37. void dispose() {
  38. FormInfo.instance.onChangeTargetValue.removeListener(_onChangeTargetValue);
  39. super.dispose();
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. return ExamCard(
  44. title: widget.currentFormObject.label ?? '',
  45. color: _isDisabledValue ? Colors.grey[300]! : Colors.white,
  46. clickCard: _clickCard,
  47. content: Container(
  48. alignment: Alignment.bottomRight,
  49. padding: const EdgeInsets.only(
  50. right: 30,
  51. left: 40,
  52. ),
  53. child: Text(
  54. _currentValue,
  55. style: TextStyle(
  56. fontSize: 26,
  57. color: Colors.black,
  58. fontFamily: "NotoSansSC",
  59. fontFamilyFallback: const ["NotoSansSC"],
  60. ),
  61. ),
  62. ),
  63. );
  64. }
  65. Future<void> _clickCard() async {
  66. if (_isDisabledValue) {
  67. return;
  68. }
  69. DateTime now = DateTime.now();
  70. DateTime? result;
  71. DateTime initialDate = now;
  72. if (_currentValue.isNotNullOrEmpty) {
  73. initialDate = DateTime.parse(_currentValue);
  74. }
  75. if (kIsWeb) {
  76. result = await showDatePicker(
  77. context: Get.context!,
  78. initialDate: initialDate,
  79. firstDate: DateTime(1900),
  80. lastDate: DateTime(2100),
  81. );
  82. } else {
  83. result = await VDialogDate(
  84. minimumDate: DateTime(1900),
  85. initialValue: initialDate,
  86. ).show();
  87. }
  88. if (result != null) {
  89. final DateFormat formatter = DateFormat('yyyy-MM-dd');
  90. var value = formatter.format(result);
  91. setState(() {
  92. _currentValue = value;
  93. });
  94. FormInfo.instance.formValue[widget.currentFormObject.key!] = value;
  95. FormInfo.instance.onValueChange.emit(
  96. this,
  97. UpdateFormArgs(
  98. sourceKey: widget.currentFormObject.key ?? '',
  99. sourceValue: value,
  100. ),
  101. );
  102. }
  103. }
  104. void _onChangeTargetValue(Object sender, TargetFormArgs e) {
  105. if (e.targetKey != widget.currentFormObject.key) {
  106. return;
  107. }
  108. if (e.isDisabledValue) {
  109. setState(() {
  110. _isDisabledValue = true;
  111. });
  112. } else if (!e.isDisabledValue) {
  113. setState(() {
  114. _isDisabledValue = false;
  115. });
  116. }
  117. setState(() {
  118. _currentValue = e.targetValue;
  119. });
  120. }
  121. }