|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/widgets.dart'; |
| 3 | +import 'package:flutter_form_builder/flutter_form_builder.dart'; |
| 4 | +import 'package:flutter_form_builder/src/widgets/grouped_checkbox.dart'; |
| 5 | + |
| 6 | +class FormBuilderCheckboxGroup<T> extends StatefulWidget { |
| 7 | + final String attribute; |
| 8 | + final List<FormFieldValidator> validators; |
| 9 | + final List<T> initialValue; |
| 10 | + final bool readOnly; |
| 11 | + final InputDecoration decoration; |
| 12 | + final ValueChanged onChanged; |
| 13 | + final ValueTransformer valueTransformer; |
| 14 | + final bool enabled; |
| 15 | + final FormFieldSetter onSaved; |
| 16 | + final bool autovalidate; |
| 17 | + final List<FormBuilderFieldOption> options; |
| 18 | + final Color activeColor; |
| 19 | + final Color checkColor; |
| 20 | + final Color focusColor; |
| 21 | + final Color hoverColor; |
| 22 | + final List<T> disabled; |
| 23 | + final MaterialTapTargetSize materialTapTargetSize; |
| 24 | + final bool tristate; |
| 25 | + final Axis wrapDirection; |
| 26 | + |
| 27 | + final WrapAlignment wrapAlignment; |
| 28 | + |
| 29 | + final double wrapSpacing; |
| 30 | + |
| 31 | + final WrapAlignment wrapRunAlignment; |
| 32 | + |
| 33 | + final double wrapRunSpacing; |
| 34 | + |
| 35 | + final WrapCrossAlignment wrapCrossAxisAlignment; |
| 36 | + |
| 37 | + final TextDirection wrapTextDirection; |
| 38 | + final VerticalDirection wrapVerticalDirection; |
| 39 | + final Widget separator; |
| 40 | + final ControlAffinity controlAffinity; |
| 41 | + |
| 42 | + final GroupedCheckboxOrientation orientation; |
| 43 | + |
| 44 | + FormBuilderCheckboxGroup({ |
| 45 | + Key key, |
| 46 | + @required this.attribute, |
| 47 | + this.initialValue, |
| 48 | + this.readOnly = false, |
| 49 | + this.decoration = const InputDecoration(), |
| 50 | + this.onChanged, |
| 51 | + this.valueTransformer, |
| 52 | + this.enabled = true, |
| 53 | + this.onSaved, |
| 54 | + this.autovalidate = false, |
| 55 | + @required this.options, |
| 56 | + this.activeColor, |
| 57 | + this.checkColor, |
| 58 | + this.focusColor, |
| 59 | + this.hoverColor, |
| 60 | + this.disabled, |
| 61 | + this.materialTapTargetSize, |
| 62 | + this.tristate = false, |
| 63 | + this.wrapDirection = Axis.horizontal, |
| 64 | + this.wrapAlignment = WrapAlignment.start, |
| 65 | + this.wrapSpacing = 0.0, |
| 66 | + this.wrapRunAlignment = WrapAlignment.start, |
| 67 | + this.wrapRunSpacing = 0.0, |
| 68 | + this.wrapCrossAxisAlignment = WrapCrossAlignment.start, |
| 69 | + this.wrapTextDirection, |
| 70 | + this.wrapVerticalDirection = VerticalDirection.down, |
| 71 | + this.separator, |
| 72 | + this.controlAffinity = ControlAffinity.leading, |
| 73 | + this.orientation = GroupedCheckboxOrientation.wrap, |
| 74 | + this.validators = const [], |
| 75 | + }) : super( |
| 76 | + key: key, |
| 77 | + ); |
| 78 | + |
| 79 | + @override |
| 80 | + _FormBuilderCheckboxGroupState<T> createState() => |
| 81 | + _FormBuilderCheckboxGroupState(); |
| 82 | +} |
| 83 | + |
| 84 | +class _FormBuilderCheckboxGroupState<T> |
| 85 | + extends State<FormBuilderCheckboxGroup> { |
| 86 | + bool _readOnly = false; |
| 87 | + final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>(); |
| 88 | + FormBuilderState _formState; |
| 89 | + dynamic _initialValue; |
| 90 | + |
| 91 | + @override |
| 92 | + void initState() { |
| 93 | + _formState = FormBuilder.of(context); |
| 94 | + _formState?.registerFieldKey(widget.attribute, _fieldKey); |
| 95 | + _initialValue = widget.initialValue ?? |
| 96 | + (_formState.initialValue.containsKey(widget.attribute) |
| 97 | + ? _formState.initialValue[widget.attribute] |
| 98 | + : null); |
| 99 | + super.initState(); |
| 100 | + } |
| 101 | + |
| 102 | + @override |
| 103 | + void dispose() { |
| 104 | + _formState?.unregisterFieldKey(widget.attribute); |
| 105 | + super.dispose(); |
| 106 | + } |
| 107 | + |
| 108 | + @override |
| 109 | + Widget build(BuildContext context) { |
| 110 | + _readOnly = _formState?.readOnly == true || widget.readOnly; |
| 111 | + |
| 112 | + return FormField( |
| 113 | + key: _fieldKey, |
| 114 | + enabled: !_readOnly, |
| 115 | + initialValue: _initialValue, |
| 116 | + validator: (val) => |
| 117 | + FormBuilderValidators.validateValidators(val, widget.validators), |
| 118 | + onSaved: (val) { |
| 119 | + var transformed; |
| 120 | + if (widget.valueTransformer != null) { |
| 121 | + transformed = widget.valueTransformer(val); |
| 122 | + _formState?.setAttributeValue(widget.attribute, transformed); |
| 123 | + } else { |
| 124 | + _formState?.setAttributeValue(widget.attribute, val); |
| 125 | + } |
| 126 | + widget.onSaved?.call(transformed ?? val); |
| 127 | + }, |
| 128 | + builder: (FormFieldState field) { |
| 129 | + return InputDecorator( |
| 130 | + decoration: widget.decoration.copyWith( |
| 131 | + enabled: _readOnly, |
| 132 | + errorText: field.errorText, |
| 133 | + ), |
| 134 | + child: GroupedCheckbox( |
| 135 | + orientation: widget.orientation, |
| 136 | + value: widget.initialValue, |
| 137 | + options: widget.options, |
| 138 | + onChanged: (val) { |
| 139 | + field.didChange(val); |
| 140 | + widget.onChanged?.call(val); |
| 141 | + }, |
| 142 | + activeColor: widget.activeColor, |
| 143 | + focusColor: widget.focusColor, |
| 144 | + checkColor: widget.checkColor, |
| 145 | + materialTapTargetSize: widget.materialTapTargetSize, |
| 146 | + disabled: widget.disabled, |
| 147 | + hoverColor: widget.hoverColor, |
| 148 | + tristate: widget.tristate, |
| 149 | + wrapAlignment: widget.wrapAlignment, |
| 150 | + wrapCrossAxisAlignment: widget.wrapCrossAxisAlignment, |
| 151 | + wrapDirection: widget.wrapDirection, |
| 152 | + wrapRunAlignment: widget.wrapRunAlignment, |
| 153 | + wrapRunSpacing: widget.wrapRunSpacing, |
| 154 | + wrapSpacing: widget.wrapSpacing, |
| 155 | + wrapTextDirection: widget.wrapTextDirection, |
| 156 | + wrapVerticalDirection: widget.wrapVerticalDirection, |
| 157 | + separator: widget.separator, |
| 158 | + controlAffinity: widget.controlAffinity, |
| 159 | + ), |
| 160 | + ); |
| 161 | + }, |
| 162 | + ); |
| 163 | + } |
| 164 | +} |
0 commit comments