浏览代码

Merge pull request #178 from UnityTech/yczhang

Text Form Field
/main
GitHub 6 年前
当前提交
cac40d4a
共有 4 个文件被更改,包括 181 次插入14 次删除
  1. 2
      Runtime/material/text_field.cs
  2. 34
      Runtime/widgets/form.cs
  3. 156
      Runtime/material/text_form_field.cs
  4. 3
      Runtime/material/text_form_field.cs.meta

2
Runtime/material/text_field.cs


properties.add(new EnumProperty<TextCapitalization>("textCapitalization", this.textCapitalization, defaultValue: TextCapitalization.none));
properties.add(new EnumProperty<TextAlign>("textAlign", this.textAlign, defaultValue: TextAlign.left));
properties.add(new EnumProperty<TextDirection>("textDirection", this.textDirection, defaultValue: null));
properties.add(new FloatProperty("cursorWidth", this.cursorWidth, defaultValue: 2.0));
properties.add(new FloatProperty("cursorWidth", this.cursorWidth, defaultValue: 2.0f));
properties.add(new DiagnosticsProperty<Radius>("cursorRadius", this.cursorRadius, defaultValue: null));
properties.add(new DiagnosticsProperty<Color>("cursorColor", this.cursorColor, defaultValue: null));
properties.add(new DiagnosticsProperty<Brightness?>("keyboardAppearance", this.keyboardAppearance, defaultValue: null));

34
Runtime/widgets/form.cs


