Unverified Commit cccbf1f2 authored by Flutter GitHub Bot's avatar Flutter GitHub Bot Committed by GitHub

Custom onPressed behavior for CloseButton widget (#51925)

parent cc52a903
...@@ -130,7 +130,7 @@ class BackButton extends StatelessWidget { ...@@ -130,7 +130,7 @@ class BackButton extends StatelessWidget {
/// * [IconButton], to create other material design icon buttons. /// * [IconButton], to create other material design icon buttons.
class CloseButton extends StatelessWidget { class CloseButton extends StatelessWidget {
/// Creates a Material Design close button. /// 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. /// The color to use for the icon.
/// ///
...@@ -138,6 +138,16 @@ class CloseButton extends StatelessWidget { ...@@ -138,6 +138,16 @@ class CloseButton extends StatelessWidget {
/// which usually matches the ambient [Theme]'s [ThemeData.iconTheme]. /// which usually matches the ambient [Theme]'s [ThemeData.iconTheme].
final Color color; 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
assert(debugCheckHasMaterialLocalizations(context)); assert(debugCheckHasMaterialLocalizations(context));
...@@ -146,7 +156,11 @@ class CloseButton extends StatelessWidget { ...@@ -146,7 +156,11 @@ class CloseButton extends StatelessWidget {
color: color, color: color,
tooltip: MaterialLocalizations.of(context).closeButtonTooltip, tooltip: MaterialLocalizations.of(context).closeButtonTooltip,
onPressed: () { onPressed: () {
Navigator.maybePop(context); if (onPressed != null) {
onPressed();
} else {
Navigator.maybePop(context);
}
}, },
); );
} }
......
...@@ -35,7 +35,7 @@ void main() { ...@@ -35,7 +35,7 @@ void main() {
}); });
testWidgets('BackButton onPressed overrides default pop behavior', (WidgetTester tester) async { testWidgets('BackButton onPressed overrides default pop behavior', (WidgetTester tester) async {
bool backPressed = false; bool customCallbackWasCalled = false;
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
home: const Material(child: Text('Home')), home: const Material(child: Text('Home')),
...@@ -43,7 +43,7 @@ void main() { ...@@ -43,7 +43,7 @@ void main() {
'/next': (BuildContext context) { '/next': (BuildContext context) {
return Material( return Material(
child: Center( child: Center(
child: BackButton(onPressed: () => backPressed = true), child: BackButton(onPressed: () => customCallbackWasCalled = true),
), ),
); );
}, },
...@@ -55,6 +55,8 @@ void main() { ...@@ -55,6 +55,8 @@ void main() {
await tester.pumpAndSettle(); 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.tap(find.byType(BackButton));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
...@@ -62,7 +64,7 @@ void main() { ...@@ -62,7 +64,7 @@ void main() {
// We're still on the second page. // We're still on the second page.
expect(find.text('Home'), findsNothing); expect(find.text('Home'), findsNothing);
// But the custom callback is called. // But the custom callback is called.
expect(backPressed, true); expect(customCallbackWasCalled, true);
}); });
testWidgets('BackButton icon', (WidgetTester tester) async { testWidgets('BackButton icon', (WidgetTester tester) async {
...@@ -180,4 +182,36 @@ void main() { ...@@ -180,4 +182,36 @@ void main() {
)); ));
expect(iconText.text.style.color, Colors.red); 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);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment