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
ed346222
Commit
ed346222
authored
Jan 22, 2016
by
Adam Barth
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1348 from abarth/focus_blur
Add the ability to lose focus
parents
d081dc67
4e83a5cc
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
73 additions
and
11 deletions
+73
-11
input.dart
packages/flutter/lib/src/material/input.dart
+1
-0
editable.dart
packages/flutter/lib/src/widgets/editable.dart
+1
-0
focus.dart
packages/flutter/lib/src/widgets/focus.dart
+20
-7
focus_test.dart
packages/flutter/test/widget/focus_test.dart
+51
-4
No files found.
packages/flutter/lib/src/material/input.dart
View file @
ed346222
...
...
@@ -90,6 +90,7 @@ class InputState extends ScrollableState<Input> {
}
void
_handleTextSubmitted
()
{
Focus
.
clear
(
context
);
if
(
config
.
onSubmitted
!=
null
)
config
.
onSubmitted
(
_value
);
}
...
...
packages/flutter/lib/src/widgets/editable.dart
View file @
ed346222
...
...
@@ -136,6 +136,7 @@ class EditableString implements KeyboardClient {
}
void
submit
(
SubmitAction
action
)
{
composing
=
const
TextRange
.
empty
();
onSubmitted
();
}
}
...
...
packages/flutter/lib/src/widgets/focus.dart
View file @
ed346222
...
...
@@ -25,7 +25,7 @@ class _FocusScope extends InheritedWidget {
Widget
child
})
:
super
(
key:
key
,
child:
child
);
final
FocusState
focusState
;
final
_
FocusState
focusState
;
final
bool
scopeFocused
;
// These are mutable because we implicitly change them when they're null in
...
...
@@ -144,6 +144,12 @@ class Focus extends StatefulComponent {
}
}
static
void
clear
(
BuildContext
context
)
{
_FocusScope
focusScope
=
context
.
ancestorWidgetOfExactType
(
_FocusScope
);
if
(
focusScope
!=
null
)
focusScope
.
focusState
.
_clearFocusedWidget
();
}
/// Focuses a particular focus scope, identified by its GlobalKey. The widget
/// must be in the widget tree.
///
...
...
@@ -157,10 +163,10 @@ class Focus extends StatefulComponent {
focusScope
.
focusState
.
_setFocusedScope
(
key
);
}
FocusState
createState
()
=>
new
FocusState
();
_FocusState
createState
()
=>
new
_
FocusState
();
}
class
FocusState
extends
State
<
Focus
>
{
class
_
FocusState
extends
State
<
Focus
>
{
GlobalKey
_focusedWidget
;
// when null, the first component to ask if it's focused will get the focus
GlobalKey
_currentlyRegisteredWidgetRemovalListenerKey
;
...
...
@@ -181,12 +187,19 @@ class FocusState extends State<Focus> {
}
}
void
_clearFocusedWidget
()
{
if
(
_focusedWidget
!=
null
)
{
_updateWidgetRemovalListener
(
null
);
setState
(()
{
_focusedWidget
=
null
;
});
}
}
void
_handleWidgetRemoved
(
GlobalKey
key
)
{
assert
(
key
!=
null
);
assert
(
_focusedWidget
==
key
);
_updateWidgetRemovalListener
(
null
);
setState
(()
{
_focusedWidget
=
null
;
});
_clearFocusedWidget
();
}
void
_updateWidgetRemovalListener
(
GlobalKey
key
)
{
...
...
packages/flutter/test/widget/focus_test.dart
View file @
ed346222
...
...
@@ -7,11 +7,19 @@ import 'package:flutter/widgets.dart';
import
'package:test/test.dart'
;
class
TestFocusable
extends
StatelessComponent
{
TestFocusable
(
this
.
no
,
this
.
yes
,
GlobalKey
key
)
:
super
(
key:
key
);
TestFocusable
({
GlobalKey
key
,
this
.
no
,
this
.
yes
,
this
.
autofocus
:
true
})
:
super
(
key:
key
);
final
String
no
;
final
String
yes
;
final
bool
autofocus
;
Widget
build
(
BuildContext
context
)
{
bool
focused
=
Focus
.
at
(
context
,
autofocus:
true
);
bool
focused
=
Focus
.
at
(
context
,
autofocus:
autofocus
);
return
new
GestureDetector
(
onTap:
()
{
Focus
.
moveTo
(
key
);
},
child:
new
Text
(
focused
?
yes
:
no
)
...
...
@@ -29,8 +37,16 @@ void main() {
child:
new
Column
(
children:
<
Widget
>[
// reverse these when you fix https://github.com/flutter/engine/issues/1495
new
TestFocusable
(
'b'
,
'B FOCUSED'
,
keyB
),
new
TestFocusable
(
'a'
,
'A FOCUSED'
,
keyA
),
new
TestFocusable
(
key:
keyB
,
no:
'b'
,
yes:
'B FOCUSED'
),
new
TestFocusable
(
key:
keyA
,
no:
'a'
,
yes:
'A FOCUSED'
),
]
)
)
...
...
@@ -65,4 +81,35 @@ void main() {
expect
(
tester
.
findText
(
'B FOCUSED'
),
isNull
);
});
});
test
(
'Can blur'
,
()
{
testWidgets
((
WidgetTester
tester
)
{
GlobalKey
keyA
=
new
GlobalKey
();
tester
.
pumpWidget
(
new
Focus
(
child:
new
TestFocusable
(
key:
keyA
,
no:
'a'
,
yes:
'A FOCUSED'
,
autofocus:
false
)
)
);
expect
(
tester
.
findText
(
'a'
),
isNotNull
);
expect
(
tester
.
findText
(
'A FOCUSED'
),
isNull
);
Focus
.
moveTo
(
keyA
);
tester
.
pump
();
expect
(
tester
.
findText
(
'a'
),
isNull
);
expect
(
tester
.
findText
(
'A FOCUSED'
),
isNotNull
);
Focus
.
clear
(
keyA
.
currentContext
);
tester
.
pump
();
expect
(
tester
.
findText
(
'a'
),
isNotNull
);
expect
(
tester
.
findText
(
'A FOCUSED'
),
isNull
);
});
});
}
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