Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
flutter
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
ExcellentOSS
flutter
Commits
cccbf1f2
Unverified
Commit
cccbf1f2
authored
5 years ago
by
Flutter GitHub Bot
Committed by
GitHub
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Custom onPressed behavior for CloseButton widget (#51925)
parent
cc52a903
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
packages/flutter/lib/src/material/back_button.dart
+16
-2
16 additions, 2 deletions
packages/flutter/lib/src/material/back_button.dart
packages/flutter/test/material/back_button_test.dart
+37
-3
37 additions, 3 deletions
packages/flutter/test/material/back_button_test.dart
with
53 additions
and
5 deletions
packages/flutter/lib/src/material/back_button.dart
+
16
−
2
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:
()
{
Navigator
.
maybePop
(
context
);
if
(
onPressed
!=
null
)
{
onPressed
();
}
else
{
Navigator
.
maybePop
(
context
);
}
},
);
}
...
...
This diff is collapsed.
Click to expand it.
packages/flutter/test/material/back_button_test.dart
+
37
−
3
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
);
});
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment