123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- import 'package:fis_common/index.dart';
- import 'package:flutter/material.dart';
- import 'package:vitalapp/consts/styles.dart';
- import 'package:vitalapp/pages/check/models/form.dart';
- import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_radio_and_select.dart';
- import 'package:vitalapp/pages/check/widgets/exam_title.dart';
- import 'package:vitalapp/pages/form/form_info.dart';
- class ExamSingleOption extends StatefulWidget {
- const ExamSingleOption({
- super.key,
- required this.currentFormObject,
- required this.option,
- required this.isSelected,
- });
- final FormObject currentFormObject;
- final Option option;
- final bool isSelected;
- @override
- State<StatefulWidget> createState() {
- return ExamSingleOptionState();
- }
- }
- class ExamSingleOptionState extends State<ExamSingleOption> {
- FormObject get currentFormObject => widget.currentFormObject;
- bool _isSelected = false;
- bool _isDisabledValue = false;
- @override
- void initState() {
- _isSelected = widget.isSelected;
- if (FormInfo.instance.formValue.containsKey(currentFormObject.parentKey!)) {
- var formValue = FormInfo.instance.formValue[currentFormObject.parentKey!];
- if (formValue is List) {
- _isSelected = formValue.contains(currentFormObject.key);
- if (_isSelected)
- Future.delayed(Duration(milliseconds: 200), () {
- FormInfo.instance.onValueChange.emit(
- this,
- UpdateFormArgs(
- sourceKey: widget.currentFormObject.parentKey ?? '',
- sourceValue: currentFormObject.key,
- type: UpdateFormType.Add,
- ),
- );
- });
- }
- }
- super.initState();
- FormInfo.instance.onChangeTargetValue.addListener(_onChangeTargetValue);
- }
- @override
- void dispose() {
- FormInfo.instance.onChangeTargetValue.removeListener(_onChangeTargetValue);
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return Container(
- padding: const EdgeInsets.all(7),
- child: InkWell(
- onTap: () => selectRaidoChange(),
- borderRadius: BorderRadius.circular(8),
- child: Card(
- elevation: 6,
- shape: RoundedRectangleBorder(
- borderRadius: GlobalStyles.borderRadius,
- ),
- child: Material(
- color: _isDisabledValue ? Colors.grey[300] : Colors.white,
- borderRadius: GlobalStyles.borderRadius,
- child: Ink(
- decoration: BoxDecoration(
- border: Border.all(
- color: _isSelected ? Colors.blue : Colors.black26,
- ),
- borderRadius: const BorderRadius.all(
- Radius.circular(8),
- ),
- color: _isSelected ? Colors.blue : Colors.transparent,
- ),
- child: Container(
- padding: const EdgeInsets.all(15),
- alignment: Alignment.center,
- width: 250,
- child: FittedBox(
- child: Text(
- widget.option.label ?? '',
- style: TextStyle(
- fontSize: 20,
- color: _isSelected ? Colors.white : Colors.black54,
- ),
- ),
- ),
- ),
- ),
- ),
- ),
- ),
- );
- }
- void selectRaidoChange() {
- if (_isDisabledValue) {
- return;
- }
- setState(() {
- _isSelected = !_isSelected;
- });
- onValueChange();
- }
- ///当前选中的值改变
- void onValueChange() {
- UpdateFormType type = UpdateFormType.Add;
- if (_isSelected) {
- if (FormInfo.instance.formValue
- .containsKey(currentFormObject.parentKey!)) {
- var sourceValue =
- FormInfo.instance.formValue[currentFormObject.parentKey!] as List;
- sourceValue.add(currentFormObject.key!);
- } else {
- FormInfo.instance.formValue[currentFormObject.parentKey!] = [
- currentFormObject.key
- ];
- }
- type = UpdateFormType.Add;
- } else if (FormInfo.instance.formValue
- .containsKey(currentFormObject.parentKey!)) {
- FormInfo.instance.formValue.remove(currentFormObject.parentKey!);
- type = UpdateFormType.Remove;
- }
- FormInfo.instance.onValueChange.emit(
- this,
- UpdateFormArgs(
- sourceKey: widget.currentFormObject.parentKey ?? '',
- sourceValue: currentFormObject.key,
- type: type,
- ),
- );
- }
- ///其他控件的改变、通知到本控件
- void _onChangeTargetValue(Object sender, TargetFormArgs e) {
- if (e.targetKey != widget.currentFormObject.key) {
- return;
- }
- if (e.isDisabledValue) {
- setState(() {
- _isSelected = false;
- _isDisabledValue = true;
- });
- } else if (!e.isDisabledValue) {
- setState(() {
- _isDisabledValue = false;
- });
- }
- if (e.targetValue == "cancelSelection" && _isSelected) {
- setState(() {
- _isSelected = false;
- });
- }
- }
- }
|