dialog_profile_time_input.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'package:intl/intl.dart';
  5. import 'package:vitalapp/architecture/app_parameters.dart';
  6. import 'package:vitalapp/components/alert_dialog.dart';
  7. import 'package:vitalapp/components/cell.dart';
  8. import 'package:vitalapp/components/dialog_date.dart';
  9. import 'package:vitalapp/components/dialog_input.dart';
  10. class VProfileTimeInputModel {
  11. VProfileTimeInputModel({this.name, this.time});
  12. late String? name;
  13. late DateTime? time;
  14. factory VProfileTimeInputModel.fromJson(Map<String, dynamic> jsonMap) {
  15. return VProfileTimeInputModel(
  16. name: jsonMap["name"],
  17. time: DateTime.parse(jsonMap["time"]),
  18. );
  19. }
  20. Map<String, dynamic> toJson() {
  21. Map<String, dynamic> jsonMap = {
  22. "name": name,
  23. "time": time != null ? DateFormat('yyyy-MM-dd').format(time!) : "",
  24. };
  25. return jsonMap;
  26. }
  27. }
  28. class VDialogProfileTimeInput extends StatefulWidget {
  29. const VDialogProfileTimeInput({
  30. Key? key,
  31. this.title,
  32. this.initialValue,
  33. }) : super(key: key);
  34. final String? title;
  35. final VProfileTimeInputModel? initialValue;
  36. @override
  37. // ignore: library_private_types_in_public_api
  38. _VDialogProfileTimeInputState createState() =>
  39. _VDialogProfileTimeInputState();
  40. Future<VProfileTimeInputModel?> show<VProfileTimeInputModel>() =>
  41. VAlertDialog.showDialog<VProfileTimeInputModel>(this);
  42. }
  43. class _VDialogProfileTimeInputState extends State<VDialogProfileTimeInput> {
  44. late VProfileTimeInputModel initialValue;
  45. @override
  46. void initState() {
  47. super.initState();
  48. initialValue = widget.initialValue ?? VProfileTimeInputModel();
  49. }
  50. @override
  51. Widget build(BuildContext context) {
  52. return VAlertDialog(
  53. title: widget.title,
  54. width: 440,
  55. contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
  56. content: _buildContent(context),
  57. showCancel: true,
  58. onConfirm: () {
  59. Get.back(result: initialValue);
  60. },
  61. );
  62. }
  63. Widget _buildContent(BuildContext context) {
  64. final children = <Widget>[];
  65. children.add(_buildTimeWidget());
  66. return SingleChildScrollView(
  67. child: Column(
  68. mainAxisSize: MainAxisSize.min,
  69. crossAxisAlignment: CrossAxisAlignment.start,
  70. children: children,
  71. ),
  72. );
  73. }
  74. Widget _buildTimeWidget() {
  75. return VListFormCellGroup(
  76. children: [
  77. VListFormCell(
  78. label: '名称',
  79. content: initialValue.name,
  80. onTap: () async {
  81. var result = await VDialogInput(
  82. initialValue: initialValue.name,
  83. ).show();
  84. if (result != null) {
  85. setState(() {
  86. initialValue.name = result;
  87. });
  88. }
  89. },
  90. ),
  91. VListFormCell(
  92. label: '时间',
  93. content: initialValue.time != null
  94. ? DateFormat('yyyy-MM-dd').format(initialValue.time!)
  95. : '',
  96. onTap: () async {
  97. DateTime? result;
  98. bool _isLocalStation = AppParameters.data.isLocalStation;
  99. if (kIsWeb || _isLocalStation) {
  100. result = await showDatePicker(
  101. context: Get.context!,
  102. initialDate: initialValue.time ?? DateTime.now(),
  103. firstDate: DateTime(1900),
  104. lastDate: DateTime(2100),
  105. );
  106. } else {
  107. result = await VDialogDate(
  108. initialValue: initialValue.time,
  109. maxValue: DateTime.now(),
  110. ).show();
  111. }
  112. if (result != null) {
  113. setState(() {
  114. initialValue.time = result;
  115. });
  116. }
  117. },
  118. )
  119. ],
  120. );
  121. }
  122. }