import 'package:eitc_erm_app/utils/Component.dart'; import 'package:eitc_erm_app/utils/Constants.dart'; import 'package:eitc_erm_app/utils/Utils.dart'; import 'package:eitc_erm_app/utils/logger.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:http/http.dart' as http; import 'bean/dropdown_list.dart'; import 'bean/normal_response3.dart'; import 'bean/user_list.dart'; /*void main() { WidgetsFlutterBinding.ensureInitialized(); runApp(AddPatient()); }*/ UserListEntity mUserListEntity = new UserListEntity(); int mWhichPatient = 0; bool isEdit = false; class AddPatient extends StatefulWidget { AddPatient({required Key key, required userListEntity, required whichPatient}) : super(key: key) { mUserListEntity = userListEntity; mWhichPatient = whichPatient; mWhichPatient == -1 ? isEdit = false : isEdit = true; logd(isEdit.toString() + ' ================='); } @override State createState() => AddPatientState(); } class AddPatientState extends State { int selectedRadioValue = 0; bool switchSelected = true; String relationship = "请选择与本人的关系"; String passport = "请选择证件类型"; String previous = "请选择既往病史"; String family = "请选择家族病史"; TextEditingController nameController = TextEditingController(); TextEditingController passportController = TextEditingController(); TextEditingController mobileController = TextEditingController(); TextEditingController allergyController = TextEditingController(); TextEditingController homeAddressController = TextEditingController(); bool mobileCanEdit = true; List selectItem0 = []; List selectItem1 = []; List selectItem2 = []; List selectItem3 = []; void getDropdownList() { for (int i = 0; i < 4; i++) { dropdownList(i); } } Future dropdownList(int i) async { String url = '${Global.BaseUrl}system/dropdownList?dictType=erm_personal_relationship'; logd(url); switch (i) { case 1: url = '${Global.BaseUrl}system/type/erm_document_type'; break; case 2: url = '${Global.BaseUrl}system/dropdownList?dictType=erm_past_history'; break; case 3: url = '${Global.BaseUrl}system/dropdownList?dictType=erm_family_history'; break; } final response = await http.get(Uri.parse(url), headers: jsonHeaders(withToken: true)); if (response.statusCode == 200) { final json = decodeBodyToJson(response.bodyBytes); logd("下拉列表$json"); DropdownList mDropdownList = DropdownList.fromJson(json); if (mDropdownList.code == Global.responseSuccessCode) { switch (i) { case 0: mDropdownList.data?.forEach((item) { selectItem0.add(item.dictValue); }); break; case 1: mDropdownList.data?.forEach((item) { selectItem1.add(item.dictValue); }); break; case 2: mDropdownList.data?.forEach((item) { selectItem2.add(item.dictValue); }); break; case 3: mDropdownList.data?.forEach((item) { selectItem3.add(item.dictValue); }); break; } if (mounted) { setState(() {}); } } else { Component.toast(mDropdownList.msg.toString(), 0); return null; } return mDropdownList; } else { Component.toast("出错了,请稍后再试!", 0); return null; } } @override void initState() { super.initState(); getDropdownList(); if (isEdit) { relationship = mUserListEntity.data![mWhichPatient].userRelationship.toString(); nameController.text = mUserListEntity.data![mWhichPatient].patientName.toString(); int? passportInt = mUserListEntity.data![mWhichPatient].documentType; switch (passportInt) { case 0: passport = "身份证"; break; case 1: passport = "护照"; break; case 2: passport = "港澳通行证"; break; } if (relationship == "本人") { mobileCanEdit = false; } else { mobileCanEdit = true; } passportController.text = mUserListEntity.data![mWhichPatient].identificationCard.toString(); previous = mUserListEntity.data![mWhichPatient].pastHistory.toString(); logd("------------------------ " + mUserListEntity.data![mWhichPatient].pastHistory.toString()); family = mUserListEntity.data![mWhichPatient].familyHistory.toString(); if (!mUserListEntity.data![mWhichPatient].allergyHistory .toString() .isEmpty) { selectedRadioValue = 1; allergyController.text = mUserListEntity.data![mWhichPatient].allergyHistory.toString(); } mobileController.text = mUserListEntity.data![mWhichPatient].patientPhone.toString(); homeAddressController.text = mUserListEntity.data![mWhichPatient].homeAddress.toString(); if (mUserListEntity.data![mWhichPatient].isDefault == 1) { switchSelected = true; } else { switchSelected = false; } } } final GlobalKey _formKey = GlobalKey(); String _errorText = ''; Future _validateInputs() async { final FormState? form = _formKey.currentState; /*if (form!.validate()) { _errorText = ''; } else { _errorText = '输入不能为空'; } form.save();*/ setState(() { if (relationship == "请选择与本人的关系") { _errorText = "请选择与本人的关系"; return; } else if (passport == "请选择证件类型") { _errorText = "请选择证件类型"; return; } else if (previous == "请选择既往病史") { _errorText = "请选择既往病史"; return; } else if (family == "请选择家族病史") { _errorText = "请选择家族病史"; return; } else if (nameController.text == '') { _errorText = "请填写姓名"; return; } else if (passportController.text == '') { _errorText = "请填写证件号码"; return; } else if (mobileController.text == '') { _errorText = "请填写手机号码"; return; } else if (selectedRadioValue == 1 && allergyController.text == '') { _errorText = "请填写过敏史"; return; } else if (!Utils.validateIDCard(passportController.text)) { _errorText = "请输入正确的身份证号码"; return; } else if (!Utils.isChinaPhoneLegal(mobileController.text)) { _errorText = "请输入正确的手机号码"; return; } else { _errorText = ""; } }); if (_errorText.isNotEmpty) { return; } logd("${previous} ${family} ${allergyController.text} "); int passportType = 0; if (passport == "身份证") { passportType = 0; } else if (passport == "护照") { passportType = 1; } else if (passport == "港澳通行证") { passportType = 2; } int isDefault = 1; if (switchSelected) { isDefault = 1; } else { isDefault = 0; } String patientId = isEdit ? mUserListEntity.data![mWhichPatient].patientId.toString() : ""; var params = { 'patientId': patientId, 'userRelationship': relationship, 'documentType': passportType, 'pastHistory': previous, 'familyHistory': family, 'patientName': nameController.text, 'identificationCard': passportController.text, 'patientPhone': mobileController.text, 'allergyHistory': allergyController.text, 'homeAddress': homeAddressController.text, 'isDefault': isDefault, }; logd(params); Map headers = jsonHeaders(withToken: true); var response = await http.post(Uri.parse('${Global.BaseUrl}user'), body: encodeBody(params), headers: headers); if (isEdit) { response = await http.put(Uri.parse('${Global.BaseUrl}user'), body: encodeBody(params), headers: headers); } logd(response.body.toString()); if (response.statusCode == 200) { final json = decodeBodyToJson(response.bodyBytes); logd("新增/编辑就诊人结果=$json"); NormalResponse3 mNormalResponse = NormalResponse3.fromJson(json); if (mNormalResponse.code == Global.responseSuccessCode) { Component.toast(isEdit ? "保存成功!" : "添加成功!", 2); Navigator.pop(context, "success"); } else { Component.toast(mNormalResponse.msg.toString(), 0); } } else { Component.toast("出错了,请稍后再试!", 0); return; } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(isEdit ? '编辑就诊人' : '添加就诊人', style: const TextStyle( color: Colors.white, )), centerTitle: true, elevation: 0.5, backgroundColor: Global.StatusBarColor, leading: IconButton( tooltip: '返回上一页', icon: const Icon( Icons.arrow_back_ios, color: Colors.white, ), onPressed: () { Navigator.of(context).pop(); //_nextPage(-1); }, ), ), body: SingleChildScrollView( child: Form( key: _formKey, child: Column(children: [ const Padding( padding: EdgeInsets.all(10), child: Text( "请认真填写就诊人信息", style: TextStyle( color: Colors.orange, ), )), if (_errorText.isNotEmpty) Container( color: Colors.red, padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 3), child: Text( _errorText, style: const TextStyle(color: Colors.white), ), ), Padding( padding: const EdgeInsets.all(10), child: GestureDetector( onTap: () { selectItem(selectItem0, 0); }, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( "关系", style: TextStyle( color: Colors.grey, ), ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( relationship, style: const TextStyle( color: Colors.black, ), ), const Icon( Icons.chevron_right, color: Colors.grey, ), ]) ]), ), ), const Divider( color: Colors.grey, thickness: 1, indent: 10, endIndent: 10, ), // new Line(), Padding( padding: const EdgeInsets.fromLTRB(10, 0, 10, 0), child: Container( alignment: Alignment.center, height: 45, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( "姓名 ", style: TextStyle( color: Colors.grey, ), ), Expanded( child: TextFormField( maxLines: 1, onSaved: (value) {}, textAlign: TextAlign.right, controller: nameController, inputFormatters: [ FilteringTextInputFormatter.singleLineFormatter, LengthLimitingTextInputFormatter(4) ], decoration: const InputDecoration( hintText: ('请填写姓名'), hintStyle: TextStyle( color: Color(0xff999999), fontSize: 13, ), alignLabelWithHint: true, border: OutlineInputBorder(borderSide: BorderSide.none), ), ), ), ]), ), ), const Divider( color: Colors.grey, thickness: 1, indent: 10, endIndent: 10, ), Padding( padding: const EdgeInsets.all(10), child: GestureDetector( onTap: () { // selectItem(['身份证', '护照', '港澳通行证'], 1); selectItem(selectItem1, 1); }, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( "证件类型", style: TextStyle( color: Colors.grey, ), ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( passport, style: const TextStyle( color: Colors.black, ), ), const Icon( Icons.chevron_right, color: Colors.grey, ), ]) ]), ), ), const Divider( color: Colors.grey, thickness: 1, indent: 10, endIndent: 10, ), Padding( padding: const EdgeInsets.fromLTRB(10, 0, 10, 0), child: Container( alignment: Alignment.center, height: 50, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( "证件号码", style: TextStyle( color: Colors.grey, ), ), Expanded( child: TextFormField( controller: passportController, maxLines: 1, onSaved: (value) {}, textAlign: TextAlign.right, inputFormatters: [ FilteringTextInputFormatter.singleLineFormatter, LengthLimitingTextInputFormatter(18) ], decoration: const InputDecoration( hintText: ('请输入证件号码'), hintStyle: TextStyle( color: Color(0xff999999), fontSize: 13, ), alignLabelWithHint: true, border: OutlineInputBorder(borderSide: BorderSide.none), ), ), ), ]), ), ), const Divider( color: Colors.grey, thickness: 1, indent: 10, endIndent: 10, ), Padding( padding: const EdgeInsets.all(10), child: GestureDetector( onTap: () { selectItem(selectItem2, 2); }, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( "既往病史", style: TextStyle( color: Colors.grey, ), ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( previous, style: const TextStyle( color: Colors.black, ), ), const Icon( Icons.chevron_right, color: Colors.grey, ), ]) ]), ), ), const Divider( color: Colors.grey, thickness: 1, indent: 10, endIndent: 10, ), Padding( padding: const EdgeInsets.all(10), child: GestureDetector( onTap: () { selectItem(selectItem3, 3); }, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( "家族病史", style: TextStyle( color: Colors.grey, ), ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( family, style: const TextStyle( color: Colors.black, ), ), const Icon( Icons.chevron_right, color: Colors.grey, ), ]) ]), ), ), const Divider( color: Colors.grey, thickness: 1, indent: 10, endIndent: 10, ), Padding( padding: const EdgeInsets.fromLTRB(10, 0, 10, 0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( "过敏史", style: TextStyle( color: Colors.grey, ), ), Row( children: [ const Text("无"), Radio( value: 0, onChanged: (value) { setState(() { allergyController.text = ""; selectedRadioValue = value!; }); }, groupValue: selectedRadioValue, ), const SizedBox( width: 20, ), const Text("有"), Radio( value: 1, onChanged: (value) { setState(() { this.selectedRadioValue = value!; }); }, groupValue: selectedRadioValue, ), ], ), ]), ), Visibility( visible: selectedRadioValue == 1, child: Padding( padding: const EdgeInsets.fromLTRB(10, 0, 10, 0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( "", style: TextStyle( color: Colors.grey, ), ), Expanded( child: TextFormField( controller: allergyController, maxLines: 1, onSaved: (value) {}, textAlign: TextAlign.right, inputFormatters: [ FilteringTextInputFormatter.singleLineFormatter, LengthLimitingTextInputFormatter(18) ], decoration: const InputDecoration( hintText: ('请填写过敏史'), hintStyle: TextStyle( color: Color(0xff999999), fontSize: 13, ), alignLabelWithHint: true, border: OutlineInputBorder(borderSide: BorderSide.none), ), ), ), ]), ), ), const Divider( color: Colors.grey, thickness: 1, indent: 10, endIndent: 10, ), Padding( padding: const EdgeInsets.fromLTRB(10, 0, 10, 0), child: Container( alignment: Alignment.center, height: 50, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( "手机号", style: TextStyle( color: Colors.grey, ), ), Expanded( child: TextFormField( enabled: mobileCanEdit, controller: mobileController, maxLines: 1, onSaved: (value) {}, textAlign: TextAlign.right, inputFormatters: [ FilteringTextInputFormatter.digitsOnly, LengthLimitingTextInputFormatter(11) ], decoration: const InputDecoration( hintText: ('请输入手机号码'), hintStyle: TextStyle( color: Color(0xff999999), fontSize: 13, ), alignLabelWithHint: true, border: OutlineInputBorder(borderSide: BorderSide.none), ), ), ), ]), ), ), const Divider( color: Colors.grey, thickness: 1, indent: 10, endIndent: 10, ), Padding( padding: const EdgeInsets.fromLTRB(10, 0, 10, 0), child: Container( alignment: Alignment.center, height: 50, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( "就诊人地址", style: TextStyle( color: Colors.grey, ), ), Expanded( child: TextFormField( controller: homeAddressController, maxLines: 1, onSaved: (value) {}, textAlign: TextAlign.right, // inputFormatters: [ // FilteringTextInputFormatter.digitsOnly, // LengthLimitingTextInputFormatter(11) // ], decoration: const InputDecoration( hintText: ('选填'), hintStyle: TextStyle( color: Color(0xff999999), fontSize: 13, ), alignLabelWithHint: true, border: OutlineInputBorder(borderSide: BorderSide.none), ), ), ), ]), ), ), const Divider( color: Colors.grey, thickness: 1, indent: 10, endIndent: 10, ), Padding( padding: const EdgeInsets.fromLTRB(10, 0, 10, 0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( "设为默认就诊人", style: TextStyle( color: Colors.grey, ), ), Switch( value: switchSelected, onChanged: (value) { setState(() { switchSelected = value; }); }, ), ]), ), Padding( padding: const EdgeInsets.symmetric(horizontal: 20), child: SizedBox( width: double.infinity, child: ElevatedButton( style: const ButtonStyle( backgroundColor: WidgetStatePropertyAll(Colors.blue), ), onPressed: () { _validateInputs(); }, child: const Text("确定", style: TextStyle(color: Colors.white, fontSize: 15)), ), )), const SizedBox( height: 10, ) ]), ), ), ); } selectItem(List list, int item) { showModalBottomSheet( context: context, builder: (BuildContext context) { return Container( width: double.infinity, height: list.length * 50, child: Column( children: List.generate( list.length, (index) => GestureDetector( onTap: () { setState(() { switch (item) { case 0: relationship = list[index]; if (relationship == "本人") { mobileCanEdit = false; mobileController.text = Global.loginPhoneNo; } else { mobileCanEdit = true; mobileController.text = ""; } break; case 1: passport = list[index]; break; case 2: previous = list[index]; break; case 3: family = list[index]; break; } }); Navigator.pop(context); }, child: Padding( padding: const EdgeInsets.all(10), child: Text( '${list[index]}', style: TextStyle(fontSize: 18), ), ), ), ), ), ); }, ); } }