using System.Collections.Generic; using uiwidgets; using Unity.UIWidgets.foundation; using Unity.UIWidgets.material; using Unity.UIWidgets.painting; using Unity.UIWidgets.rendering; using Unity.UIWidgets.service; using Unity.UIWidgets.ui; using Unity.UIWidgets.widgets; using UnityEngine; using Color = Unity.UIWidgets.ui.Color; using TextStyle = Unity.UIWidgets.painting.TextStyle; using ui_ = Unity.UIWidgets.widgets.ui_; namespace UIWidgetsSample { public class TextInputSample : UIWidgetsSamplePanel { protected override void main() { ui_.runApp( new MaterialApp( home: new EditableInputTypeWidget() ) ); } protected new void OnEnable() { base.OnEnable(); } class _TextInputSample : StatefulWidget { public readonly string title; public _TextInputSample(Key key = null, string title = null) : base(key) { this.title = title; } public override State createState() { return new _TextInputSampleState(); } } class _TextInputSampleState : State<_TextInputSample> { TextEditingController titleController = new TextEditingController(""); TextEditingController descController = new TextEditingController(""); FocusNode _titleFocusNode; FocusNode _descFocusNode; public override void initState() { base.initState(); this._titleFocusNode = new FocusNode(); this._descFocusNode = new FocusNode(); } public override void dispose() { this._titleFocusNode.dispose(); this._descFocusNode.dispose(); base.dispose(); } Widget title() { return new Container(child: new Text(this.widget.title ?? "", textAlign: TextAlign.center, style: new TextStyle(fontSize: 24, fontWeight: FontWeight.w700)), margin: EdgeInsets.only(bottom: 20)); } Widget titleInput() { return new Row( children: new List( ) { new SizedBox(width: 100, child: new Text("Title")), new Flexible(child: new Container( decoration: new BoxDecoration(border: Border.all(new Color(0xFF000000), 1)), padding: EdgeInsets.fromLTRB(8, 0, 8, 0), child: new EditableText(maxLines: 1, controller: this.titleController, selectionControls: MaterialUtils.materialTextSelectionControls, backgroundCursorColor: Colors.transparent, autofocus: true, focusNode: new FocusNode(), style: new TextStyle( fontSize: 18, height: 1.5f, color: new Color(0xFF1389FD) ), selectionColor: Color.fromARGB(255, 255, 0, 0), cursorColor: Color.fromARGB(255, 0, 0, 0)) )), } ); } Widget descInput() { return new Container( margin: EdgeInsets.fromLTRB(0, 10, 0, 10), child: new Row( children: new List( ) { new SizedBox(width: 100, child: new Text("Description")), new Flexible(child: new Container( height: 200, decoration: new BoxDecoration(border: Border.all(new Color(0xFF000000), 1)), padding: EdgeInsets.fromLTRB(8, 0, 8, 0), child: new EditableText(maxLines: 200, controller: this.descController, backgroundCursorColor: Colors.transparent, selectionControls: MaterialUtils.materialTextSelectionControls, focusNode: new FocusNode(), style: new TextStyle( fontSize: 18, height: 1.5f, color: new Color(0xFF1389FD) ), selectionColor: Color.fromARGB(255, 255, 0, 0), cursorColor: Color.fromARGB(255, 0, 0, 0)) )), } )); } public override Widget build(BuildContext context) { var container = new Container( padding: EdgeInsets.all(10), decoration: new BoxDecoration(color: new Color(0x7F000000), border: Border.all(color: Color.fromARGB(255, 255, 0, 0), width: 5), borderRadius: BorderRadius.all(2)), child: new Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: new List { this.title(), this.titleInput(), this.descInput(), } ) ); return container; } } } public class EditableInputTypeWidget : StatefulWidget { public EditableInputTypeWidget(Key key = null) : base(key) { } public override State createState() { return new _EditableInputTypeWidgetState(); } } class _EditableInputTypeWidgetState : State { Widget rowWidgets(string title, Widget widget) { return new Container( height: 80, child: new Row( children: new List { new Container(width: 100, child: new Text(title, style: new TextStyle(fontSize: 14, height: 2.0f, color: Colors.black, decoration: TextDecoration.none))), new Flexible(child: new Container(child: widget, padding: EdgeInsets.all(4), decoration: new BoxDecoration(border: Border.all(color: Color.black)))) } )); } void textSubmitted(string text) { Debug.Log($"text submitted {text}"); } List buildInputs(bool unityKeyboard) { List widgets = new List(); var style = new TextStyle(); var cursorColor = new Color(0xFF000000); var selectionColor = new Color(0xFF6F6F6F); widgets.Add(this.rowWidgets("Default", new EditStateProvider(builder: ((buildContext, controller, node) => new EditableText(controller: controller, focusNode: node, style: style, cursorColor: cursorColor, backgroundCursorColor: Colors.blue, selectionColor: selectionColor, onSubmitted: this.textSubmitted , unityTouchKeyboard: unityKeyboard, selectionControls: MaterialUtils.materialTextSelectionControls, cursorWidth: 5.0f, cursorRadius: Radius.circular(2.5f), cursorOpacityAnimates: true, paintCursorAboveText: true))))); widgets.Add(this.rowWidgets("Multiple Line", new EditStateProvider( builder: ((buildContext, controller, node) => new EditableText(controller: controller, focusNode: node, style: style, cursorColor: cursorColor, backgroundCursorColor: Colors.transparent, selectionColor: selectionColor, maxLines: 4, onSubmitted: this.textSubmitted, unityTouchKeyboard: unityKeyboard, selectionControls: MaterialUtils.materialTextSelectionControls // , // globalKeyEventHandler: (evt, enableCustomAction) => { // //customized key event handler which may swallow input keys to the editable and // //perform customized functionality // // // //evt is the input rawKeyEvent // //you should perform any customized functionality within the closure of "if(enableCustomAction) {}", // //otherwise it could be performed multiple times for a single key event. // // // //Very Important: for any input event, please ensure that all the output RawInputKeyResponse is the same // //regardless of the value of enableCustomAction, otherwise the behavior of this handler would become // //wrong and unpredictable // if (evt.data.unityEvent.keyCode == KeyCode.UpArrow) { // if (enableCustomAction) { // Debug.Log("UpUpUp"); // } // // return RawInputKeyResponse.swallowResponse; // } // // if (evt.data.unityEvent.keyCode == KeyCode.DownArrow) { // if (enableCustomAction) { // Debug.Log("UpUpUp"); // } // // return RawInputKeyResponse.swallowResponse; // } // // if (evt.data.unityEvent.character == '\n' || // evt.data.unityEvent.character == '\r' || // evt.data.unityEvent.character == 3 || // evt.data.unityEvent.character == 10) { // // if (evt.data.unityEvent.shift) { // if (enableCustomAction) { // Debug.Log("shift return >>>"); // } // return new RawInputKeyResponse(true, evt.data.unityEvent.character, inputAction: TextInputAction.newline); // } // else { // if (enableCustomAction) { // Debug.Log("send !!!!"); // } // return RawInputKeyResponse.swallowResponse; // } // } // // return RawInputKeyResponse.convert(evt); // } ))))); widgets.Add(this.rowWidgets("ObscureText", new EditStateProvider( builder: ((buildContext, controller, node) => new EditableText(controller: controller, focusNode: node, style: style, cursorColor: cursorColor, backgroundCursorColor: Colors.transparent, selectionColor: selectionColor, obscureText: true, onSubmitted: this.textSubmitted, unityTouchKeyboard: unityKeyboard, selectionControls: MaterialUtils.materialTextSelectionControls))))); widgets.Add(this.rowWidgets("Number", new EditStateProvider(builder: ((buildContext, controller, node) => new EditableText(controller: controller, focusNode: node, style: style, cursorColor: cursorColor, backgroundCursorColor: Colors.transparent, selectionColor: selectionColor, keyboardType: TextInputType.number, onSubmitted: this.textSubmitted, unityTouchKeyboard: unityKeyboard, selectionControls: MaterialUtils.materialTextSelectionControls))))); widgets.Add(this.rowWidgets("Phone", new EditStateProvider(builder: ((buildContext, controller, node) => new EditableText(controller: controller, focusNode: node, style: style, cursorColor: cursorColor, backgroundCursorColor: Colors.transparent, selectionColor: selectionColor, keyboardType: TextInputType.phone, onSubmitted: this.textSubmitted, unityTouchKeyboard: unityKeyboard, selectionControls: MaterialUtils.materialTextSelectionControls))))); widgets.Add(this.rowWidgets("Email", new EditStateProvider(builder: ((buildContext, controller, node) => new EditableText(controller: controller, focusNode: node, style: style, cursorColor: cursorColor, backgroundCursorColor: Colors.transparent, selectionColor: selectionColor, keyboardType: TextInputType.emailAddress, onSubmitted: this.textSubmitted, unityTouchKeyboard: unityKeyboard, selectionControls: MaterialUtils.materialTextSelectionControls))))); widgets.Add(this.rowWidgets("Url", new EditStateProvider(builder: ((buildContext, controller, node) => new EditableText(controller: controller, focusNode: node, style: style, cursorColor: cursorColor, backgroundCursorColor: Colors.transparent, selectionColor: selectionColor, keyboardType: TextInputType.url, onSubmitted: this.textSubmitted, unityTouchKeyboard: unityKeyboard, selectionControls: MaterialUtils.materialTextSelectionControls))))); return widgets; } public override Widget build(BuildContext context) { List widgets = new List(); widgets.Add(new Text("UIWidgets Touch Keyboard", style: new TextStyle(fontSize: 20, height: 2.0f, color: Colors.black, decoration: TextDecoration.none), textAlign: TextAlign.center)); widgets.AddRange(this.buildInputs(false)); widgets.Add(new Text("Unity Touch Keyboard", style: new TextStyle(fontSize: 20, height: 2.0f, color: Colors.black, decoration: TextDecoration.none), textAlign: TextAlign.center)); widgets.AddRange(this.buildInputs(true)); return new Container( padding: EdgeInsets.all(12), child: new SingleChildScrollView(child: new Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: widgets))); } } public class EditStateProvider : StatefulWidget { public delegate EditableText EditableBuilder(BuildContext context, TextEditingController controller, FocusNode focusNode); public readonly EditableBuilder builder; public EditStateProvider(Key key = null, EditableBuilder builder = null) : base(key) { D.assert(builder != null); this.builder = builder; } public override State createState() { return new _EditStateProviderState(); } } class _EditStateProviderState : State { TextEditingController _controller; FocusNode _focusNode; public override void initState() { base.initState(); this._focusNode = new FocusNode(); this._controller = new TextEditingController(""); } public override void dispose() { this._focusNode.dispose(); base.dispose(); } public override Widget build(BuildContext context) { return this.widget.builder(context, this._controller, this._focusNode); } } }