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
cccbf1f2
Unverified
Commit
cccbf1f2
authored
Mar 05, 2020
by
Flutter GitHub Bot
Committed by
GitHub
Mar 05, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Custom onPressed behavior for CloseButton widget (#51925)
parent
cc52a903
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
5 deletions
+53
-5
back_button.dart
packages/flutter/lib/src/material/back_button.dart
+16
-2
back_button_test.dart
packages/flutter/test/material/back_button_test.dart
+37
-3
No files found.
packages/flutter/lib/src/material/back_button.dart
View file @
cccbf1f2
...
...
@@ -130,7 +130,7 @@ class BackButton extends StatelessWidget {
/// * [IconButton], to create other material design icon buttons.
class
CloseButton
extends
StatelessWidget
{
/// Creates a Material Design close button.
const
CloseButton
({
Key
key
,
this
.
color
})
:
super
(
key:
key
);
const
CloseButton
({
Key
key
,
this
.
color
,
this
.
onPressed
})
:
super
(
key:
key
);
/// The color to use for the icon.
///
...
...
@@ -138,6 +138,16 @@ class CloseButton extends StatelessWidget {
/// which usually matches the ambient [Theme]'s [ThemeData.iconTheme].
final
Color
color
;
/// An override callback to perform instead of the default behavior which is
/// to pop the [Navigator].
///
/// It can, for instance, be used to pop the platform's navigation stack
/// via [SytemNavigator] instead of Flutter's [Navigator] in add-to-app
/// situations.
///
/// Defaults to null.
final
VoidCallback
onPressed
;
@override
Widget
build
(
BuildContext
context
)
{
assert
(
debugCheckHasMaterialLocalizations
(
context
));
...
...
@@ -146,7 +156,11 @@ class CloseButton extends StatelessWidget {
color:
color
,
tooltip:
MaterialLocalizations
.
of
(
context
).
closeButtonTooltip
,
onPressed:
()
{
if
(
onPressed
!=
null
)
{
onPressed
();
}
else
{
Navigator
.
maybePop
(
context
);
}
},
);
}
...
...
packages/flutter/test/material/back_button_test.dart
View file @
cccbf1f2
...
...
@@ -35,7 +35,7 @@ void main() {
});
testWidgets
(
'BackButton onPressed overrides default pop behavior'
,
(
WidgetTester
tester
)
async
{
bool
backPress
ed
=
false
;
bool
customCallbackWasCall
ed
=
false
;
await
tester
.
pumpWidget
(
MaterialApp
(
home:
const
Material
(
child:
Text
(
'Home'
)),
...
...
@@ -43,7 +43,7 @@ void main() {
'/next'
:
(
BuildContext
context
)
{
return
Material
(
child:
Center
(
child:
BackButton
(
onPressed:
()
=>
backPress
ed
=
true
),
child:
BackButton
(
onPressed:
()
=>
customCallbackWasCall
ed
=
true
),
),
);
},
...
...
@@ -55,6 +55,8 @@ void main() {
await
tester
.
pumpAndSettle
();
expect
(
find
.
text
(
'Home'
),
findsNothing
);
// Start off on the second page.
expect
(
customCallbackWasCalled
,
false
);
// customCallbackWasCalled should still be false.
await
tester
.
tap
(
find
.
byType
(
BackButton
));
await
tester
.
pumpAndSettle
();
...
...
@@ -62,7 +64,7 @@ void main() {
// We're still on the second page.
expect
(
find
.
text
(
'Home'
),
findsNothing
);
// But the custom callback is called.
expect
(
backPress
ed
,
true
);
expect
(
customCallbackWasCall
ed
,
true
);
});
testWidgets
(
'BackButton icon'
,
(
WidgetTester
tester
)
async
{
...
...
@@ -180,4 +182,36 @@ void main() {
));
expect
(
iconText
.
text
.
style
.
color
,
Colors
.
red
);
});
testWidgets
(
'CloseButton onPressed overrides default pop behavior'
,
(
WidgetTester
tester
)
async
{
bool
customCallbackWasCalled
=
false
;
await
tester
.
pumpWidget
(
MaterialApp
(
home:
const
Material
(
child:
Text
(
'Home'
)),
routes:
<
String
,
WidgetBuilder
>{
'/next'
:
(
BuildContext
context
)
{
return
Material
(
child:
Center
(
child:
CloseButton
(
onPressed:
()
=>
customCallbackWasCalled
=
true
),
),
);
},
},
),
);
tester
.
state
<
NavigatorState
>(
find
.
byType
(
Navigator
)).
pushNamed
(
'/next'
);
await
tester
.
pumpAndSettle
();
expect
(
find
.
text
(
'Home'
),
findsNothing
);
// Start off on the second page.
expect
(
customCallbackWasCalled
,
false
);
// customCallbackWasCalled should still be false.
await
tester
.
tap
(
find
.
byType
(
CloseButton
));
await
tester
.
pumpAndSettle
();
// We're still on the second page.
expect
(
find
.
text
(
'Home'
),
findsNothing
);
// The custom callback is called, setting customCallbackWasCalled to true.
expect
(
customCallbackWasCalled
,
true
);
});
}
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