dialog_profile_time_input.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 const VDialogInput().show();
  80. if (result != null) {
  81. setState(() {
  82. initialValue.name = result;
  83. });
  84. }
  85. },
  86. ),
  87. VListFormCell(
  88. label: '时间',
  89. content: initialValue.time != null
  90. ? DateFormat('yyyy-MM-dd').format(initialValue.time!)
  91. : '',
  92. onTap: () async {
  93. var result = await const VDialogDate().show();
  94. if (result != null) {
  95. setState(() {
  96. initialValue.time = result;
  97. });
  98. }
  99. },
  100. )
  101. ],
  102. );
  103. }
  104. }