浏览代码

Update text field and text theme. Partially update editable text.

Finish updating material.
/main
Yuncong Zhang 6 年前
当前提交
c03875b2
共有 4 个文件被更改,包括 177 次插入83 次删除
  1. 4
      Runtime/material/input_decorator.cs
  2. 146
      Runtime/material/text_field.cs
  3. 28
      Runtime/material/text_theme.cs
  4. 82
      Runtime/widgets/editable_text.cs

4
Runtime/material/input_decorator.cs


TextStyle helperStyle = null,
string hintText = null,
TextStyle hintStyle = null,
int? hintMaxlines = null,
int? hintMaxLines = null,
string errorText = null,
TextStyle errorStyle = null,
int? errorMaxLines = null,

helperStyle: helperStyle ?? this.helperStyle,
hintText: hintText ?? this.hintText,
hintStyle: hintStyle ?? this.hintStyle,
hintMaxLines: hintMaxlines ?? this.hintMaxLines,
hintMaxLines: hintMaxLines ?? this.hintMaxLines,
errorText: errorText ?? this.errorText,
errorStyle: errorStyle ?? this.errorStyle,
errorMaxLines: errorMaxLines ?? this.errorMaxLines,

146
Runtime/material/text_field.cs


using Unity.UIWidgets.service;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
using UnityEngine;
using Color = Unity.UIWidgets.ui.Color;
public delegate Widget InputCounterWidgetBuilder(
BuildContext buildContext,
int? currentLength,
int? maxLength,
bool? isFocused);
public class TextField : StatefulWidget {
public TextField(Key key = null, TextEditingController controller = null, FocusNode focusNode = null,
InputDecoration decoration = null, bool noDecoration = false, TextInputType keyboardType = null,

ValueChanged<string> onSubmitted = null, List<TextInputFormatter> inputFormatters = null,
bool? enabled = null, float? cursorWidth = 2.0f, Radius cursorRadius = null, Color cursorColor = null,
Brightness? keyboardAppearance = null, EdgeInsets scrollPadding = null,
bool enableInteractiveSelection = true,
GestureTapCallback onTap = null
DragStartBehavior dragStartBehavior = DragStartBehavior.down,
bool? enableInteractiveSelection = null,
GestureTapCallback onTap = null,
InputCounterWidgetBuilder buildCounter = null
D.assert(maxLength == null || maxLength > 0);
D.assert(maxLength == null || maxLength == TextField.noMaxLength || maxLength > 0);
this.controller = controller;
this.focusNode = focusNode;

this.onTap = onTap;
this.keyboardType = keyboardType ?? (maxLines == 1 ? TextInputType.text : TextInputType.multiline);
this.scrollPadding = scrollPadding ?? EdgeInsets.all(20.0f);
this.dragStartBehavior = dragStartBehavior;
this.buildCounter = buildCounter;
}
public readonly TextEditingController controller;

public readonly bool autocorrect;
public readonly int? maxLines;
public const long noMaxLength = 9007199254740992; // math.pow(2, 53);
public const long noMaxLength = -1;
public readonly int? maxLength;

public readonly EdgeInsets scrollPadding;
public readonly bool enableInteractiveSelection;
public readonly bool? enableInteractiveSelection;
public readonly DragStartBehavior dragStartBehavior;
public bool selectionEnabled {
get {
return this.enableInteractiveSelection ?? !this.obscureText;
}
}
public readonly InputCounterWidgetBuilder buildCounter;
public override State createState() {
return new _TextFieldState();

new DiagnosticsProperty<TextEditingController>("controller", this.controller, defaultValue: null));
properties.add(new DiagnosticsProperty<FocusNode>("focusNode", this.focusNode, defaultValue: null));
properties.add(new DiagnosticsProperty<bool?>("enabled", this.enabled, defaultValue: null));
properties.add(new DiagnosticsProperty<InputDecoration>("decoration", this.decoration));
properties.add(new DiagnosticsProperty<InputDecoration>("decoration", this.decoration, defaultValue: new InputDecoration()));
properties.add(new DiagnosticsProperty<bool>("autocorrect", this.autocorrect, defaultValue: false));
properties.add(new DiagnosticsProperty<bool>("autocorrect", this.autocorrect, defaultValue: true));
properties.add(new FlagProperty("maxLengthEnforced", value: this.maxLengthEnforced,
ifTrue: "max length enforced"));
properties.add(new DiagnosticsProperty<GestureTapCallback>("onTap", this.onTap, defaultValue: null));
properties.add(new FlagProperty("maxLengthEnforced", value: this.maxLengthEnforced, defaultValue: true,
ifFalse: "maxLength not enforced"));
properties.add(new EnumProperty<TextInputAction?>("textInputAction", this.textInputAction, defaultValue: null));
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 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));
properties.add(new DiagnosticsProperty<EdgeInsets>("scrollPadding", this.scrollPadding, defaultValue: EdgeInsets.all(20.0f)));
properties.add(new FlagProperty("selectionEnabled", value: this.selectionEnabled, defaultValue: true, ifFalse: "selection disabled"));
}
}