public class FormState : State<Form> {
int _generation = 0;
public readonly HashSet<FormFieldState<dynamic>> _fields = new HashSet<FormFieldState<dynamic>>();
readonly HashSet<FormFieldState> _fields = new HashSet<FormFieldState>();
public FormState() {
}

this.setState(() => { ++this._generation; });
}
public void _register(FormFieldState<dynamic> field) {
public void _register(FormFieldState field) {
public void _unregister(FormFieldState<dynamic> field) {
public void _unregister(FormFieldState field) {
this._fields.Remove(field);
}

);
}
void save() {
foreach (FormFieldState<dynamic> field in this._fields) {
public void save() {
foreach (FormFieldState field in this._fields) {
void reset() {
foreach (FormFieldState<dynamic> field in this._fields) {
public void reset() {
foreach (FormFieldState field in this._fields) {
field.reset();
}

bool validate() {
public bool validate() {
this._forceRebuild();
return this._validate();
}

foreach (FormFieldState<dynamic> field in this._fields) {
foreach (FormFieldState field in this._fields) {
hasError = !field.validate() || hasError;
}

}
}
public class FormFieldState<T> : State<FormField<T>> where T : class {
public interface FormFieldState {
void save();
bool validate();
void reset();
}
public class FormFieldState<T> : State<FormField<T>>, FormFieldState where T : class {
T _value;
string _errorText;

}
}
public void reset() {
public virtual void reset() {
this.setState(() => {
this._value = this.widget.initialValue;
this._errorText = null;

}
public override void deactivate() {
Form.of(this.context)?._unregister(this as FormFieldState<dynamic>);
Form.of(this.context)?._unregister(this);
base.deactivate();
}

}
Form.of(context)?._register(this as FormFieldState<dynamic>);
Form.of(context)?._register(this);
return this.widget.builder(this);
}
}

156
Runtime/material/text_form_field.cs


using System.Collections.Generic;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.material;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.service;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
using TextStyle = Unity.UIWidgets.painting.TextStyle;
namespace UIWidgets.Runtime.material {
public class TextFormField : FormField<string> {
public TextFormField(
Key key = null,
TextEditingController controller = null,
string initialValue = null,
FocusNode focusNode = null,
InputDecoration decoration = null,
TextInputType keyboardType = null,
TextCapitalization textCapitalization = TextCapitalization.none,
TextInputAction? textInputAction = null,
TextStyle style = null,
TextDirection? textDirection = null,
TextAlign textAlign = TextAlign.left,
bool autofocus = false,
bool obscureText = false,
bool autocorrect = true,
bool autovalidate = false,
bool maxLengthEnforced = true,
int maxLines = 1,
int? maxLength = null,
VoidCallback onEditingComplete = null,
ValueChanged<string> onFieldSubmitted = null,
FormFieldSetter<string> onSaved = null,
FormFieldValidator<string> validator = null,
List<TextInputFormatter> inputFormatters = null,
bool enabled = true,
float cursorWidth = 2.0f,
Radius cursorRadius = null,
Color cursorColor = null,
Brightness? keyboardAppearance = null,
EdgeInsets scrollPadding = null,
bool enableInteractiveSelection = true,
InputCounterWidgetBuilder buildCounter = null
) : base(
key: key,
initialValue: controller != null ? controller.text : (initialValue ?? ""),
onSaved: onSaved,
validator: validator,
autovalidate: autovalidate,
enabled: enabled,
builder: (FormFieldState<string> field) => {
_TextFormFieldState state = (_TextFormFieldState) field;
InputDecoration effectiveDecoration = (decoration ?? new InputDecoration())
.applyDefaults(Theme.of(field.context).inputDecorationTheme);
return new TextField(
controller: state._effectiveController,
focusNode: focusNode,
decoration: effectiveDecoration.copyWith(errorText: field.errorText),
keyboardType: keyboardType,
textInputAction: textInputAction,
style: style,
textAlign: textAlign,
textDirection: textDirection ?? TextDirection.ltr,
textCapitalization: textCapitalization,
autofocus: autofocus,
obscureText: obscureText,
autocorrect: autocorrect,
maxLengthEnforced: maxLengthEnforced,
maxLines: maxLines,
maxLength: maxLength,
onChanged: field.didChange,
onEditingComplete: onEditingComplete,
onSubmitted: onFieldSubmitted,
inputFormatters: inputFormatters,
enabled: enabled,
cursorWidth: cursorWidth,
cursorRadius: cursorRadius,
cursorColor: cursorColor,
scrollPadding: scrollPadding ?? EdgeInsets.all(20.0f),
keyboardAppearance: keyboardAppearance,
enableInteractiveSelection: enableInteractiveSelection,
buildCounter: buildCounter
);
}
) {
D.assert(initialValue == null || controller == null);
D.assert(maxLines > 0);
D.assert(maxLength == null || maxLength > 0);
this.controller = controller;
}
public readonly TextEditingController controller;
public override State createState() {
return new _TextFormFieldState();
}
}
class _TextFormFieldState : FormFieldState<string> {
TextEditingController _controller;
public TextEditingController _effectiveController {
get { return this.widget.controller ?? this._controller; }
}
public new TextFormField widget {
get { return (TextFormField) base.widget; }
}
public override void initState() {
base.initState();
if (this.widget.controller == null) {
this._controller = new TextEditingController(text: this.widget.initialValue);
}
else {
this.widget.controller.addListener(this._handleControllerChanged);
}
}
public override void didUpdateWidget(StatefulWidget _oldWidget) {
TextFormField oldWidget = _oldWidget as TextFormField;
base.didUpdateWidget(oldWidget);
if (this.widget.controller != oldWidget.controller) {
oldWidget.controller?.removeListener(this._handleControllerChanged);
this.widget.controller?.addListener(this._handleControllerChanged);
if (oldWidget.controller != null && this.widget.controller == null) {
this._controller = TextEditingController.fromValue(oldWidget.controller.value);
}
if (this.widget.controller != null) {
this.setValue(this.widget.controller.text);
if (oldWidget.controller == null) {
this._controller = null;
}
}
}
}
public override void dispose() {
this.widget.controller?.removeListener(this._handleControllerChanged);
base.dispose();
}
public override void reset() {
base.reset();
this.setState(() => { this._effectiveController.text = (string) this.widget.initialValue; });
}
void _handleControllerChanged() {
if (this._effectiveController.text != this.value) {
this.didChange(this._effectiveController.text);
}
}
}
}

3
Runtime/material/text_form_field.cs.meta


fileFormatVersion: 2
guid: 0a9f1467be1c4653b3dbf8a1c7209f70
timeCreated: 1557304128
正在加载...
取消
保存