Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 3a014f6

Browse files
author
Yuncong Zhang
committed
Fix form.
1 parent f5407ef commit 3a014f6

File tree

3 files changed

+43
-35
lines changed

3 files changed

+43
-35
lines changed

Runtime/material/dropdown.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -709,22 +709,22 @@ public override Widget build(BuildContext context) {
709709
}
710710
}
711711

712-
public class DropdownButtonFormField<T> : FormField where T : class {
712+
public class DropdownButtonFormField<T> : FormField<T> where T : class {
713713
public DropdownButtonFormField(
714714
Key key = null,
715715
T value = null,
716716
List<DropdownMenuItem<T>> items = null,
717717
ValueChanged<T> onChanged = null,
718718
InputDecoration decoration = null,
719-
FormFieldSetter onSaved = null,
720-
FormFieldValidator validator = null,
719+
FormFieldSetter<T> onSaved = null,
720+
FormFieldValidator<T> validator = null,
721721
Widget hint = null
722722
) : base(
723723
key: key,
724724
onSaved: onSaved,
725725
initialValue: value,
726726
validator: validator,
727-
builder: (FormFieldState field) => {
727+
builder: (FormFieldState<T> field) => {
728728
InputDecoration effectiveDecoration = (decoration ?? new InputDecoration())
729729
.applyDefaults(Theme.of(field.context).inputDecorationTheme);
730730
return new InputDecorator(
@@ -752,15 +752,15 @@ public override State createState() {
752752
}
753753
}
754754

755-
class _DropdownButtonFormFieldState<T> : FormFieldState where T : class {
755+
class _DropdownButtonFormFieldState<T> : FormFieldState<T> where T : class {
756756
public new DropdownButtonFormField<T> widget {
757757
get { return base.widget as DropdownButtonFormField<T>; }
758758
}
759759

760-
public override void didChange(object value) {
760+
public override void didChange(T value) {
761761
base.didChange(value);
762762
if (this.widget.onChanged != null) {
763-
this.widget.onChanged((T) value);
763+
this.widget.onChanged(value);
764764
}
765765
}
766766
}

Runtime/material/text_form_field.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using TextStyle = Unity.UIWidgets.painting.TextStyle;
99

1010
namespace UIWidgets.Runtime.material {
11-
public class TextFormField : FormField {
11+
public class TextFormField : FormField<string> {
1212
public TextFormField(
1313
Key key = null,
1414
TextEditingController controller = null,
@@ -30,8 +30,8 @@ public TextFormField(
3030
int? maxLength = null,
3131
VoidCallback onEditingComplete = null,
3232
ValueChanged<string> onFieldSubmitted = null,
33-
FormFieldSetter onSaved = null,
34-
FormFieldValidator validator = null,
33+
FormFieldSetter<string> onSaved = null,
34+
FormFieldValidator<string> validator = null,
3535
List<TextInputFormatter> inputFormatters = null,
3636
bool enabled = true,
3737
float cursorWidth = 2.0f,
@@ -48,7 +48,7 @@ public TextFormField(
4848
validator: validator,
4949
autovalidate: autovalidate,
5050
enabled: enabled,
51-
builder: (FormFieldState field) => {
51+
builder: (FormFieldState<string> field) => {
5252
_TextFormFieldState state = (_TextFormFieldState) field;
5353
InputDecoration effectiveDecoration = (decoration ?? new InputDecoration())
5454
.applyDefaults(Theme.of(field.context).inputDecorationTheme);
@@ -96,7 +96,7 @@ public override State createState() {
9696
}
9797
}
9898

99-
class _TextFormFieldState : FormFieldState {
99+
class _TextFormFieldState : FormFieldState<string> {
100100
TextEditingController _controller;
101101

102102
public TextEditingController _effectiveController {
@@ -110,7 +110,7 @@ public TextEditingController _effectiveController {
110110
public override void initState() {
111111
base.initState();
112112
if (this.widget.controller == null) {
113-
this._controller = new TextEditingController(text: (string) this.widget.initialValue);
113+
this._controller = new TextEditingController(text: this.widget.initialValue);
114114
}
115115
else {
116116
this.widget.controller.addListener(this._handleControllerChanged);

Runtime/widgets/form.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public override State createState() {
3838

3939
public class FormState : State<Form> {
4040
int _generation = 0;
41-
public readonly HashSet<object> _fields = new HashSet<object>();
41+
readonly HashSet<FormFieldState> _fields = new HashSet<FormFieldState>();
4242

4343
public FormState() {
4444
}
@@ -55,11 +55,11 @@ void _forceRebuild() {
5555
this.setState(() => { ++this._generation; });
5656
}
5757

58-
public void _register(object field) {
58+
public void _register(FormFieldState field) {
5959
this._fields.Add(field);
6060
}
6161

62-
public void _unregister(object field) {
62+
public void _unregister(FormFieldState field) {
6363
this._fields.Remove(field);
6464
}
6565

@@ -133,19 +133,19 @@ public override bool updateShouldNotify(InheritedWidget _old) {
133133
}
134134
}
135135

136-
public delegate string FormFieldValidator(object value);
136+
public delegate string FormFieldValidator<T>(T value);
137137

138-
public delegate void FormFieldSetter(object newValue);
138+
public delegate void FormFieldSetter<T>(T newValue);
139139

140-
public delegate Widget FormFieldBuilder(FormFieldState field);
140+
public delegate Widget FormFieldBuilder<T>(FormFieldState<T> field) where T : class;
141141

142-
public class FormField : StatefulWidget {
142+
public class FormField<T> : StatefulWidget where T : class {
143143
public FormField(
144144
Key key = null,
145-
FormFieldBuilder builder = null,
146-
FormFieldSetter onSaved = null,
147-
FormFieldValidator validator = null,
148-
object initialValue = null,
145+
FormFieldBuilder<T> builder = null,
146+
FormFieldSetter<T> onSaved = null,
147+
FormFieldValidator<T> validator = null,
148+
T initialValue = null,
149149
bool autovalidate = false,
150150
bool enabled = true
151151
) : base(key: key) {
@@ -158,28 +158,36 @@ public FormField(
158158
this.enabled = enabled;
159159
}
160160

161-
public readonly FormFieldSetter onSaved;
161+
public readonly FormFieldSetter<T> onSaved;
162162

163-
public readonly FormFieldValidator validator;
163+
public readonly FormFieldValidator<T> validator;
164164

165-
public readonly FormFieldBuilder builder;
165+
public readonly FormFieldBuilder<T> builder;
166166

167-
public readonly object initialValue;
167+
public readonly T initialValue;
168168

169169
public readonly bool autovalidate;
170170

171171
public readonly bool enabled;
172172

173173
public override State createState() {
174-
return new FormFieldState();
174+
return new FormFieldState<T>();
175175
}
176176
}
177177

178-
public class FormFieldState : State<FormField> {
179-
object _value;
178+
public interface FormFieldState {
179+
void save();
180+
181+
bool validate();
182+
183+
void reset();
184+
}
185+
186+
public class FormFieldState<T> : State<FormField<T>>, FormFieldState where T : class {
187+
T _value;
180188
string _errorText;
181189

182-
public object value {
190+
public T value {
183191
get { return this._value; }
184192
}
185193

@@ -191,7 +199,7 @@ public bool hasError {
191199
get { return this._errorText != null; }
192200
}
193201

194-
public virtual void save() {
202+
public void save() {
195203
if (this.widget.onSaved != null) {
196204
this.widget.onSaved(this.value);
197205
}
@@ -217,12 +225,12 @@ bool _validate() {
217225
return !this.hasError;
218226
}
219227

220-
public virtual void didChange(object value) {
228+
public virtual void didChange(T value) {
221229
this.setState(() => { this._value = value; });
222230
Form.of(this.context)?._fieldDidChange();
223231
}
224232

225-
protected void setValue(object value) {
233+
protected void setValue(T value) {
226234
this._value = value;
227235
}
228236

0 commit comments

Comments
 (0)