forked from UnityTech/UIWidgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextInputSample.cs
More file actions
265 lines (228 loc) · 12.3 KB
/
TextInputSample.cs
File metadata and controls
265 lines (228 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System.Collections.Generic;
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;
namespace UIWidgetsSample {
public class TextInputSample : UIWidgetsSamplePanel {
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();
}
}
protected override Widget createWidget() {
return new MaterialApp(
home: new EditableInputTypeWidget()
);
}
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<Widget>(
) {
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<Widget>(
) {
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<Widget> {
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<EditableInputTypeWidget> {
Widget rowWidgets(string title, Widget widget) {
return new Container(
height: 80,
child: new Row(
children: new List<Widget> {
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<Widget> buildInputs(bool unityKeyboard) {
List<Widget> widgets = new List<Widget>();
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, node, style, cursorColor, 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, node, style, cursorColor, Colors.transparent,
selectionColor: selectionColor, maxLines: 4,
onSubmitted: this.textSubmitted, unityTouchKeyboard: unityKeyboard,
selectionControls: MaterialUtils.materialTextSelectionControls)))));
widgets.Add(this.rowWidgets("ObscureText", new EditStateProvider(
builder: ((buildContext, controller, node) =>
new EditableText(controller, node, style, cursorColor, 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, node, style, cursorColor, 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, node, style, cursorColor, 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, node, style, cursorColor, 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, node, style, cursorColor, Colors.transparent,
selectionColor: selectionColor, keyboardType: TextInputType.url,
onSubmitted: this.textSubmitted, unityTouchKeyboard: unityKeyboard,
selectionControls: MaterialUtils.materialTextSelectionControls)))));
return widgets;
}
public override Widget build(BuildContext context) {
List<Widget> widgets = new List<Widget>();
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<EditStateProvider> {
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);
}
}
}