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
fc870971
Unverified
Commit
fc870971
authored
Mar 22, 2018
by
Ian Hickson
Committed by
GitHub
Mar 22, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Form.onChanged, and rename FormState.onChanged (#15405)
parent
aba0379d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
12 deletions
+35
-12
expansion_panels_demo.dart
...tter_gallery/lib/demo/material/expansion_panels_demo.dart
+4
-4
text_form_field.dart
packages/flutter/lib/src/material/text_form_field.dart
+2
-2
form.dart
packages/flutter/lib/src/widgets/form.dart
+29
-6
No files found.
examples/flutter_gallery/lib/demo/material/expansion_panels_demo.dart
View file @
fc870971
...
@@ -254,7 +254,7 @@ class _ExpansionPanelsDemoState extends State<ExpansionPanelsDemo> {
...
@@ -254,7 +254,7 @@ class _ExpansionPanelsDemoState extends State<ExpansionPanelsDemo> {
new
Radio
<
_Location
>(
new
Radio
<
_Location
>(
value:
_Location
.
Bahamas
,
value:
_Location
.
Bahamas
,
groupValue:
field
.
value
,
groupValue:
field
.
value
,
onChanged:
field
.
onChanged
,
onChanged:
field
.
didChange
,
),
),
const
Text
(
'Bahamas'
)
const
Text
(
'Bahamas'
)
]
]
...
@@ -265,7 +265,7 @@ class _ExpansionPanelsDemoState extends State<ExpansionPanelsDemo> {
...
@@ -265,7 +265,7 @@ class _ExpansionPanelsDemoState extends State<ExpansionPanelsDemo> {
new
Radio
<
_Location
>(
new
Radio
<
_Location
>(
value:
_Location
.
Barbados
,
value:
_Location
.
Barbados
,
groupValue:
field
.
value
,
groupValue:
field
.
value
,
onChanged:
field
.
onChanged
,
onChanged:
field
.
didChange
,
),
),
const
Text
(
'Barbados'
)
const
Text
(
'Barbados'
)
]
]
...
@@ -276,7 +276,7 @@ class _ExpansionPanelsDemoState extends State<ExpansionPanelsDemo> {
...
@@ -276,7 +276,7 @@ class _ExpansionPanelsDemoState extends State<ExpansionPanelsDemo> {
new
Radio
<
_Location
>(
new
Radio
<
_Location
>(
value:
_Location
.
Bermuda
,
value:
_Location
.
Bermuda
,
groupValue:
field
.
value
,
groupValue:
field
.
value
,
onChanged:
field
.
onChanged
,
onChanged:
field
.
didChange
,
),
),
const
Text
(
'Bermuda'
)
const
Text
(
'Bermuda'
)
]
]
...
@@ -320,7 +320,7 @@ class _ExpansionPanelsDemoState extends State<ExpansionPanelsDemo> {
...
@@ -320,7 +320,7 @@ class _ExpansionPanelsDemoState extends State<ExpansionPanelsDemo> {
activeColor:
Colors
.
orange
[
100
+
(
field
.
value
*
5.0
).
round
()],
activeColor:
Colors
.
orange
[
100
+
(
field
.
value
*
5.0
).
round
()],
label:
'
${field.value.round()}
'
,
label:
'
${field.value.round()}
'
,
value:
field
.
value
,
value:
field
.
value
,
onChanged:
field
.
onChanged
,
onChanged:
field
.
didChange
,
);
);
},
},
),
),
...
...
packages/flutter/lib/src/material/text_form_field.dart
View file @
fc870971
...
@@ -98,7 +98,7 @@ class TextFormField extends FormField<String> {
...
@@ -98,7 +98,7 @@ class TextFormField extends FormField<String> {
maxLengthEnforced:
maxLengthEnforced
,
maxLengthEnforced:
maxLengthEnforced
,
maxLines:
maxLines
,
maxLines:
maxLines
,
maxLength:
maxLength
,
maxLength:
maxLength
,
onChanged:
field
.
onChanged
,
onChanged:
field
.
didChange
,
onSubmitted:
onFieldSubmitted
,
onSubmitted:
onFieldSubmitted
,
inputFormatters:
inputFormatters
,
inputFormatters:
inputFormatters
,
);
);
...
@@ -173,6 +173,6 @@ class _TextFormFieldState extends FormFieldState<String> {
...
@@ -173,6 +173,6 @@ class _TextFormFieldState extends FormFieldState<String> {
// example, the reset() method. In such cases, the FormField value will
// example, the reset() method. In such cases, the FormField value will
// already have been set.
// already have been set.
if
(
_effectiveController
.
text
!=
value
)
if
(
_effectiveController
.
text
!=
value
)
onChanged
(
_effectiveController
.
text
);
didChange
(
_effectiveController
.
text
);
}
}
}
}
packages/flutter/lib/src/widgets/form.dart
View file @
fc870971
...
@@ -26,6 +26,7 @@ class Form extends StatefulWidget {
...
@@ -26,6 +26,7 @@ class Form extends StatefulWidget {
@required
this
.
child
,
@required
this
.
child
,
this
.
autovalidate
:
false
,
this
.
autovalidate
:
false
,
this
.
onWillPop
,
this
.
onWillPop
,
this
.
onChanged
,
})
:
assert
(
child
!=
null
),
})
:
assert
(
child
!=
null
),
super
(
key:
key
);
super
(
key:
key
);
...
@@ -66,6 +67,12 @@ class Form extends StatefulWidget {
...
@@ -66,6 +67,12 @@ class Form extends StatefulWidget {
/// back button.
/// back button.
final
WillPopCallback
onWillPop
;
final
WillPopCallback
onWillPop
;
/// Called when one of the form fields changes.
///
/// In addition to this callback being invoked, all the form fields themselves
/// will rebuild.
final
VoidCallback
onChanged
;
@override
@override
FormState
createState
()
=>
new
FormState
();
FormState
createState
()
=>
new
FormState
();
}
}
...
@@ -83,6 +90,12 @@ class FormState extends State<Form> {
...
@@ -83,6 +90,12 @@ class FormState extends State<Form> {
// Called when a form field has changed. This will cause all form fields
// Called when a form field has changed. This will cause all form fields
// to rebuild, useful if form fields have interdependencies.
// to rebuild, useful if form fields have interdependencies.
void
_fieldDidChange
()
{
void
_fieldDidChange
()
{
if
(
widget
.
onChanged
!=
null
)
widget
.
onChanged
();
_forceRebuild
();
}
void
_forceRebuild
()
{
setState
(()
{
setState
(()
{
++
_generation
;
++
_generation
;
});
});
...
@@ -117,7 +130,12 @@ class FormState extends State<Form> {
...
@@ -117,7 +130,12 @@ class FormState extends State<Form> {
}
}
/// Resets every [FormField] that is a descendant of this [Form] back to its
/// Resets every [FormField] that is a descendant of this [Form] back to its
/// initialState.
/// [FormField.initialState].
///
/// The [Form.onChanged] callback will be called.
///
/// If the form's [Form.autovalidate] property is true, the fields will all be
/// revalidated after being reset.
void
reset
()
{
void
reset
()
{
for
(
FormFieldState
<
dynamic
>
field
in
_fields
)
for
(
FormFieldState
<
dynamic
>
field
in
_fields
)
field
.
reset
();
field
.
reset
();
...
@@ -126,8 +144,10 @@ class FormState extends State<Form> {
...
@@ -126,8 +144,10 @@ class FormState extends State<Form> {
/// Validates every [FormField] that is a descendant of this [Form], and
/// Validates every [FormField] that is a descendant of this [Form], and
/// returns true if there are no errors.
/// returns true if there are no errors.
///
/// The form will rebuild to report the results.
bool
validate
()
{
bool
validate
()
{
_f
ieldDidChange
();
_f
orceRebuild
();
return
_validate
();
return
_validate
();
}
}
...
@@ -213,7 +233,7 @@ class FormField<T> extends StatefulWidget {
...
@@ -213,7 +233,7 @@ class FormField<T> extends StatefulWidget {
super
(
key:
key
);
super
(
key:
key
);
/// An optional method to call with the final value when the form is saved via
/// An optional method to call with the final value when the form is saved via
///
Form.save()
.
///
[FormState.save]
.
final
FormFieldSetter
<
T
>
onSaved
;
final
FormFieldSetter
<
T
>
onSaved
;
/// An optional method that validates an input. Returns an error string to
/// An optional method that validates an input. Returns an error string to
...
@@ -289,8 +309,11 @@ class FormFieldState<T> extends State<FormField<T>> {
...
@@ -289,8 +309,11 @@ class FormFieldState<T> extends State<FormField<T>> {
}
}
/// Updates this field's state to the new value. Useful for responding to
/// Updates this field's state to the new value. Useful for responding to
/// child widget changes, e.g. [Slider]'s onChanged argument.
/// child widget changes, e.g. [Slider]'s [Slider.onChanged] argument.
void
onChanged
(
T
value
)
{
///
/// Triggers the [Form.onChanged] callback and, if the [Form.autovalidate]
/// field is set, revalidates all the fields of the form.
void
didChange
(
T
value
)
{
setState
(()
{
setState
(()
{
_value
=
value
;
_value
=
value
;
});
});
...
@@ -302,7 +325,7 @@ class FormFieldState<T> extends State<FormField<T>> {
...
@@ -302,7 +325,7 @@ class FormFieldState<T> extends State<FormField<T>> {
/// This method should be only be called by subclasses that need to update
/// This method should be only be called by subclasses that need to update
/// the form field value due to state changes identified during the widget
/// the form field value due to state changes identified during the widget
/// build phase, when calling `setState` is prohibited. In all other cases,
/// build phase, when calling `setState` is prohibited. In all other cases,
/// the value should be set by a call to [
onChanged
], which ensures that
/// the value should be set by a call to [
didChange
], which ensures that
/// `setState` is called.
/// `setState` is called.
@protected
@protected
void
setValue
(
T
value
)
{
void
setValue
(
T
value
)
{
...
...
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