dialog_profile_time_input.dart 3.2 KB

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