123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- import 'dart:convert';
- import 'package:fis_common/index.dart';
- import 'package:fis_jsonrpc/rpc.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:get/get.dart';
- import 'package:vitalapp/architecture/storage/text_storage.dart';
- import 'package:vitalapp/architecture/utils/prompt_box.dart';
- import 'package:vitalapp/components/appbar.dart';
- import 'package:vitalapp/global.dart';
- import 'package:vitalapp/managers/interfaces/template.dart';
- import 'package:vitalapp/pages/check/models/form.dart';
- import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_single_line_radio.dart';
- import 'package:vitalapp/store/store.dart';
- class SelfCareAssessmentModule extends StatefulWidget {
- final String cardKey;
- final Future<bool> Function(String, String, dynamic) callBack;
- final String? patientCode;
- final String? examData;
- final bool isEdit;
- const SelfCareAssessmentModule({
- super.key,
- required this.cardKey,
- required this.callBack,
- this.patientCode,
- this.examData,
- this.isEdit = false,
- });
- @override
- State<SelfCareAssessmentModule> createState() => _SelfCareAssessmentModule();
- }
- class _SelfCareAssessmentModule extends State<SelfCareAssessmentModule> {
- ///当前最新的模版键值对
- Map<String, dynamic> templateRelation = {};
- String templateCode = '';
- ///是否首次进入页面
- bool isFirstEnter = true;
- ///当前模版数据
- List<FormObject> currentTemplate = [];
- ///当前title的下标
- int currentTitleIndex = 0;
- ///数据存储
- Map<String, dynamic> formValue = {};
- final _templateManager = Get.find<ITemplateManager>();
- @override
- void initState() {
- super.initState();
- initReadCached();
- WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
- if (mounted) {
- initTemplate();
- }
- });
- }
- Future<void> initTemplate() async {
- Store.app.busy = true;
- await fetchTemplateIndex();
- await fetchTemplate(widget.cardKey);
- await fetchTemplateData();
- Store.app.busy = false;
- setState(() {});
- }
- Future<void> initReadCached() async {
- if (widget.patientCode == null) {
- return;
- }
- TextStorage cachedRecord = TextStorage(
- fileName: widget.cardKey,
- directory: "patient/${widget.patientCode}",
- );
- String? value = await cachedRecord.read();
- if (value == null) {
- formValue = {};
- Store.resident.handleSaveMedicalData(jsonEncode(formValue));
- return;
- }
- Store.resident.handleSaveMedicalData(value);
- formValue = jsonDecode(value);
- }
- Future<bool?> saveCachedRecord() async {
- if (widget.patientCode == null) {
- return false;
- }
- Store.resident.handleSaveMedicalData(jsonEncode(formValue));
- TextStorage cachedRecord = TextStorage(
- fileName: widget.cardKey,
- directory: "patient/${widget.patientCode}",
- );
- return cachedRecord.save(jsonEncode(formValue));
- }
- Future<bool?> deleteDirectory() async {
- TextStorage cachedRecord = TextStorage(
- fileName: widget.cardKey,
- directory: "patient/${widget.patientCode}",
- );
- return cachedRecord.deleteDirectory();
- }
- Future<void> fetchTemplateIndex() async {
- try {
- var templates =
- await _templateManager.readTemplateRelation("templateRelation");
- templateRelation = jsonDecode(templates!);
- } catch (e) {}
- }
- Future<void> fetchTemplate(String key) async {
- try {
- if (templateRelation[key] == null) {
- currentTemplate = [];
- if (!FPlatform.isWindows) {
- return;
- }
- }
- String? template;
- if (templateRelation.containsKey(key)) {
- templateCode = templateRelation[key]!;
- template = await _templateManager.readTemplate(templateCode);
- }
- List<Map<String, dynamic>> list = [];
- if (template == null) {
- var json = await loadJsonFromAssets('assets/templates/${key}.json');
- list = jsonDecode(json)["Content"].cast<Map<String, dynamic>>();
- } else {
- var templateContent =
- TemplateDTO.fromJson(jsonDecode(template)).templateContent!;
- list = jsonDecode(templateContent).cast<Map<String, dynamic>>();
- }
- for (var i in list) {
- if (i["children"] != null) {
- List<FormObject> currentChildren = [];
- for (var j in i['children']) {
- currentChildren.add(FormObject.fromJson(j));
- }
- i['children'] = currentChildren;
- }
- var item = FormObject.fromJson(i);
- currentTemplate.add(item);
- }
- } catch (e) {}
- }
- Future<void> fetchTemplateData() async {
- if (widget.examData?.isNotEmpty ?? false) {
- formValue = jsonDecode(widget.examData!);
- return;
- }
- }
- Future<String> loadJsonFromAssets(String filePath) async {
- String jsonString = await rootBundle.loadString(filePath);
- return jsonString;
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: VAppBar(
- titleText: "自理能力评估",
- actions: [
- TextButton(
- child: Padding(
- padding: const EdgeInsets.only(right: 16.0),
- child: Text(
- "提交",
- style: const TextStyle(
- fontSize: 28,
- color: Colors.white,
- ),
- ),
- ),
- onPressed: () async {
- if (!kIsOnline) {
- PromptBox.toast('请检查网络连接');
- return;
- }
- if (!_validateFormValue(formValue)) {
- PromptBox.toast('未填写任何评估内容');
- return;
- }
- final result = await widget.callBack(
- widget.cardKey,
- templateCode,
- jsonEncode(formValue),
- );
- Get.back(result: formValue["SelfCareScore"]);
- if (result) {
- if (!widget.isEdit) formValue.clear();
- if (!FPlatform.isWindows) {
- deleteDirectory();
- }
- PromptBox.toast('提交成功');
- setState(() {});
- }
- },
- )
- ],
- ),
- body: Column(
- mainAxisAlignment: MainAxisAlignment.start,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- const SizedBox(
- height: 20,
- ),
- Expanded(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.start,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- SizedBox(
- width: 50,
- ),
- //内容展示
- _buildContent(),
- SizedBox(
- width: 50,
- ),
- ],
- ),
- ),
- Row(
- children: [
- SizedBox(
- width: 30,
- ),
- Text(
- '评估得分:${formValue["SelfCareScore"] ?? 0} 评估结论:${formValue["SelfCareConclusion"] ?? "可自理"}',
- style: TextStyle(fontSize: 32),
- ),
- ],
- ),
- ],
- ),
- );
- }
- ///加载主界面
- Widget _buildContent() {
- int itemCount = 0; //选项数量
- if (currentTemplate.isNotEmpty) {
- itemCount = currentTemplate[currentTitleIndex].children?.length ?? 0;
- }
- List<Widget> items = List.generate(itemCount, (index) {
- FormObject? currentFormObject =
- currentTemplate[currentTitleIndex].children?[index];
- int span = currentFormObject?.span ?? 12;
- currentFormObject!.options = currentFormObject.options!
- .where((element) => element.label != "-")
- .toList();
- return buildSingleItem(buildWidget(currentFormObject), span);
- });
- return Expanded(
- child: Scrollbar(
- child: SingleChildScrollView(
- child: Column(
- children: [
- Container(
- alignment: Alignment.topCenter,
- padding: const EdgeInsets.all(15),
- child: Column(
- children: [
- Wrap(
- runSpacing: 10,
- alignment: WrapAlignment.start,
- children: items,
- ),
- ],
- ),
- ),
- ],
- ),
- )));
- }
- Widget buildSingleItem(Widget item, int span) {
- return Column(
- children: [
- FractionallySizedBox(
- widthFactor: span == 24 ? 1 : 0.5,
- child: item,
- ),
- ],
- );
- }
- Widget buildWidget(FormObject? currentFormObject) {
- Map<String, Widget Function(FormObject)> widgetMap = {
- 'radio': _buildRadio,
- };
- Widget Function(FormObject) builder = widgetMap[currentFormObject?.type]!;
- return builder(currentFormObject!);
- }
- /// 单选框组件
- Widget _buildRadio(FormObject currentFormObject) {
- List<Option> options = currentFormObject.options ?? [];
- String currentSelected = formValue[currentFormObject.key!] ?? "";
- void selectRaidoChange(Option e) {
- currentSelected = e.value ?? '';
- setState(() {
- optionUpdate(currentFormObject, currentSelected);
- });
- }
- return Container(
- child: ExamSingleLineRadio(
- options: options,
- currentFormObject: currentFormObject,
- selectRaidoChange: selectRaidoChange,
- currentSelected: currentSelected,
- ),
- );
- }
- Future<void> optionUpdate(
- FormObject formObject, String currentSelected) async {
- formValue[formObject.key!] = currentSelected;
- caculationConclusion();
- // saveCachedRecord();
- }
- Future<void> caculationConclusion() async {
- int score = 0;
- var currentTemplateStoreList = currentTemplate[0].children!;
- for (var element in currentTemplateStoreList) {
- var elementStore = formValue[element.key!];
- if (elementStore != null) {
- score += int.parse(elementStore);
- }
- }
- formValue["SelfCareScore"] = score.toString();
- var result = "可自理";
- if (score <= 3) {
- result = "可自理";
- } else if (score <= 8) {
- result = "轻度依赖";
- } else if (score <= 18) {
- result = "中度依赖";
- } else {
- result = "不能自理";
- }
- formValue["SelfCareConclusion"] = result;
- saveCachedRecord();
- }
- bool _validateFormValue(Map<String, dynamic> formValue) {
- if (formValue.isEmpty) {
- return false;
- } else {
- return true;
- }
- }
- }
|