InputDecoration _getEffectiveDecoration() {
MaterialLocalizations localizations = MaterialLocalizations.of(this.context);
ThemeData themeData = Theme.of(this.context);
.applyDefaults(Theme.of(this.context).inputDecorationTheme)
.applyDefaults(themeData.inputDecorationTheme)
enabled: this.widget.enabled
enabled: this.widget.enabled,
hintMaxLines: this.widget.decoration?.hintMaxLines ?? this.widget.maxLines
if (!this.needsCounter) {
if (effectiveDecoration.counter != null || effectiveDecoration.counterText != null) {
Widget counter;
if (effectiveDecoration.counter == null
&& effectiveDecoration.counterText == null
&& this.widget.buildCounter != null) {
bool isFocused = this._effectiveFocusNode.hasFocus;
counter = this.widget.buildCounter(
this.context,
currentLength: currentLength,
maxLength: this.widget.maxLength,
isFocused: isFocused
);
return effectiveDecoration.copyWith(counter: counter);
}
if (this.widget.maxLength == null)
return effectiveDecoration;
if (this.widget.maxLength != TextField.noMaxLength) {
if (this.widget.maxLength > 0) {
if (this._effectiveController.value.text.Length > this.widget.maxLength) {
return effectiveDecoration.copyWith(
errorText: effectiveDecoration.errorText ?? "",
counterStyle: effectiveDecoration.errorStyle
?? themeData.textTheme.caption.copyWith(color: themeData.errorColor),
counterText: counterText
);
}
if (this._effectiveController.value.text.Length > this.widget.maxLength) {
ThemeData themeData = Theme.of(this.context);
return effectiveDecoration.copyWith(
errorText: effectiveDecoration.errorText ?? "",
counterStyle: effectiveDecoration.errorStyle
?? themeData.textTheme.caption.copyWith(color: themeData.errorColor),
counterText: counterText
);
}
return effectiveDecoration.copyWith(
counterText: counterText

}
void _handleSelectionChanged(TextSelection selection, SelectionChangedCause cause) {
if (cause == SelectionChangedCause.longPress) {
// Feedback.forLongPress(context); todo add feedback
if (Theme.of(this.context).platform == RuntimePlatform.IPhonePlayer
&& cause == SelectionChangedCause.longPress) {
this._editableTextKey.currentState?.bringIntoView(selection.basePos);
ThemeData themeData = Theme.of(this.context);
Color color = Theme.of(this.context).splashColor;
Color color = themeData.splashColor;
InteractiveInkFeature splash = null;

} // else we're probably in deactivate()
}
splash = Theme.of(this.context).splashFactory.create(
splash = themeData.splashFactory.create(
controller: inkController,
referenceBox: referenceBox,
position: position,

}
void _handleSingleTapUp(TapUpDetails details) {
if (this.widget.enableInteractiveSelection) {
if (this.widget.enableInteractiveSelection == true) {
this._renderEditable.handleTap();
}

}
void _handleLongPress() {
if (this.widget.enableInteractiveSelection) {
if (this.widget.enableInteractiveSelection == true) {
this._renderEditable.handleLongPress();
}

base.build(context); // See AutomaticKeepAliveClientMixin.
D.assert(MaterialD.debugCheckHasMaterial(context));
D.assert(WidgetsD.debugCheckHasDirectionality(context));
D.assert(
!(this.widget.style != null && this.widget.style.inherit == false &&
(this.widget.style.fontSize == null || this.widget.style.textBaseline == null)),
"inherit false style must supply fontSize and textBaseline"
);
TextStyle style = this.widget.style ?? themeData.textTheme.subhead;
TextStyle style = themeData.textTheme.subhead.merge(this.widget.style);
Brightness keyboardAppearance = this.widget.keyboardAppearance ?? themeData.primaryColorBrightness;
TextEditingController controller = this._effectiveController;
FocusNode focusNode = this._effectiveFocusNode;

}
bool forcePressEnabled = false;
TextSelectionControls textSelectionControls = MaterialUtils.materialTextSelectionControls;;
bool paintCursorAboveText = false;
bool cursorOpacityAnimates = false;
Offset cursorOffset = null;
Color cursorColor = this.widget.cursorColor ?? themeData.cursorColor;
Radius cursorRadius = this.widget.cursorRadius;
Widget child = new RepaintBoundary(
child: new EditableText(

autocorrect: this.widget.autocorrect,
maxLines: this.widget.maxLines,
selectionColor: themeData.textSelectionColor,
selectionControls: this.widget.enableInteractiveSelection
? MaterialUtils.materialTextSelectionControls
: null,
selectionControls: this.widget.selectionEnabled ? textSelectionControls : null,
onSelectionChanged: this._handleSelectionChanged,
onSelectionChanged: this._handleSelectionChanged,
cursorRadius: this.widget.cursorRadius,
cursorColor: this.widget.cursorColor ?? Theme.of(context).cursorColor,
cursorRadius: cursorRadius,
cursorColor: cursorColor,
cursorOpacityAnimates: cursorOpacityAnimates,
cursorOffset: cursorOffset,
paintCursorAboveText: paintCursorAboveText,
backgroundCursorColor: new Color(0xFF8E8E93),// TODO: CupertinoColors.inactiveGray,
enableInteractiveSelection: this.widget.enableInteractiveSelection
enableInteractiveSelection: this.widget.enableInteractiveSelection == true,
dragStartBehavior: this.widget.dragStartBehavior
)
);

28
Runtime/material/text_theme.cs


}
public static TextTheme lerp(TextTheme a, TextTheme b, float t) {
D.assert(a != null);
D.assert(b != null);
display4: TextStyle.lerp(a.display4, b.display4, t),
display3: TextStyle.lerp(a.display3, b.display3, t),
display2: TextStyle.lerp(a.display2, b.display2, t),
display1: TextStyle.lerp(a.display1, b.display1, t),
headline: TextStyle.lerp(a.headline, b.headline, t),
title: TextStyle.lerp(a.title, b.title, t),
subhead: TextStyle.lerp(a.subhead, b.subhead, t),
body2: TextStyle.lerp(a.body2, b.body2, t),
body1: TextStyle.lerp(a.body1, b.body1, t),
caption: TextStyle.lerp(a.caption, b.caption, t),
button: TextStyle.lerp(a.button, b.button, t),
subtitle: TextStyle.lerp(a.subtitle, b.subtitle, t),
overline: TextStyle.lerp(a.overline, b.overline, t)
display4: TextStyle.lerp(a?.display4, b?.display4, t),
display3: TextStyle.lerp(a?.display3, b?.display3, t),
display2: TextStyle.lerp(a?.display2, b?.display2, t),
display1: TextStyle.lerp(a?.display1, b?.display1, t),
headline: TextStyle.lerp(a?.headline, b?.headline, t),
title: TextStyle.lerp(a?.title, b?.title, t),
subhead: TextStyle.lerp(a?.subhead, b?.subhead, t),
body2: TextStyle.lerp(a?.body2, b?.body2, t),
body1: TextStyle.lerp(a?.body1, b?.body1, t),
caption: TextStyle.lerp(a?.caption, b?.caption, t),
button: TextStyle.lerp(a?.button, b?.button, t),
subtitle: TextStyle.lerp(a?.subtitle, b?.subtitle, t),
overline: TextStyle.lerp(a?.overline, b?.overline, t)
);
}

82
Runtime/widgets/editable_text.cs


using Unity.UIWidgets.animation;
using Unity.UIWidgets.async;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.gestures;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.scheduler;

public readonly Color cursorColor;
public readonly Color backgroundCursorColor;
public readonly int? maxLines;
public readonly bool autofocus;

public readonly bool unityTouchKeyboard;
public EditableText(TextEditingController controller, FocusNode focusNode, TextStyle style,
Color cursorColor, bool obscureText = false, bool autocorrect = false,
Color cursorColor, Color backgroundCursorColor = null, bool obscureText = false, bool autocorrect = false,
TextAlign textAlign = TextAlign.left, TextDirection? textDirection = null,
float? textScaleFactor = null, int? maxLines = 1,
bool autofocus = false, Color selectionColor = null, TextSelectionControls selectionControls = null,

ValueChanged<string> onSubmitted = null, SelectionChangedCallback onSelectionChanged = null,
List<TextInputFormatter> inputFormatters = null, bool rendererIgnoresPointer = false,
EdgeInsets scrollPadding = null, bool unityTouchKeyboard = false,
Key key = null, float? cursorWidth = 2.0f, Radius cursorRadius = null, Brightness? keyboardAppearance = Brightness.light,
bool enableInteractiveSelection = true
) : base(key) {
Key key = null, float? cursorWidth = 2.0f, Radius cursorRadius = null, bool cursorOpacityAnimates = false,
Offset cursorOffset = null, bool paintCursorAboveText = false,
Brightness? keyboardAppearance = Brightness.light,
DragStartBehavior dragStartBehavior = DragStartBehavior.down,
bool? enableInteractiveSelection = null
) : base(key) {
D.assert(backgroundCursorColor != null);
this.keyboardType = keyboardType ?? (maxLines == 1 ? TextInputType.text : TextInputType.multiline);
this.scrollPadding = scrollPadding ?? EdgeInsets.all(20.0f);

this.cursorWidth = cursorWidth;
this.cursorRadius = cursorRadius;
this.cursorOpacityAnimates = cursorOpacityAnimates;
this.cursorOffset = cursorOffset;
this.paintCursorAboveText = paintCursorAboveText;
this.dragStartBehavior = dragStartBehavior;
public readonly bool cursorOpacityAnimates;
public readonly Offset cursorOffset;
public readonly bool paintCursorAboveText;
public readonly DragStartBehavior dragStartBehavior;
public readonly bool enableInteractiveSelection;
public readonly bool? enableInteractiveSelection;
public bool selectionEnabled {
get { return this.enableInteractiveSelection ?? !this.obscureText; }
}
public override State createState() {
return new EditableTextState();
}

}
}
public class EditableTextState : AutomaticKeepAliveClientMixin<EditableText>, WidgetsBindingObserver, TextInputClient,
public class EditableTextState : AutomaticKeepAliveClientWithTickerProviderStateMixin<EditableText>,
WidgetsBindingObserver, TextInputClient,
static TimeSpan _kCursorBlinkWaitForStart = TimeSpan.FromMilliseconds(150);
static readonly TimeSpan _fadeDuration = TimeSpan.FromMilliseconds(500);
static readonly TimeSpan _floatingCursorResetTime = TimeSpan.FromMilliseconds(150);
ValueNotifier<bool> _showCursor = new ValueNotifier<bool>(false);
bool _targetCursorVisibility = false;
ValueNotifier<bool> _cursorVisibilityNotifier = new ValueNotifier<bool>(false);
AnimationController _cursorBlinkOpacityController;
TextInputConnection _textInputConnection;
TextSelectionOverlay _selectionOverlay;

? TextInputAction.newline
: TextInputAction.done),
textCapitalization: this.widget.textCapitalization,
keyboardAppearance: this.widget.keyboardAppearance??Brightness.light,
keyboardAppearance: this.widget.keyboardAppearance ?? Brightness.light,
));
this._textInputConnection.setEditingState(localValue);
this._updateImePosIfNeed();

);
});
}
if (this._lastBottomViewInset < Window.instance.viewInsets.bottom) {
this._showCaretOnScreen();
if (this._lastBottomViewInset < Window.instance.viewInsets.bottom) {
this._showCaretOnScreen();
this._lastBottomViewInset = Window.instance.viewInsets.bottom;
this._lastBottomViewInset = Window.instance.viewInsets.bottom;
public void didChangeTextScaleFactor() {}
public void didChangeTextScaleFactor() {
}
public void didChangeLocales(List<Locale> locale) {}
public void didChangeLocales(List<Locale> locale) {
}
public IPromise<bool> didPopRoute() {
return Promise<bool>.Resolved(false);

}
public bool cursorCurrentlyVisible {
get { return this._showCursor.value; }
get { return this._cursorVisibilityNotifier.value; }
}
public TimeSpan cursorBlinkInterval {

void _cursorTick() {
this._showCursor.value = !this._unityKeyboard() && !this._showCursor.value;
this._cursorVisibilityNotifier.value = !this._unityKeyboard() && !this._cursorVisibilityNotifier.value;
if (this._obscureShowCharTicksPending > 0) {
this.setState(() => { this._obscureShowCharTicksPending--; });
}

this._showCursor.value = !this._unityKeyboard();
this._cursorVisibilityNotifier.value = !this._unityKeyboard();
this._cursorTimer = Window.instance.run(_kCursorBlinkHalfPeriod, this._cursorTick,
periodic: true);
}

}
this._cursorTimer = null;
this._showCursor.value = false;
this._cursorVisibilityNotifier.value = false;
this._obscureShowCharTicksPending = 0;
}

WidgetsBinding.instance.removeObserver(this);
this._value = new TextEditingValue(text: this._value.text);
}
this.updateKeepAlive();
}

