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
7cec142f
Unverified
Commit
7cec142f
authored
Apr 06, 2021
by
Douglas Iacovelli
Committed by
GitHub
Apr 06, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add back button listener widget (#79642)
parent
4bf26b68
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
375 additions
and
1 deletion
+375
-1
router.dart
packages/flutter/lib/src/widgets/router.dart
+68
-0
router_test.dart
packages/flutter/test/widgets/router_test.dart
+307
-1
No files found.
packages/flutter/lib/src/widgets/router.dart
View file @
7cec142f
...
...
@@ -998,6 +998,74 @@ class ChildBackButtonDispatcher extends BackButtonDispatcher {
}
}
/// A convenience widget that registers a callback for when the back button is pressed.
///
/// In order to use this widget, there must be an ancestor [Router] widget in the tree
/// that has a [RootBackButtonDispatcher]. e.g. The [Router] widget created by the
/// [MaterialApp.router] has a built-in [RootBackButtonDispatcher] by default.
///
/// It only applies to platforms that accept back button clicks, such as Android.
///
/// It can be useful for scenarios, in which you create a different state in your
/// screen but don't want to use a new page for that.
class
BackButtonListener
extends
StatefulWidget
{
/// Creates a BackButtonListener widget .
///
/// The [child] and [onBackButtonPressed] arguments must not be null.
const
BackButtonListener
({
Key
?
key
,
required
this
.
child
,
required
this
.
onBackButtonPressed
,
})
:
super
(
key:
key
);
/// The widget below this widget in the tree.
final
Widget
child
;
/// The callback function that will be called when the back button is pressed.
///
/// It must return a boolean future with true if this child will handle the request;
/// otherwise, return a boolean future with false.
final
ValueGetter
<
Future
<
bool
>>
onBackButtonPressed
;
@override
_BackButtonListenerState
createState
()
=>
_BackButtonListenerState
();
}
class
_BackButtonListenerState
extends
State
<
BackButtonListener
>
{
BackButtonDispatcher
?
dispatcher
;
@override
void
didChangeDependencies
()
{
dispatcher
?.
removeCallback
(
widget
.
onBackButtonPressed
);
final
BackButtonDispatcher
?
rootBackDispatcher
=
Router
.
of
(
context
).
backButtonDispatcher
;
assert
(
rootBackDispatcher
!=
null
,
'The parent router must have a backButtonDispatcher to use this widget'
);
dispatcher
=
rootBackDispatcher
!.
createChildBackButtonDispatcher
()
..
addCallback
(
widget
.
onBackButtonPressed
)
..
takePriority
();
super
.
didChangeDependencies
();
}
@override
void
didUpdateWidget
(
covariant
BackButtonListener
oldWidget
)
{
if
(
oldWidget
.
onBackButtonPressed
!=
widget
.
onBackButtonPressed
)
{
dispatcher
?.
removeCallback
(
oldWidget
.
onBackButtonPressed
);
dispatcher
?.
addCallback
(
widget
.
onBackButtonPressed
);
}
super
.
didUpdateWidget
(
oldWidget
);
}
@override
void
dispose
()
{
dispatcher
?.
removeCallback
(
widget
.
onBackButtonPressed
);
super
.
dispose
();
}
@override
Widget
build
(
BuildContext
context
)
=>
widget
.
child
;
}
/// A delegate that is used by the [Router] widget to parse a route information
/// into a configuration of type T.
///
...
...
packages/flutter/test/widgets/router_test.dart
View file @
7cec142f
This diff is collapsed.
Click to expand it.
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