|
| 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:group_radio_button/group_radio_button.dart'; |
| 5 | + |
| 6 | +class FormBuilderRadioGroup extends StatefulWidget { |
| 7 | + final String attribute; |
| 8 | + final List<FormFieldValidator> validators; |
| 9 | + final dynamic initialValue; |
| 10 | + final bool readOnly; |
| 11 | + final InputDecoration decoration; |
| 12 | + final ValueChanged onChanged; |
| 13 | + final ValueTransformer valueTransformer; |
| 14 | + |
| 15 | + final bool leadingInput; |
| 16 | + final List<FormBuilderFieldOption> options; |
| 17 | + final MaterialTapTargetSize materialTapTargetSize; |
| 18 | + final Color activeColor; |
| 19 | + final FormFieldSetter onSaved; |
| 20 | + final EdgeInsets contentPadding; |
| 21 | + |
| 22 | + FormBuilderRadioGroup({ |
| 23 | + Key key, |
| 24 | + @required this.attribute, |
| 25 | + @required this.options, |
| 26 | + this.initialValue, |
| 27 | + this.validators = const [], |
| 28 | + this.readOnly = false, |
| 29 | + this.decoration = const InputDecoration(), |
| 30 | + this.onChanged, |
| 31 | + this.valueTransformer, |
| 32 | + this.leadingInput = false, |
| 33 | + this.materialTapTargetSize, |
| 34 | + this.activeColor, |
| 35 | + this.onSaved, |
| 36 | + this.contentPadding = const EdgeInsets.all(0.0), |
| 37 | + }) : super(key: key); |
| 38 | + |
| 39 | + @override |
| 40 | + _FormBuilderRadioGroupState createState() => _FormBuilderRadioGroupState(); |
| 41 | +} |
| 42 | + |
| 43 | +class _FormBuilderRadioGroupState extends State<FormBuilderRadioGroup> { |
| 44 | + bool _readOnly = false; |
| 45 | + final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>(); |
| 46 | + FormBuilderState _formState; |
| 47 | + dynamic _initialValue; |
| 48 | + |
| 49 | + @override |
| 50 | + void initState() { |
| 51 | + _formState = FormBuilder.of(context); |
| 52 | + _formState?.registerFieldKey(widget.attribute, _fieldKey); |
| 53 | + _initialValue = widget.initialValue ?? |
| 54 | + (_formState.initialValue.containsKey(widget.attribute) |
| 55 | + ? _formState.initialValue[widget.attribute] |
| 56 | + : null); |
| 57 | + super.initState(); |
| 58 | + } |
| 59 | + |
| 60 | + @override |
| 61 | + void dispose() { |
| 62 | + _formState?.unregisterFieldKey(widget.attribute); |
| 63 | + super.dispose(); |
| 64 | + } |
| 65 | + |
| 66 | + @override |
| 67 | + Widget build(BuildContext context) { |
| 68 | + _readOnly = (_formState?.readOnly == true) ? true : widget.readOnly; |
| 69 | + |
| 70 | + return FormField( |
| 71 | + key: _fieldKey, |
| 72 | + enabled: !_readOnly, |
| 73 | + initialValue: _initialValue, |
| 74 | + validator: (val) { |
| 75 | + for (int i = 0; i < widget.validators.length; i++) { |
| 76 | + if (widget.validators[i](val) != null) { |
| 77 | + return widget.validators[i](val); |
| 78 | + } |
| 79 | + } |
| 80 | + return null; |
| 81 | + }, |
| 82 | + onSaved: (val) { |
| 83 | + var transformed; |
| 84 | + if (widget.valueTransformer != null) { |
| 85 | + transformed = widget.valueTransformer(val); |
| 86 | + _formState?.setAttributeValue(widget.attribute, transformed); |
| 87 | + } else { |
| 88 | + _formState?.setAttributeValue(widget.attribute, val); |
| 89 | + } |
| 90 | + if (widget.onSaved != null) { |
| 91 | + widget.onSaved(transformed ?? val); |
| 92 | + } |
| 93 | + }, |
| 94 | + builder: (FormFieldState<dynamic> field) { |
| 95 | + return InputDecorator( |
| 96 | + decoration: widget.decoration.copyWith( |
| 97 | + enabled: !_readOnly, |
| 98 | + errorText: field.errorText, |
| 99 | + ), |
| 100 | + child: RadioGroup.builder( |
| 101 | + groupValue: field.value, |
| 102 | + onChanged: _readOnly ? null : (value) { |
| 103 | + FocusScope.of(context).requestFocus(FocusNode()); |
| 104 | + field.didChange(value); |
| 105 | + if (widget.onChanged != null) widget.onChanged(value); |
| 106 | + }, |
| 107 | + items: widget.options.map((option) => option.value).toList(growable: false), |
| 108 | + itemBuilder: (item) { |
| 109 | + return RadioButtonBuilder( |
| 110 | + item.toString(), |
| 111 | + textPosition: RadioButtonTextPosition.right, |
| 112 | + ); |
| 113 | + }, |
| 114 | + direction: Axis.horizontal, |
| 115 | + horizontalAlignment: MainAxisAlignment.start, |
| 116 | + spacebetween: 40, |
| 117 | + ), |
| 118 | + ); |
| 119 | + }, |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
0 commit comments