textSpan: this.buildTextSpan(),
value: this._value,
cursorColor: this.widget.cursorColor,
showCursor: this._showCursor,
showCursor: this._cursorVisibilityNotifier,
hasFocus: this._hasFocus,
maxLines: this.widget.maxLines,
selectionColor: this.widget.selectionColor,

rendererIgnoresPointer: this.widget.rendererIgnoresPointer,
cursorWidth: this.widget.cursorWidth,
cursorRadius: this.widget.cursorRadius,
enableInteractiveSelection: this.widget.enableInteractiveSelection,
enableInteractiveSelection: this.widget.enableInteractiveSelection == true,
textSelectionDelegate: this
)
)

}
bool _imePosUpdateScheduled = false;
void _updateImePosIfNeed() {
if (!this._hasInputConnection || !this._textInputConnection.imeRequired()) {
return;

return;
}
this._imePosUpdateScheduled = true;
SchedulerBinding.instance.addPostFrameCallback(_ => {
this._imePosUpdateScheduled = false;

this._textInputConnection.setIMEPos(this._getImePos());
});
}

TextDirection? textDirection = null, bool obscureText = false, TextAlign textAlign = TextAlign.left,
bool autocorrect = false, ViewportOffset offset = null, SelectionChangedHandler onSelectionChanged = null,
CaretChangedHandler onCaretChanged = null, bool rendererIgnoresPointer = false,
Key key = null, TextSelectionDelegate textSelectionDelegate = null, float? cursorWidth = null,
Key key = null, TextSelectionDelegate textSelectionDelegate = null, float? cursorWidth = null,
Radius cursorRadius = null, bool enableInteractiveSelection = true) : base(key) {
this.textSpan = textSpan;
this.value = value;

this.cursorWidth = cursorWidth;
this.cursorRadius = cursorRadius;
this.enableInteractiveSelection = enableInteractiveSelection;
}
public override RenderObject createRenderObject(BuildContext context) {

onSelectionChanged: this.onSelectionChanged,
onCaretChanged: this.onCaretChanged,
ignorePointer: this.rendererIgnoresPointer,
cursorWidth: this.cursorWidth??1.0f,
cursorWidth: this.cursorWidth ?? 1.0f,
cursorRadius: this.cursorRadius,
enableInteractiveSelection: this.enableInteractiveSelection,
textSelectionDelegate: this.textSelectionDelegate

正在加载...
取消
保存