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 < string > {
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 < string > onSaved = null ,
34
+ FormFieldValidator < string > 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 < string > 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 < string > {
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 : 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
+ }
0 commit comments