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 {
/// * [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);
}
},
);
}
......
......@@ -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: <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