|
1 |
| -// import 'package:flutter/cupertino.dart'; |
2 |
| -// import 'package:flutter/material.dart'; |
3 |
| -// import 'package:flutter/widgets.dart'; |
4 |
| -// import 'package:flutter_form_builder/flutter_form_builder.dart'; |
5 |
| -// |
6 |
| -// /// Field for selection of a value from the `CupertinoSegmentedControl` |
7 |
| -// class FormBuilderSegmentedControl<T> extends FormBuilderField<T> { |
8 |
| -// /// The color used to fill the backgrounds of unselected widgets and as the |
9 |
| -// /// text color of the selected widget. |
10 |
| -// /// |
11 |
| -// /// Defaults to [CupertinoTheme]'s `primaryContrastingColor` if null. |
12 |
| -// final Color? unselectedColor; |
13 |
| -// |
14 |
| -// /// The color used to fill the background of the selected widget and as the text |
15 |
| -// /// color of unselected widgets. |
16 |
| -// /// |
17 |
| -// /// Defaults to [CupertinoTheme]'s `primaryColor` if null. |
18 |
| -// final Color? selectedColor; |
19 |
| -// |
20 |
| -// /// The color used as the border around each widget. |
21 |
| -// /// |
22 |
| -// /// Defaults to [CupertinoTheme]'s `primaryColor` if null. |
23 |
| -// final Color? borderColor; |
24 |
| -// |
25 |
| -// /// The color used to fill the background of the widget the user is |
26 |
| -// /// temporarily interacting with through a long press or drag. |
27 |
| -// /// |
28 |
| -// /// Defaults to the selectedColor at 20% opacity if null. |
29 |
| -// final Color? pressedColor; |
30 |
| -// |
31 |
| -// /// The CupertinoSegmentedControl will be placed inside this padding |
32 |
| -// /// |
33 |
| -// /// Defaults to EdgeInsets.symmetric(horizontal: 16.0) |
34 |
| -// final EdgeInsetsGeometry? padding; |
35 |
| -// |
36 |
| -// /// The list of options the user can select. |
37 |
| -// final List<FormBuilderFieldOption<T>> options; |
38 |
| -// |
39 |
| -// /// Creates field for selection of a value from the `CupertinoSegmentedControl` |
40 |
| -// FormBuilderSegmentedControl({ |
41 |
| -// Key? key, |
42 |
| -// //From Super |
43 |
| -// required String name, |
44 |
| -// FormFieldValidator<T>? validator, |
45 |
| -// T? initialValue, |
46 |
| -// InputDecoration decoration = const InputDecoration(), |
47 |
| -// ValueChanged<T>? onChanged, |
48 |
| -// ValueTransformer<T>? valueTransformer, |
49 |
| -// bool enabled = true, |
50 |
| -// FormFieldSetter<T>? onSaved, |
51 |
| -// AutovalidateMode autovalidateMode = AutovalidateMode.disabled, |
52 |
| -// VoidCallback? onReset, |
53 |
| -// FocusNode? focusNode, |
54 |
| -// required this.options, |
55 |
| -// this.borderColor, |
56 |
| -// this.selectedColor, |
57 |
| -// this.pressedColor, |
58 |
| -// this.padding, |
59 |
| -// this.unselectedColor, |
60 |
| -// }) : super( |
61 |
| -// key: key, |
62 |
| -// initialValue: initialValue, |
63 |
| -// name: name, |
64 |
| -// validator: validator, |
65 |
| -// valueTransformer: valueTransformer, |
66 |
| -// onChanged: onChanged, |
67 |
| -// autovalidateMode: autovalidateMode, |
68 |
| -// onSaved: onSaved, |
69 |
| -// enabled: enabled, |
70 |
| -// onReset: onReset, |
71 |
| -// decoration: decoration, |
72 |
| -// focusNode: focusNode, |
73 |
| -// builder: (FormFieldState<T?> field) { |
74 |
| -// final state = field as _FormBuilderSegmentedControlState<T>; |
75 |
| -// final theme = Theme.of(state.context); |
76 |
| -// |
77 |
| -// return InputDecorator( |
78 |
| -// decoration: state.decoration(), |
79 |
| -// child: Padding( |
80 |
| -// padding: const EdgeInsets.only(top: 10.0), |
81 |
| -// child: CupertinoSegmentedControl<T>( |
82 |
| -// borderColor: state.enabled |
83 |
| -// ? borderColor ?? theme.primaryColor |
84 |
| -// : theme.disabledColor, |
85 |
| -// selectedColor: state.enabled |
86 |
| -// ? selectedColor ?? theme.primaryColor |
87 |
| -// : theme.disabledColor, |
88 |
| -// pressedColor: state.enabled |
89 |
| -// ? pressedColor ?? theme.primaryColor |
90 |
| -// : theme.disabledColor, |
91 |
| -// groupValue: state.value, |
92 |
| -// children: <T, Widget>{ |
93 |
| -// for (final option in options) |
94 |
| -// option.value: Padding( |
95 |
| -// padding: const EdgeInsets.symmetric(vertical: 10.0), |
96 |
| -// child: option, |
97 |
| -// ), |
98 |
| -// }, |
99 |
| -// padding: padding, |
100 |
| -// unselectedColor: unselectedColor, |
101 |
| -// onValueChanged: (value) { |
102 |
| -// state.requestFocus(); |
103 |
| -// if (state.enabled) { |
104 |
| -// field.didChange(value); |
105 |
| -// } else { |
106 |
| -// field.reset(); |
107 |
| -// } |
108 |
| -// }, |
109 |
| -// ), |
110 |
| -// ), |
111 |
| -// ); |
112 |
| -// }, |
113 |
| -// ); |
114 |
| -// |
115 |
| -// @override |
116 |
| -// _FormBuilderSegmentedControlState<T> createState() => |
117 |
| -// _FormBuilderSegmentedControlState(); |
118 |
| -// } |
119 |
| -// |
120 |
| -// class _FormBuilderSegmentedControlState<T> |
121 |
| -// extends FormBuilderFieldState<FormBuilderSegmentedControl<T>, T> {} |
| 1 | +import 'package:flutter/cupertino.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter/widgets.dart'; |
| 4 | +import 'package:flutter_form_builder/flutter_form_builder.dart'; |
| 5 | + |
| 6 | +/// Field for selection of a value from the `CupertinoSegmentedControl` |
| 7 | +class FormBuilderSegmentedControl<T extends Object> extends FormBuilderField<T> { |
| 8 | + /// The color used to fill the backgrounds of unselected widgets and as the |
| 9 | + /// text color of the selected widget. |
| 10 | + /// |
| 11 | + /// Defaults to [CupertinoTheme]'s `primaryContrastingColor` if null. |
| 12 | + final Color? unselectedColor; |
| 13 | + |
| 14 | + /// The color used to fill the background of the selected widget and as the text |
| 15 | + /// color of unselected widgets. |
| 16 | + /// |
| 17 | + /// Defaults to [CupertinoTheme]'s `primaryColor` if null. |
| 18 | + final Color? selectedColor; |
| 19 | + |
| 20 | + /// The color used as the border around each widget. |
| 21 | + /// |
| 22 | + /// Defaults to [CupertinoTheme]'s `primaryColor` if null. |
| 23 | + final Color? borderColor; |
| 24 | + |
| 25 | + /// The color used to fill the background of the widget the user is |
| 26 | + /// temporarily interacting with through a long press or drag. |
| 27 | + /// |
| 28 | + /// Defaults to the selectedColor at 20% opacity if null. |
| 29 | + final Color? pressedColor; |
| 30 | + |
| 31 | + /// The CupertinoSegmentedControl will be placed inside this padding |
| 32 | + /// |
| 33 | + /// Defaults to EdgeInsets.symmetric(horizontal: 16.0) |
| 34 | + final EdgeInsetsGeometry? padding; |
| 35 | + |
| 36 | + /// The list of options the user can select. |
| 37 | + final List<FormBuilderFieldOption<T>> options; |
| 38 | + |
| 39 | + /// Creates field for selection of a value from the `CupertinoSegmentedControl` |
| 40 | + FormBuilderSegmentedControl({ |
| 41 | + Key? key, |
| 42 | + //From Super |
| 43 | + required String name, |
| 44 | + FormFieldValidator<T>? validator, |
| 45 | + T? initialValue, |
| 46 | + InputDecoration decoration = const InputDecoration(), |
| 47 | + ValueChanged<T>? onChanged, |
| 48 | + ValueTransformer<T>? valueTransformer, |
| 49 | + bool enabled = true, |
| 50 | + FormFieldSetter<T>? onSaved, |
| 51 | + AutovalidateMode autovalidateMode = AutovalidateMode.disabled, |
| 52 | + VoidCallback? onReset, |
| 53 | + FocusNode? focusNode, |
| 54 | + required this.options, |
| 55 | + this.borderColor, |
| 56 | + this.selectedColor, |
| 57 | + this.pressedColor, |
| 58 | + this.padding, |
| 59 | + this.unselectedColor, |
| 60 | + }) : super( |
| 61 | + key: key, |
| 62 | + initialValue: initialValue, |
| 63 | + name: name, |
| 64 | + validator: validator, |
| 65 | + valueTransformer: valueTransformer, |
| 66 | + onChanged: onChanged, |
| 67 | + autovalidateMode: autovalidateMode, |
| 68 | + onSaved: onSaved, |
| 69 | + enabled: enabled, |
| 70 | + onReset: onReset, |
| 71 | + decoration: decoration, |
| 72 | + focusNode: focusNode, |
| 73 | + builder: (FormFieldState<T?> field) { |
| 74 | + final state = field as _FormBuilderSegmentedControlState<T>; |
| 75 | + final theme = Theme.of(state.context); |
| 76 | + |
| 77 | + return InputDecorator( |
| 78 | + decoration: state.decoration(), |
| 79 | + child: Padding( |
| 80 | + padding: const EdgeInsets.only(top: 10.0), |
| 81 | + child: CupertinoSegmentedControl<T>( |
| 82 | + borderColor: state.enabled |
| 83 | + ? borderColor ?? theme.primaryColor |
| 84 | + : theme.disabledColor, |
| 85 | + selectedColor: state.enabled |
| 86 | + ? selectedColor ?? theme.primaryColor |
| 87 | + : theme.disabledColor, |
| 88 | + pressedColor: state.enabled |
| 89 | + ? pressedColor ?? theme.primaryColor |
| 90 | + : theme.disabledColor, |
| 91 | + groupValue: state.value, |
| 92 | + children: <T, Widget>{ |
| 93 | + for (final option in options) |
| 94 | + option.value: Padding( |
| 95 | + padding: const EdgeInsets.symmetric(vertical: 10.0), |
| 96 | + child: option, |
| 97 | + ), |
| 98 | + }, |
| 99 | + padding: padding, |
| 100 | + unselectedColor: unselectedColor, |
| 101 | + onValueChanged: (value) { |
| 102 | + state.requestFocus(); |
| 103 | + if (state.enabled) { |
| 104 | + field.didChange(value); |
| 105 | + } else { |
| 106 | + field.reset(); |
| 107 | + } |
| 108 | + }, |
| 109 | + ), |
| 110 | + ), |
| 111 | + ); |
| 112 | + }, |
| 113 | + ); |
| 114 | + |
| 115 | + @override |
| 116 | + _FormBuilderSegmentedControlState<T> createState() => |
| 117 | + _FormBuilderSegmentedControlState(); |
| 118 | +} |
| 119 | + |
| 120 | +class _FormBuilderSegmentedControlState<T extends Object> |
| 121 | + extends FormBuilderFieldState<FormBuilderSegmentedControl<T>, T> {} |
0 commit comments