From cccbf1f2009c9d84938426b812b0828816fd182e Mon Sep 17 00:00:00 2001 From: Flutter GitHub Bot <52682268+fluttergithubbot@users.noreply.github.com> Date: Wed, 4 Mar 2020 20:36:02 -0800 Subject: [PATCH] Custom onPressed behavior for CloseButton widget (#51925) --- .../flutter/lib/src/material/back_button.dart | 18 ++++++++- .../test/material/back_button_test.dart | 40 +++++++++++++++++-- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/packages/flutter/lib/src/material/back_button.dart b/packages/flutter/lib/src/material/back_button.dart index 54922081ed..4a2a04250f 100644 --- a/packages/flutter/lib/src/material/back_button.dart +++ b/packages/flutter/lib/src/material/back_button.dart @@ -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); + } }, ); } diff --git a/packages/flutter/test/material/back_button_test.dart b/packages/flutter/test/material/back_button_test.dart index 042e2cfb63..281598d0cc 100644 --- a/packages/flutter/test/material/back_button_test.dart +++ b/packages/flutter/test/material/back_button_test.dart @@ -35,7 +35,7 @@ void main() { }); testWidgets('BackButton onPressed overrides default pop behavior', (WidgetTester tester) async { - bool backPressed = false; + bool customCallbackWasCalled = 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: () => backPressed = true), + child: BackButton(onPressed: () => customCallbackWasCalled = 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(backPressed, true); + expect(customCallbackWasCalled, 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: { + '/next': (BuildContext context) { + return Material( + child: Center( + child: CloseButton(onPressed: () => customCallbackWasCalled = true), + ), + ); + }, + }, + ), + ); + + tester.state(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); + }); }