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

Commit f5407ef

Browse files
author
Yuncong Zhang
committed
Tmp store.
1 parent f58740d commit f5407ef

File tree

5 files changed

+198
-39
lines changed

5 files changed

+198
-39
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<T> where T : class {
712+
public class DropdownButtonFormField<T> : FormField 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<T> onSaved = null,
720-
FormFieldValidator<T> validator = null,
719+
FormFieldSetter onSaved = null,
720+
FormFieldValidator validator = null,
721721
Widget hint = null
722722
) : base(
723723
key: key,
724724
onSaved: onSaved,
725725
initialValue: value,
726726
validator: validator,
727-
builder: (FormFieldState<T> field) => {
727+
builder: (FormFieldState 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<T> where T : class {
755+
class _DropdownButtonFormFieldState<T> : FormFieldState where T : class {
756756
public new DropdownButtonFormField<T> widget {
757757
get { return base.widget as DropdownButtonFormField<T>; }
758758
}
759759

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

Runtime/material/text_field.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public override void debugFillProperties(DiagnosticPropertiesBuilder properties)
161161
properties.add(new EnumProperty<TextCapitalization>("textCapitalization", this.textCapitalization, defaultValue: TextCapitalization.none));
162162
properties.add(new EnumProperty<TextAlign>("textAlign", this.textAlign, defaultValue: TextAlign.left));
163163
properties.add(new EnumProperty<TextDirection>("textDirection", this.textDirection, defaultValue: null));
164-
properties.add(new FloatProperty("cursorWidth", this.cursorWidth, defaultValue: 2.0));
164+
properties.add(new FloatProperty("cursorWidth", this.cursorWidth, defaultValue: 2.0f));
165165
properties.add(new DiagnosticsProperty<Radius>("cursorRadius", this.cursorRadius, defaultValue: null));
166166
properties.add(new DiagnosticsProperty<Color>("cursorColor", this.cursorColor, defaultValue: null));
167167
properties.add(new DiagnosticsProperty<Brightness?>("keyboardAppearance", this.keyboardAppearance, defaultValue: null));

Runtime/material/text_form_field.cs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
using System.Collections.Generic;
2+
using Unity.UIWidgets.foundation;
3+
using Unity.UIWidgets.material;
4+
using Unity.UIWidgets.painting;
5+
using Unity.UIWidgets.service;
6+
using Unity.UIWidgets.ui;
7+
using Unity.UIWidgets.widgets;
8+
using TextStyle = Unity.UIWidgets.painting.TextStyle;
9+
10+
namespace UIWidgets.Runtime.material {
11+
public class TextFormField : FormField {
12+
public TextFormField(
13+
Key key = null,
14+
TextEditingController controller = null,
15+
string initialValue = null,
16+
FocusNode focusNode = null,
17+
InputDecoration decoration = null,
18+
TextInputType keyboardType = null,
19+
TextCapitalization textCapitalization = TextCapitalization.none,
20+
TextInputAction? textInputAction = null,
21+
TextStyle style = null,
22+
TextDirection? textDirection = null,
23+
TextAlign textAlign = TextAlign.left,
24+
bool autofocus = false,
25+
bool obscureText = false,
26+
bool autocorrect = true,
27+
bool autovalidate = false,
28+
bool maxLengthEnforced = true,
29+
int maxLines = 1,
30+
int? maxLength = null,
31+
VoidCallback onEditingComplete = null,
32+
ValueChanged<string> onFieldSubmitted = null,
33+
FormFieldSetter onSaved = null,
34+
FormFieldValidator validator = null,
35+
List<TextInputFormatter> inputFormatters = null,
36+
bool enabled = true,
37+
float cursorWidth = 2.0f,
38+
Radius cursorRadius = null,
39+
Color cursorColor = null,
40+
Brightness? keyboardAppearance = null,
41+
EdgeInsets scrollPadding = null,
42+
bool enableInteractiveSelection = true,
43+
InputCounterWidgetBuilder buildCounter = null
44+
) : base(
45+
key: key,
46+
initialValue: controller != null ? controller.text : (initialValue ?? ""),
47+
onSaved: onSaved,
48+
validator: validator,
49+
autovalidate: autovalidate,
50+
enabled: enabled,
51+
builder: (FormFieldState field) => {
52+
_TextFormFieldState state = (_TextFormFieldState) field;
53+
InputDecoration effectiveDecoration = (decoration ?? new InputDecoration())
54+
.applyDefaults(Theme.of(field.context).inputDecorationTheme);
55+
return new TextField(
56+
controller: state._effectiveController,
57+
focusNode: focusNode,
58+
decoration: effectiveDecoration.copyWith(errorText: field.errorText),
59+
keyboardType: keyboardType,
60+
textInputAction: textInputAction,
61+
style: style,
62+
textAlign: textAlign,
63+
textDirection: textDirection ?? TextDirection.ltr,
64+
textCapitalization: textCapitalization,
65+
autofocus: autofocus,
66+
obscureText: obscureText,
67+
autocorrect: autocorrect,
68+
maxLengthEnforced: maxLengthEnforced,
69+
maxLines: maxLines,
70+
maxLength: maxLength,
71+
onChanged: field.didChange,
72+
onEditingComplete: onEditingComplete,
73+
onSubmitted: onFieldSubmitted,
74+
inputFormatters: inputFormatters,
75+
enabled: enabled,
76+
cursorWidth: cursorWidth,
77+
cursorRadius: cursorRadius,
78+
cursorColor: cursorColor,
79+
scrollPadding: scrollPadding ?? EdgeInsets.all(20.0f),
80+
keyboardAppearance: keyboardAppearance,
81+
enableInteractiveSelection: enableInteractiveSelection,
82+
buildCounter: buildCounter
83+
);
84+
}
85+
) {
86+
D.assert(initialValue == null || controller == null);
87+
D.assert(maxLines > 0);
88+
D.assert(maxLength == null || maxLength > 0);
89+
this.controller = controller;
90+
}
91+
92+
public readonly TextEditingController controller;
93+
94+
public override State createState() {
95+
return new _TextFormFieldState();
96+
}
97+
}
98+
99+
class _TextFormFieldState : FormFieldState {
100+
TextEditingController _controller;
101+
102+
public TextEditingController _effectiveController {
103+
get { return this.widget.controller ?? this._controller; }
104+
}
105+
106+
public new TextFormField widget {
107+
get { return (TextFormField) base.widget; }
108+
}
109+
110+
public override void initState() {
111+
base.initState();
112+
if (this.widget.controller == null) {
113+
this._controller = new TextEditingController(text: (string) this.widget.initialValue);
114+
}
115+
else {
116+
this.widget.controller.addListener(this._handleControllerChanged);
117+
}
118+
}
119+
120+
public override void didUpdateWidget(StatefulWidget _oldWidget) {
121+
TextFormField oldWidget = _oldWidget as TextFormField;
122+
base.didUpdateWidget(oldWidget);
123+
if (this.widget.controller != oldWidget.controller) {
124+
oldWidget.controller?.removeListener(this._handleControllerChanged);
125+
this.widget.controller?.addListener(this._handleControllerChanged);
126+
127+
if (oldWidget.controller != null && this.widget.controller == null) {
128+
this._controller = TextEditingController.fromValue(oldWidget.controller.value);
129+
}
130+
131+
if (this.widget.controller != null) {
132+
this.setValue(this.widget.controller.text);
133+
if (oldWidget.controller == null) {
134+
this._controller = null;
135+
}
136+
}
137+
}
138+
}
139+
140+
public override void dispose() {
141+
this.widget.controller?.removeListener(this._handleControllerChanged);
142+
base.dispose();
143+
}
144+
145+
public override void reset() {
146+
base.reset();
147+
this.setState(() => { this._effectiveController.text = (string) this.widget.initialValue; });
148+
}
149+
150+
void _handleControllerChanged() {
151+
if (this._effectiveController.text != this.value) {
152+
this.didChange(this._effectiveController.text);
153+
}
154+
}
155+
}
156+
}

Runtime/material/text_form_field.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/widgets/form.cs

Lines changed: 31 additions & 31 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<FormFieldState<dynamic>> _fields = new HashSet<FormFieldState<dynamic>>();
41+
public readonly HashSet<object> _fields = new HashSet<object>();
4242

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

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

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

@@ -78,28 +78,28 @@ public override Widget build(BuildContext context) {
7878
);
7979
}
8080

81-
void save() {
82-
foreach (FormFieldState<dynamic> field in this._fields) {
81+
public void save() {
82+
foreach (FormFieldState field in this._fields) {
8383
field.save();
8484
}
8585
}
8686

87-
void reset() {
88-
foreach (FormFieldState<dynamic> field in this._fields) {
87+
public void reset() {
88+
foreach (FormFieldState field in this._fields) {
8989
field.reset();
9090
}
9191

9292
this._fieldDidChange();
9393
}
9494

95-
bool validate() {
95+
public bool validate() {
9696
this._forceRebuild();
9797
return this._validate();
9898
}
9999

100100
bool _validate() {
101101
bool hasError = false;
102-
foreach (FormFieldState<dynamic> field in this._fields) {
102+
foreach (FormFieldState field in this._fields) {
103103
hasError = !field.validate() || hasError;
104104
}
105105

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

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

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

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

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

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

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

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

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

169169
public readonly bool autovalidate;
170170

171171
public readonly bool enabled;
172172

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

178-
public class FormFieldState<T> : State<FormField<T>> where T : class {
179-
T _value;
178+
public class FormFieldState : State<FormField> {
179+
object _value;
180180
string _errorText;
181181

182-
public T value {
182+
public object value {
183183
get { return this._value; }
184184
}
185185

@@ -191,13 +191,13 @@ public bool hasError {
191191
get { return this._errorText != null; }
192192
}
193193

194-
public void save() {
194+
public virtual void save() {
195195
if (this.widget.onSaved != null) {
196196
this.widget.onSaved(this.value);
197197
}
198198
}
199199

200-
public void reset() {
200+
public virtual void reset() {
201201
this.setState(() => {
202202
this._value = this.widget.initialValue;
203203
this._errorText = null;
@@ -217,12 +217,12 @@ bool _validate() {
217217
return !this.hasError;
218218
}
219219

220-
public virtual void didChange(T value) {
220+
public virtual void didChange(object value) {
221221
this.setState(() => { this._value = value; });
222222
Form.of(this.context)?._fieldDidChange();
223223
}
224224

225-
protected void setValue(T value) {
225+
protected void setValue(object value) {
226226
this._value = value;
227227
}
228228

@@ -232,7 +232,7 @@ public override void initState() {
232232
}
233233

234234
public override void deactivate() {
235-
Form.of(this.context)?._unregister(this as FormFieldState<dynamic>);
235+
Form.of(this.context)?._unregister(this);
236236
base.deactivate();
237237
}
238238

@@ -241,7 +241,7 @@ public override Widget build(BuildContext context) {
241241
this._validate();
242242
}
243243

244-
Form.of(context)?._register(this as FormFieldState<dynamic>);
244+
Form.of(context)?._register(this);
245245
return this.widget.builder(this);
246246
}
247247
}

0 commit comments

Comments
 (0)