Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
b6cca392
Unverified
Commit
b6cca392
authored
Mar 14, 2018
by
Hans Muller
Committed by
GitHub
Mar 14, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated the gallery text fields demo (#15362)
parent
c345c1bb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
179 additions
and
95 deletions
+179
-95
text_form_field_demo.dart
...utter_gallery/lib/demo/material/text_form_field_demo.dart
+154
-91
text_form_field_demo_test.dart
...gallery/test/demo/material/text_form_field_demo_test.dart
+19
-4
text_form_field.dart
packages/flutter/lib/src/material/text_form_field.dart
+6
-0
No files found.
examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart
View file @
b6cca392
...
@@ -23,6 +23,60 @@ class PersonData {
...
@@ -23,6 +23,60 @@ class PersonData {
String
password
=
''
;
String
password
=
''
;
}
}
class
PasswordField
extends
StatefulWidget
{
const
PasswordField
({
this
.
fieldKey
,
this
.
hintText
,
this
.
labelText
,
this
.
helperText
,
this
.
onSaved
,
this
.
validator
,
this
.
onFieldSubmitted
,
});
final
Key
fieldKey
;
final
String
hintText
;
final
String
labelText
;
final
String
helperText
;
final
FormFieldSetter
<
String
>
onSaved
;
final
FormFieldValidator
<
String
>
validator
;
final
ValueChanged
<
String
>
onFieldSubmitted
;
@override
_PasswordFieldState
createState
()
=>
new
_PasswordFieldState
();
}
class
_PasswordFieldState
extends
State
<
PasswordField
>
{
bool
_obscureText
=
true
;
@override
Widget
build
(
BuildContext
context
)
{
return
new
TextFormField
(
key:
widget
.
fieldKey
,
obscureText:
_obscureText
,
maxLength:
8
,
onSaved:
widget
.
onSaved
,
validator:
widget
.
validator
,
onFieldSubmitted:
widget
.
onFieldSubmitted
,
decoration:
new
InputDecoration
(
border:
const
UnderlineInputBorder
(),
filled:
true
,
hintText:
widget
.
hintText
,
labelText:
widget
.
labelText
,
helperText:
widget
.
helperText
,
suffixIcon:
new
GestureDetector
(
onTap:
()
{
setState
(()
{
_obscureText
=
!
_obscureText
;
});
},
child:
new
Icon
(
_obscureText
?
Icons
.
visibility
:
Icons
.
visibility_off
),
),
),
);
}
}
class
TextFormFieldDemoState
extends
State
<
TextFormFieldDemo
>
{
class
TextFormFieldDemoState
extends
State
<
TextFormFieldDemo
>
{
final
GlobalKey
<
ScaffoldState
>
_scaffoldKey
=
new
GlobalKey
<
ScaffoldState
>();
final
GlobalKey
<
ScaffoldState
>
_scaffoldKey
=
new
GlobalKey
<
ScaffoldState
>();
...
@@ -36,6 +90,7 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
...
@@ -36,6 +90,7 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
bool
_autovalidate
=
false
;
bool
_autovalidate
=
false
;
bool
_formWasEdited
=
false
;
bool
_formWasEdited
=
false
;
final
GlobalKey
<
FormState
>
_formKey
=
new
GlobalKey
<
FormState
>();
final
GlobalKey
<
FormState
>
_formKey
=
new
GlobalKey
<
FormState
>();
final
GlobalKey
<
FormFieldState
<
String
>>
_passwordFieldKey
=
new
GlobalKey
<
FormFieldState
<
String
>>();
final
GlobalKey
<
FormFieldState
<
String
>>
_passwordFieldKey
=
new
GlobalKey
<
FormFieldState
<
String
>>();
final
_UsNumberTextInputFormatter
_phoneNumberFormatter
=
new
_UsNumberTextInputFormatter
();
final
_UsNumberTextInputFormatter
_phoneNumberFormatter
=
new
_UsNumberTextInputFormatter
();
...
@@ -64,7 +119,7 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
...
@@ -64,7 +119,7 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
_formWasEdited
=
true
;
_formWasEdited
=
true
;
final
RegExp
phoneExp
=
new
RegExp
(
r'^\(\d\d\d\) \d\d\d\-\d\d\d\d$'
);
final
RegExp
phoneExp
=
new
RegExp
(
r'^\(\d\d\d\) \d\d\d\-\d\d\d\d$'
);
if
(!
phoneExp
.
hasMatch
(
value
))
if
(!
phoneExp
.
hasMatch
(
value
))
return
'(###) ###-#### -
Please enter a valid
US phone number.'
;
return
'(###) ###-#### -
Enter a
US phone number.'
;
return
null
;
return
null
;
}
}
...
@@ -72,9 +127,9 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
...
@@ -72,9 +127,9 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
_formWasEdited
=
true
;
_formWasEdited
=
true
;
final
FormFieldState
<
String
>
passwordField
=
_passwordFieldKey
.
currentState
;
final
FormFieldState
<
String
>
passwordField
=
_passwordFieldKey
.
currentState
;
if
(
passwordField
.
value
==
null
||
passwordField
.
value
.
isEmpty
)
if
(
passwordField
.
value
==
null
||
passwordField
.
value
.
isEmpty
)
return
'Please
choose
a password.'
;
return
'Please
enter
a password.'
;
if
(
passwordField
.
value
!=
value
)
if
(
passwordField
.
value
!=
value
)
return
'
P
asswords don
\'
t match'
;
return
'
The p
asswords don
\'
t match'
;
return
null
;
return
null
;
}
}
...
@@ -118,103 +173,111 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
...
@@ -118,103 +173,111 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
key:
_formKey
,
key:
_formKey
,
autovalidate:
_autovalidate
,
autovalidate:
_autovalidate
,
onWillPop:
_warnUserAboutInvalidData
,
onWillPop:
_warnUserAboutInvalidData
,
child:
new
List
View
(
child:
new
SingleChildScroll
View
(
padding:
const
EdgeInsets
.
symmetric
(
horizontal:
16.0
),
padding:
const
EdgeInsets
.
symmetric
(
horizontal:
16.0
),
children:
<
Widget
>[
child:
new
Column
(
new
TextFormField
(
crossAxisAlignment:
CrossAxisAlignment
.
stretch
,
decoration:
const
InputDecoration
(
children:
<
Widget
>[
icon:
const
Icon
(
Icons
.
person
),
const
SizedBox
(
height:
24.0
),
hintText:
'What do people call you?'
,
new
TextFormField
(
labelText:
'Name *'
,
decoration:
const
InputDecoration
(
border:
const
UnderlineInputBorder
(),
filled:
true
,
icon:
const
Icon
(
Icons
.
person
),
hintText:
'What do people call you?'
,
labelText:
'Name *'
,
),
onSaved:
(
String
value
)
{
person
.
name
=
value
;
},
validator:
_validateName
,
),
const
SizedBox
(
height:
24.0
),
new
TextFormField
(
decoration:
const
InputDecoration
(
border:
const
UnderlineInputBorder
(),
filled:
true
,
icon:
const
Icon
(
Icons
.
phone
),
hintText:
'Where can we reach you?'
,
labelText:
'Phone Number *'
,
prefixText:
'+1'
,
),
keyboardType:
TextInputType
.
phone
,
onSaved:
(
String
value
)
{
person
.
phoneNumber
=
value
;
},
validator:
_validatePhoneNumber
,
// TextInputFormatters are applied in sequence.
inputFormatters:
<
TextInputFormatter
>
[
WhitelistingTextInputFormatter
.
digitsOnly
,
// Fit the validating format.
_phoneNumberFormatter
,
],
),
),
onSaved:
(
String
value
)
{
person
.
name
=
value
;
},
const
SizedBox
(
height:
24.0
),
validator:
_validateName
,
new
TextFormField
(
),
decoration:
const
InputDecoration
(
new
TextFormField
(
border:
const
UnderlineInputBorder
(),
decoration:
const
InputDecoration
(
filled:
true
,
icon:
const
Icon
(
Icons
.
phone
),
icon:
const
Icon
(
Icons
.
email
),
hintText:
'Where can we reach you?'
,
hintText:
'Your email address'
,
labelText:
'Phone Number *'
,
labelText:
'E-mail'
,
prefixText:
'+1'
),
keyboardType:
TextInputType
.
emailAddress
,
onSaved:
(
String
value
)
{
person
.
email
=
value
;
},
),
),
keyboardType:
TextInputType
.
phone
,
const
SizedBox
(
height:
24.0
),
onSaved:
(
String
value
)
{
person
.
phoneNumber
=
value
;
},
new
TextFormField
(
validator:
_validatePhoneNumber
,
decoration:
const
InputDecoration
(
// TextInputFormatters are applied in sequence.
border:
const
OutlineInputBorder
(),
inputFormatters:
<
TextInputFormatter
>
[
hintText:
'Tell us about yourself'
,
WhitelistingTextInputFormatter
.
digitsOnly
,
helperText:
'Keep it short, this is just a demo.'
,
// Fit the validating format.
labelText:
'Life story'
,
_phoneNumberFormatter
,
),
],
maxLines:
3
,
),
new
TextFormField
(
decoration:
const
InputDecoration
(
icon:
const
Icon
(
Icons
.
email
),
hintText:
'Your email address'
,
labelText:
'E-mail'
,
),
),
keyboardType:
TextInputType
.
emailAddress
,
const
SizedBox
(
height:
24.0
),
onSaved:
(
String
value
)
{
person
.
email
=
value
;
},
new
TextFormField
(
),
keyboardType:
TextInputType
.
number
,
new
TextFormField
(
decoration:
const
InputDecoration
(
decoration:
const
InputDecoration
(
border:
const
OutlineInputBorder
(),
hintText:
'Tell us about yourself'
,
labelText:
'Salary'
,
helperText:
'Keep it short, this is just a demo'
,
prefixText:
'
\$
'
,
labelText:
'Life story'
,
suffixText:
'USD'
,
suffixStyle:
const
TextStyle
(
color:
Colors
.
green
)
),
maxLines:
1
,
),
),
maxLines:
3
,
const
SizedBox
(
height:
24.0
),
),
new
PasswordField
(
new
TextFormField
(
fieldKey:
_passwordFieldKey
,
keyboardType:
TextInputType
.
number
,
helperText:
'No more than 8 characters.'
,
decoration:
const
InputDecoration
(
labelText:
'Password *'
,
labelText:
'Salary'
,
onSaved:
(
String
value
)
{
person
.
password
=
value
;
},
prefixText:
'
\$
'
,
suffixText:
'USD'
,
suffixStyle:
const
TextStyle
(
color:
Colors
.
green
)
),
),
maxLines:
1
,
const
SizedBox
(
height:
24.0
),
),
new
TextFormField
(
new
Row
(
decoration:
const
InputDecoration
(
crossAxisAlignment:
CrossAxisAlignment
.
start
,
border:
const
UnderlineInputBorder
(),
children:
<
Widget
>[
filled:
true
,
new
Expanded
(
labelText:
'Re-type password'
,
child:
new
TextFormField
(
key:
_passwordFieldKey
,
decoration:
const
InputDecoration
(
hintText:
'How do you log in?'
,
labelText:
'New Password *'
,
),
obscureText:
true
,
onSaved:
(
String
value
)
{
person
.
password
=
value
;
},
),
),
),
const
SizedBox
(
width:
16.0
),
maxLength:
8
,
new
Expanded
(
onFieldSubmitted:
(
String
value
)
{
person
.
password
=
value
;
},
child:
new
TextFormField
(
obscureText:
true
,
decoration:
const
InputDecoration
(
validator:
_validatePassword
,
hintText:
'How do you log in?'
,
),
labelText:
'Re-type Password *'
,
const
SizedBox
(
height:
24.0
),
),
new
Center
(
obscureText:
true
,
child:
new
RaisedButton
(
onFieldSubmitted:
(
String
value
)
{
_handleSubmitted
();
},
child:
const
Text
(
'SUBMIT'
),
validator:
_validatePassword
,
onPressed:
_handleSubmitted
,
),
),
),
],
),
new
Container
(
padding:
const
EdgeInsets
.
all
(
20.0
),
alignment:
Alignment
.
center
,
child:
new
RaisedButton
(
child:
const
Text
(
'SUBMIT'
),
onPressed:
_handleSubmitted
,
),
),
),
const
SizedBox
(
height:
24.0
),
new
Container
(
new
Text
(
padding:
const
EdgeInsets
.
only
(
top:
20.0
),
'* indicates required field'
,
child:
new
Text
(
'* indicates required field'
,
style:
Theme
.
of
(
context
).
textTheme
.
caption
),
style:
Theme
.
of
(
context
).
textTheme
.
caption
),
),
],
const
SizedBox
(
height:
24.0
),
],
),
),
),
),
),
),
),
...
...
examples/flutter_gallery/test/demo/material/text_form_field_demo_test.dart
View file @
b6cca392
...
@@ -16,21 +16,36 @@ void main() {
...
@@ -16,21 +16,36 @@ void main() {
final
Finder
nameField
=
find
.
widgetWithText
(
TextFormField
,
'Name *'
);
final
Finder
nameField
=
find
.
widgetWithText
(
TextFormField
,
'Name *'
);
expect
(
nameField
,
findsOneWidget
);
expect
(
nameField
,
findsOneWidget
);
final
Finder
passwordField
=
find
.
widgetWithText
(
TextFormField
,
'Password *'
);
expect
(
passwordField
,
findsOneWidget
);
await
tester
.
enterText
(
nameField
,
''
);
await
tester
.
enterText
(
nameField
,
''
);
// The submit button isn't initially visible. Drag it into view so that
// it will see the tap.
await
tester
.
drag
(
nameField
,
const
Offset
(
0.0
,
-
1200.0
));
await
tester
.
pumpAndSettle
();
await
tester
.
tap
(
submitButton
);
await
tester
.
tap
(
submitButton
);
await
tester
.
pump
();
await
tester
.
pumpAndSettle
();
// Now drag the password field (the submit button will be obscured by
// the snackbar) and expose the name field again.
await
tester
.
drag
(
passwordField
,
const
Offset
(
0.0
,
1200.0
));
await
tester
.
pumpAndSettle
();
expect
(
find
.
text
(
'Name is required.'
),
findsOneWidget
);
expect
(
find
.
text
(
'Name is required.'
),
findsOneWidget
);
expect
(
find
.
text
(
'Please enter only alphabetical characters.'
),
findsNothing
);
expect
(
find
.
text
(
'Please enter only alphabetical characters.'
),
findsNothing
);
await
tester
.
enterText
(
nameField
,
'#'
);
await
tester
.
enterText
(
nameField
,
'#'
);
// Make the submit button visible again (by dragging the name field), so
// it will see the tap.
await
tester
.
drag
(
nameField
,
const
Offset
(
0.0
,
-
1200.0
));
await
tester
.
tap
(
submitButton
);
await
tester
.
tap
(
submitButton
);
await
tester
.
pump
();
await
tester
.
pump
AndSettle
();
expect
(
find
.
text
(
'Name is required.'
),
findsNothing
);
expect
(
find
.
text
(
'Name is required.'
),
findsNothing
);
expect
(
find
.
text
(
'Please enter only alphabetical characters.'
),
findsOneWidget
);
expect
(
find
.
text
(
'Please enter only alphabetical characters.'
),
findsOneWidget
);
await
tester
.
enterText
(
nameField
,
'Jane Doe'
);
await
tester
.
enterText
(
nameField
,
'Jane Doe'
);
await
tester
.
tap
(
submitButton
);
await
tester
.
tap
(
submitButton
);
await
tester
.
pump
();
await
tester
.
pump
AndSettle
();
expect
(
find
.
text
(
'Name is required.'
),
findsNothing
);
expect
(
find
.
text
(
'Name is required.'
),
findsNothing
);
expect
(
find
.
text
(
'Please enter only alphabetical characters.'
),
findsNothing
);
expect
(
find
.
text
(
'Please enter only alphabetical characters.'
),
findsNothing
);
});
});
...
...
packages/flutter/lib/src/material/text_form_field.dart
View file @
b6cca392
...
@@ -60,7 +60,9 @@ class TextFormField extends FormField<String> {
...
@@ -60,7 +60,9 @@ class TextFormField extends FormField<String> {
bool
autofocus:
false
,
bool
autofocus:
false
,
bool
obscureText:
false
,
bool
obscureText:
false
,
bool
autocorrect:
true
,
bool
autocorrect:
true
,
bool
maxLengthEnforced:
true
,
int
maxLines:
1
,
int
maxLines:
1
,
int
maxLength
,
ValueChanged
<
String
>
onFieldSubmitted
,
ValueChanged
<
String
>
onFieldSubmitted
,
FormFieldSetter
<
String
>
onSaved
,
FormFieldSetter
<
String
>
onSaved
,
FormFieldValidator
<
String
>
validator
,
FormFieldValidator
<
String
>
validator
,
...
@@ -71,7 +73,9 @@ class TextFormField extends FormField<String> {
...
@@ -71,7 +73,9 @@ class TextFormField extends FormField<String> {
assert
(
autofocus
!=
null
),
assert
(
autofocus
!=
null
),
assert
(
obscureText
!=
null
),
assert
(
obscureText
!=
null
),
assert
(
autocorrect
!=
null
),
assert
(
autocorrect
!=
null
),
assert
(
maxLengthEnforced
!=
null
),
assert
(
maxLines
==
null
||
maxLines
>
0
),
assert
(
maxLines
==
null
||
maxLines
>
0
),
assert
(
maxLength
==
null
||
maxLength
>
0
),
super
(
super
(
key:
key
,
key:
key
,
initialValue:
controller
!=
null
?
controller
.
text
:
(
initialValue
??
''
),
initialValue:
controller
!=
null
?
controller
.
text
:
(
initialValue
??
''
),
...
@@ -91,7 +95,9 @@ class TextFormField extends FormField<String> {
...
@@ -91,7 +95,9 @@ class TextFormField extends FormField<String> {
autofocus:
autofocus
,
autofocus:
autofocus
,
obscureText:
obscureText
,
obscureText:
obscureText
,
autocorrect:
autocorrect
,
autocorrect:
autocorrect
,
maxLengthEnforced:
maxLengthEnforced
,
maxLines:
maxLines
,
maxLines:
maxLines
,
maxLength:
maxLength
,
onChanged:
field
.
onChanged
,
onChanged:
field
.
onChanged
,
onSubmitted:
onFieldSubmitted
,
onSubmitted:
onFieldSubmitted
,
inputFormatters:
inputFormatters
,
inputFormatters:
inputFormatters
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment