import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:intl/intl.dart'; import 'package:vitalapp/architecture/app_parameters.dart'; import 'package:vitalapp/components/alert_dialog.dart'; import 'package:vitalapp/components/cell.dart'; import 'package:vitalapp/components/dialog_date.dart'; import 'package:vitalapp/components/dialog_input.dart'; class VProfileTimeInputModel { VProfileTimeInputModel({this.name, this.time}); late String? name; late DateTime? time; factory VProfileTimeInputModel.fromJson(Map jsonMap) { return VProfileTimeInputModel( name: jsonMap["name"], time: DateTime.parse(jsonMap["time"]), ); } Map toJson() { Map jsonMap = { "name": name, "time": time != null ? DateFormat('yyyy-MM-dd').format(time!) : "", }; return jsonMap; } } class VDialogProfileTimeInput extends StatefulWidget { const VDialogProfileTimeInput({ Key? key, this.title, this.initialValue, }) : super(key: key); final String? title; final VProfileTimeInputModel? initialValue; @override // ignore: library_private_types_in_public_api _VDialogProfileTimeInputState createState() => _VDialogProfileTimeInputState(); Future show() => VAlertDialog.showDialog(this); } class _VDialogProfileTimeInputState extends State { late VProfileTimeInputModel initialValue; @override void initState() { super.initState(); initialValue = widget.initialValue ?? VProfileTimeInputModel(); } @override Widget build(BuildContext context) { return VAlertDialog( title: widget.title, width: 440, contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24), content: _buildContent(context), showCancel: true, onConfirm: () { Get.back(result: initialValue); }, ); } Widget _buildContent(BuildContext context) { final children = []; children.add(_buildTimeWidget()); return SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: children, ), ); } Widget _buildTimeWidget() { return VListFormCellGroup( children: [ VListFormCell( label: '名称', content: initialValue.name, onTap: () async { var result = await VDialogInput( initialValue: initialValue.name, ).show(); if (result != null) { setState(() { initialValue.name = result; }); } }, ), VListFormCell( label: '时间', content: initialValue.time != null ? DateFormat('yyyy-MM-dd').format(initialValue.time!) : '', onTap: () async { DateTime? result; bool _isLocalStation = AppParameters.data.isLocalStation; if (kIsWeb || _isLocalStation) { result = await showDatePicker( context: Get.context!, initialDate: initialValue.time ?? DateTime.now(), firstDate: DateTime(1900), lastDate: DateTime(2100), ); } else { result = await VDialogDate( initialValue: initialValue.time, maxValue: DateTime.now(), ).show(); } if (result != null) { setState(() { initialValue.time = result; }); } }, ) ], ); } }