From 28a214ba8aa9604174eabfe4af3b2a93730abae2 Mon Sep 17 00:00:00 2001 From: Bernardo Ferrari Date: Tue, 29 Oct 2019 22:41:44 -0300 Subject: [PATCH] Add onLongPress to Buttons (#40641) * Add onLongPress to Buttons. * Button enabled status will now respond to onLongPress --- packages/flutter/lib/src/material/button.dart | 21 ++++- .../flutter/lib/src/material/flat_button.dart | 8 +- .../lib/src/material/material_button.dart | 27 ++++-- .../lib/src/material/outline_button.dart | 13 ++- .../lib/src/material/raised_button.dart | 10 ++- .../test/material/flat_button_test.dart | 75 +++++++++++++++++ .../test/material/material_button_test.dart | 75 +++++++++++++++++ .../test/material/outline_button_test.dart | 84 ++++++++++++++----- .../test/material/raised_button_test.dart | 75 +++++++++++++++++ .../material/raw_material_button_test.dart | 75 +++++++++++++++++ 10 files changed, 428 insertions(+), 35 deletions(-) diff --git a/packages/flutter/lib/src/material/button.dart b/packages/flutter/lib/src/material/button.dart index 367344bf34..4b4c9cc5c5 100644 --- a/packages/flutter/lib/src/material/button.dart +++ b/packages/flutter/lib/src/material/button.dart @@ -38,6 +38,7 @@ class RawMaterialButton extends StatefulWidget { const RawMaterialButton({ Key key, @required this.onPressed, + this.onLongPress, this.onHighlightChanged, this.textStyle, this.fillColor, @@ -75,9 +76,22 @@ class RawMaterialButton extends StatefulWidget { /// Called when the button is tapped or otherwise activated. /// - /// If this is set to null, the button will be disabled, see [enabled]. + /// If this callback and [onLongPress] are null, then the button will be disabled. + /// + /// See also: + /// + /// * [enabled], which is true if the button is enabled. final VoidCallback onPressed; + /// Called when the button is long-pressed. + /// + /// If this callback and [onPressed] are null, then the button will be disabled. + /// + /// See also: + /// + /// * [enabled], which is true if the button is enabled. + final VoidCallback onLongPress; + /// Called by the underlying [InkWell] widget's [InkWell.onHighlightChanged] /// callback. /// @@ -222,8 +236,8 @@ class RawMaterialButton extends StatefulWidget { /// Whether the button is enabled or disabled. /// /// Buttons are disabled by default. To enable a button, set its [onPressed] - /// property to a non-null value. - bool get enabled => onPressed != null; + /// or [onLongPress] properties to a non-null value. + bool get enabled => onPressed != null || onLongPress != null; /// Configures the minimum size of the tap target. /// @@ -352,6 +366,7 @@ class _RawMaterialButtonState extends State { hoverColor: widget.hoverColor, onHover: _handleHoveredChanged, onTap: widget.onPressed, + onLongPress: widget.onLongPress, customBorder: effectiveShape, child: IconTheme.merge( data: IconThemeData(color: effectiveTextColor), diff --git a/packages/flutter/lib/src/material/flat_button.dart b/packages/flutter/lib/src/material/flat_button.dart index 2a2cead5dc..2d22796784 100644 --- a/packages/flutter/lib/src/material/flat_button.dart +++ b/packages/flutter/lib/src/material/flat_button.dart @@ -29,7 +29,7 @@ import 'theme_data.dart'; /// interactive, with ink splashes, without also committing to these stylistic /// choices, consider using [InkWell] instead. /// -/// If the [onPressed] callback is null, then the button will be disabled, +/// If the [onPressed] and [onLongPress] callbacks are null, then this button will be disabled, /// will not react to touch, and will be colored as specified by /// the [disabledColor] property instead of the [color] property. If you are /// trying to change the button's [color] and it is not having any effect, check @@ -102,6 +102,7 @@ class FlatButton extends MaterialButton { const FlatButton({ Key key, @required VoidCallback onPressed, + VoidCallback onLongPress, ValueChanged onHighlightChanged, ButtonTextTheme textTheme, Color textColor, @@ -125,6 +126,7 @@ class FlatButton extends MaterialButton { super( key: key, onPressed: onPressed, + onLongPress: onLongPress, onHighlightChanged: onHighlightChanged, textTheme: textTheme, textColor: textColor, @@ -155,6 +157,7 @@ class FlatButton extends MaterialButton { factory FlatButton.icon({ Key key, @required VoidCallback onPressed, + VoidCallback onLongPress, ValueChanged onHighlightChanged, ButtonTextTheme textTheme, Color textColor, @@ -182,6 +185,7 @@ class FlatButton extends MaterialButton { final ButtonThemeData buttonTheme = ButtonTheme.of(context); return RawMaterialButton( onPressed: onPressed, + onLongPress: onLongPress, onHighlightChanged: onHighlightChanged, fillColor: buttonTheme.getFillColor(this), textStyle: theme.textTheme.button.copyWith(color: buttonTheme.getTextColor(this)), @@ -215,6 +219,7 @@ class _FlatButtonWithIcon extends FlatButton with MaterialButtonWithIconMixin { _FlatButtonWithIcon({ Key key, @required VoidCallback onPressed, + VoidCallback onLongPress, ValueChanged onHighlightChanged, ButtonTextTheme textTheme, Color textColor, @@ -241,6 +246,7 @@ class _FlatButtonWithIcon extends FlatButton with MaterialButtonWithIconMixin { super( key: key, onPressed: onPressed, + onLongPress: onLongPress, onHighlightChanged: onHighlightChanged, textTheme: textTheme, textColor: textColor, diff --git a/packages/flutter/lib/src/material/material_button.dart b/packages/flutter/lib/src/material/material_button.dart index a10a08f95b..5b12358471 100644 --- a/packages/flutter/lib/src/material/material_button.dart +++ b/packages/flutter/lib/src/material/material_button.dart @@ -21,8 +21,8 @@ import 'theme_data.dart'; /// /// The button's size will expand to fit the child widget, if necessary. /// -/// MaterialButtons whose [onPressed] handler is null will be disabled. To have -/// an enabled button, make sure to pass a non-null value for onPressed. +/// MaterialButtons whose [onPressed] and [onLongPress] callbacks are null will be disabled. To have +/// an enabled button, make sure to pass a non-null value for [onPressed] or [onLongPress]. /// /// Rather than using this class directly, consider using [FlatButton], /// [OutlineButton], or [RaisedButton], which configure this class with @@ -51,6 +51,7 @@ class MaterialButton extends StatelessWidget { const MaterialButton({ Key key, @required this.onPressed, + this.onLongPress, this.onHighlightChanged, this.textTheme, this.textColor, @@ -88,9 +89,22 @@ class MaterialButton extends StatelessWidget { /// The callback that is called when the button is tapped or otherwise activated. /// - /// If this is set to null, the button will be disabled. + /// If this callback and [onLongPress] are null, then the button will be disabled. + /// + /// See also: + /// + /// * [enabled], which is true if the button is enabled. final VoidCallback onPressed; + /// The callback that is called when the button is long-pressed. + /// + /// If this callback and [onPressed] are null, then the button will be disabled. + /// + /// See also: + /// + /// * [enabled], which is true if the button is enabled. + final VoidCallback onLongPress; + /// Called by the underlying [InkWell] widget's [InkWell.onHighlightChanged] /// callback. /// @@ -287,8 +301,8 @@ class MaterialButton extends StatelessWidget { /// Whether the button is enabled or disabled. /// /// Buttons are disabled by default. To enable a button, set its [onPressed] - /// property to a non-null value. - bool get enabled => onPressed != null; + /// or [onLongPress] properties to a non-null value. + bool get enabled => onPressed != null || onLongPress != null; /// The internal padding for the button's [child]. /// @@ -348,6 +362,7 @@ class MaterialButton extends StatelessWidget { return RawMaterialButton( onPressed: onPressed, + onLongPress: onLongPress, onHighlightChanged: onHighlightChanged, fillColor: buttonTheme.getFillColor(this), textStyle: theme.textTheme.button.copyWith(color: buttonTheme.getTextColor(this)), @@ -377,7 +392,7 @@ class MaterialButton extends StatelessWidget { @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); - properties.add(ObjectFlagProperty('onPressed', onPressed, ifNull: 'disabled')); + properties.add(FlagProperty('enabled', value: enabled, ifFalse: 'disabled')); properties.add(DiagnosticsProperty('textTheme', textTheme, defaultValue: null)); properties.add(ColorProperty('textColor', textColor, defaultValue: null)); properties.add(ColorProperty('disabledTextColor', disabledTextColor, defaultValue: null)); diff --git a/packages/flutter/lib/src/material/outline_button.dart b/packages/flutter/lib/src/material/outline_button.dart index dcb54e10ec..3491cfaae7 100644 --- a/packages/flutter/lib/src/material/outline_button.dart +++ b/packages/flutter/lib/src/material/outline_button.dart @@ -28,7 +28,7 @@ const Duration _kElevationDuration = Duration(milliseconds: 75); /// wide grey rounded rectangle that does not change when the button is /// pressed or disabled. By default the button's background is transparent. /// -/// If the [onPressed] callback is null, then the button will be disabled and by +/// If the [onPressed] or [onLongPress] callbacks are null, then the button will be disabled and by /// default will resemble a flat button in the [disabledColor]. /// /// The button's [highlightElevation], which defines the size of the @@ -61,6 +61,7 @@ class OutlineButton extends MaterialButton { const OutlineButton({ Key key, @required VoidCallback onPressed, + VoidCallback onLongPress, ButtonTextTheme textTheme, Color textColor, Color disabledTextColor, @@ -85,6 +86,7 @@ class OutlineButton extends MaterialButton { super( key: key, onPressed: onPressed, + onLongPress: onLongPress, textTheme: textTheme, textColor: textColor, disabledTextColor: disabledTextColor, @@ -113,6 +115,7 @@ class OutlineButton extends MaterialButton { factory OutlineButton.icon({ Key key, @required VoidCallback onPressed, + VoidCallback onLongPress, ButtonTextTheme textTheme, Color textColor, Color disabledTextColor, @@ -169,6 +172,7 @@ class OutlineButton extends MaterialButton { final ButtonThemeData buttonTheme = ButtonTheme.of(context); return _OutlineButton( onPressed: onPressed, + onLongPress: onLongPress, brightness: buttonTheme.getBrightness(this), textTheme: textTheme, textColor: buttonTheme.getTextColor(this), @@ -207,6 +211,7 @@ class _OutlineButtonWithIcon extends OutlineButton with MaterialButtonWithIconMi _OutlineButtonWithIcon({ Key key, @required VoidCallback onPressed, + VoidCallback onLongPress, ButtonTextTheme textTheme, Color textColor, Color disabledTextColor, @@ -234,6 +239,7 @@ class _OutlineButtonWithIcon extends OutlineButton with MaterialButtonWithIconMi super( key: key, onPressed: onPressed, + onLongPress: onLongPress, textTheme: textTheme, textColor: textColor, disabledTextColor: disabledTextColor, @@ -266,6 +272,7 @@ class _OutlineButton extends StatefulWidget { const _OutlineButton({ Key key, @required this.onPressed, + this.onLongPress, this.brightness, this.textTheme, this.textColor, @@ -292,6 +299,7 @@ class _OutlineButton extends StatefulWidget { super(key: key); final VoidCallback onPressed; + final VoidCallback onLongPress; final Brightness brightness; final ButtonTextTheme textTheme; final Color textColor; @@ -312,7 +320,7 @@ class _OutlineButton extends StatefulWidget { final bool autofocus; final Widget child; - bool get enabled => onPressed != null; + bool get enabled => onPressed != null || onLongPress != null; @override _OutlineButtonState createState() => _OutlineButtonState(); @@ -440,6 +448,7 @@ class _OutlineButtonState extends State<_OutlineButton> with SingleTickerProvide highlightColor: widget.highlightColor, disabledColor: Colors.transparent, onPressed: widget.onPressed, + onLongPress: widget.onLongPress, elevation: 0.0, disabledElevation: 0.0, focusElevation: 0.0, diff --git a/packages/flutter/lib/src/material/raised_button.dart b/packages/flutter/lib/src/material/raised_button.dart index 1758b526fd..86758b2bfe 100644 --- a/packages/flutter/lib/src/material/raised_button.dart +++ b/packages/flutter/lib/src/material/raised_button.dart @@ -20,10 +20,10 @@ import 'theme_data.dart'; /// in long busy lists of content, or in wide spaces. Avoid using raised buttons /// on already-raised content such as dialogs or cards. /// -/// If the [onPressed] callback is null, then the button will be disabled and by +/// If [onPressed] and [onLongPress] callbacks are null, then the button will be disabled and by /// default will resemble a flat button in the [disabledColor]. If you are /// trying to change the button's [color] and it is not having any effect, check -/// that you are passing a non-null [onPressed] handler. +/// that you are passing a non-null [onPressed] or [onLongPress] callbacks. /// /// If you want an ink-splash effect for taps, but don't want to use a button, /// consider using [InkWell] directly. @@ -108,6 +108,7 @@ class RaisedButton extends MaterialButton { const RaisedButton({ Key key, @required VoidCallback onPressed, + VoidCallback onLongPress, ValueChanged onHighlightChanged, ButtonTextTheme textTheme, Color textColor, @@ -142,6 +143,7 @@ class RaisedButton extends MaterialButton { super( key: key, onPressed: onPressed, + onLongPress: onLongPress, onHighlightChanged: onHighlightChanged, textTheme: textTheme, textColor: textColor, @@ -179,6 +181,7 @@ class RaisedButton extends MaterialButton { factory RaisedButton.icon({ Key key, @required VoidCallback onPressed, + VoidCallback onLongPress, ValueChanged onHighlightChanged, ButtonTextTheme textTheme, Color textColor, @@ -209,6 +212,7 @@ class RaisedButton extends MaterialButton { final ButtonThemeData buttonTheme = ButtonTheme.of(context); return RawMaterialButton( onPressed: onPressed, + onLongPress: onLongPress, onHighlightChanged: onHighlightChanged, clipBehavior: clipBehavior, fillColor: buttonTheme.getFillColor(this), @@ -252,6 +256,7 @@ class _RaisedButtonWithIcon extends RaisedButton with MaterialButtonWithIconMixi _RaisedButtonWithIcon({ Key key, @required VoidCallback onPressed, + VoidCallback onLongPress, ValueChanged onHighlightChanged, ButtonTextTheme textTheme, Color textColor, @@ -284,6 +289,7 @@ class _RaisedButtonWithIcon extends RaisedButton with MaterialButtonWithIconMixi super( key: key, onPressed: onPressed, + onLongPress: onLongPress, onHighlightChanged: onHighlightChanged, textTheme: textTheme, textColor: textColor, diff --git a/packages/flutter/test/material/flat_button_test.dart b/packages/flutter/test/material/flat_button_test.dart index 027a8cf96b..ba66ad16ce 100644 --- a/packages/flutter/test/material/flat_button_test.dart +++ b/packages/flutter/test/material/flat_button_test.dart @@ -333,6 +333,81 @@ void main() { paintsExactlyCountTimes(#clipPath, 0), ); }); + + testWidgets('FlatButton onPressed and onLongPress callbacks are correctly called when non-null', (WidgetTester tester) async { + + bool wasPressed; + Finder flatButton; + + Widget buildFrame({ VoidCallback onPressed, VoidCallback onLongPress }) { + return Directionality( + textDirection: TextDirection.ltr, + child: FlatButton( + child: const Text('button'), + onPressed: onPressed, + onLongPress: onLongPress, + ), + ); + } + + // onPressed not null, onLongPress null. + wasPressed = false; + await tester.pumpWidget( + buildFrame(onPressed: () { wasPressed = true; }, onLongPress: null), + ); + flatButton = find.byType(FlatButton); + expect(tester.widget(flatButton).enabled, true); + await tester.tap(flatButton); + expect(wasPressed, true); + + // onPressed null, onLongPress not null. + wasPressed = false; + await tester.pumpWidget( + buildFrame(onPressed: null, onLongPress: () { wasPressed = true; }), + ); + flatButton = find.byType(FlatButton); + expect(tester.widget(flatButton).enabled, true); + await tester.longPress(flatButton); + expect(wasPressed, true); + + // onPressed null, onLongPress null. + await tester.pumpWidget( + buildFrame(onPressed: null, onLongPress: null), + ); + flatButton = find.byType(FlatButton); + expect(tester.widget(flatButton).enabled, false); + }); + + testWidgets('FlatButton onPressed and onLongPress callbacks are distinctly recognized', (WidgetTester tester) async { + bool didPressButton = false; + bool didLongPressButton = false; + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: FlatButton( + onPressed: () { + didPressButton = true; + }, + onLongPress: () { + didLongPressButton = true; + }, + child: const Text('button'), + ), + ), + ); + + final Finder flatButton = find.byType(FlatButton); + expect(tester.widget(flatButton).enabled, true); + + expect(didPressButton, isFalse); + await tester.tap(flatButton); + expect(didPressButton, isTrue); + + expect(didLongPressButton, isFalse); + await tester.longPress(flatButton); + expect(didLongPressButton, isTrue); + }); } TextStyle _iconStyle(WidgetTester tester, IconData icon) { diff --git a/packages/flutter/test/material/material_button_test.dart b/packages/flutter/test/material/material_button_test.dart index f627b6478b..37ec97d0dd 100644 --- a/packages/flutter/test/material/material_button_test.dart +++ b/packages/flutter/test/material/material_button_test.dart @@ -298,6 +298,81 @@ void main() { expect(focusNode.hasPrimaryFocus, isTrue); }); + testWidgets('MaterialButton onPressed and onLongPress callbacks are correctly called when non-null', (WidgetTester tester) async { + + bool wasPressed; + Finder materialButton; + + Widget buildFrame({ VoidCallback onPressed, VoidCallback onLongPress }) { + return Directionality( + textDirection: TextDirection.ltr, + child: MaterialButton( + child: const Text('button'), + onPressed: onPressed, + onLongPress: onLongPress, + ), + ); + } + + // onPressed not null, onLongPress null. + wasPressed = false; + await tester.pumpWidget( + buildFrame(onPressed: () { wasPressed = true; }, onLongPress: null), + ); + materialButton = find.byType(MaterialButton); + expect(tester.widget(materialButton).enabled, true); + await tester.tap(materialButton); + expect(wasPressed, true); + + // onPressed null, onLongPress not null. + wasPressed = false; + await tester.pumpWidget( + buildFrame(onPressed: null, onLongPress: () { wasPressed = true; }), + ); + materialButton = find.byType(MaterialButton); + expect(tester.widget(materialButton).enabled, true); + await tester.longPress(materialButton); + expect(wasPressed, true); + + // onPressed null, onLongPress null. + await tester.pumpWidget( + buildFrame(onPressed: null, onLongPress: null), + ); + materialButton = find.byType(MaterialButton); + expect(tester.widget(materialButton).enabled, false); + }); + + testWidgets('MaterialButton onPressed and onLongPress callbacks are distinctly recognized', (WidgetTester tester) async { + bool didPressButton = false; + bool didLongPressButton = false; + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: MaterialButton( + onPressed: () { + didPressButton = true; + }, + onLongPress: () { + didLongPressButton = true; + }, + child: const Text('button'), + ), + ), + ); + + final Finder materialButton = find.byType(MaterialButton); + expect(tester.widget(materialButton).enabled, true); + + expect(didPressButton, isFalse); + await tester.tap(materialButton); + expect(didPressButton, isTrue); + + expect(didLongPressButton, isFalse); + await tester.longPress(materialButton); + expect(didLongPressButton, isTrue); + }); + // This test is very similar to the '...explicit splashColor and highlightColor' test // in icon_button_test.dart. If you change this one, you may want to also change that one. testWidgets('MaterialButton with explicit splashColor and highlightColor', (WidgetTester tester) async { diff --git a/packages/flutter/test/material/outline_button_test.dart b/packages/flutter/test/material/outline_button_test.dart index 87b26cd11d..6eda9336c8 100644 --- a/packages/flutter/test/material/outline_button_test.dart +++ b/packages/flutter/test/material/outline_button_test.dart @@ -450,37 +450,48 @@ void main() { expect(find.byType(OutlineButton), paints..path(color: disabledColor)); }); - testWidgets('Outline button responds to tap when enabled', (WidgetTester tester) async { - int pressedCount = 0; + testWidgets('OutlineButton onPressed and onLongPress callbacks are correctly called when non-null', (WidgetTester tester) async { - Widget buildFrame(VoidCallback onPressed) { + bool wasPressed; + Finder outlineButton; + + Widget buildFrame({ VoidCallback onPressed, VoidCallback onLongPress }) { return Directionality( textDirection: TextDirection.ltr, - child: Theme( - data: ThemeData(), - child: Center( - child: OutlineButton(onPressed: onPressed), - ), + child: OutlineButton( + child: const Text('button'), + onPressed: onPressed, + onLongPress: onLongPress, ), ); } + // onPressed not null, onLongPress null. + wasPressed = false; await tester.pumpWidget( - buildFrame(() { pressedCount += 1; }), + buildFrame(onPressed: () { wasPressed = true; }, onLongPress: null), ); - expect(tester.widget(find.byType(OutlineButton)).enabled, true); - await tester.tap(find.byType(OutlineButton)); - await tester.pumpAndSettle(); - expect(pressedCount, 1); - - await tester.pumpWidget( - buildFrame(null), - ); - final Finder outlineButton = find.byType(OutlineButton); - expect(tester.widget(outlineButton).enabled, false); + outlineButton = find.byType(OutlineButton); + expect(tester.widget(outlineButton).enabled, true); await tester.tap(outlineButton); - await tester.pumpAndSettle(); - expect(pressedCount, 1); + expect(wasPressed, true); + + // onPressed null, onLongPress not null. + wasPressed = false; + await tester.pumpWidget( + buildFrame(onPressed: null, onLongPress: () { wasPressed = true; }), + ); + outlineButton = find.byType(OutlineButton); + expect(tester.widget(outlineButton).enabled, true); + await tester.longPress(outlineButton); + expect(wasPressed, true); + + // onPressed null, onLongPress null. + await tester.pumpWidget( + buildFrame(onPressed: null, onLongPress: null), + ); + outlineButton = find.byType(OutlineButton); + expect(tester.widget(outlineButton).enabled, false); }); testWidgets('Outline button doesn\'t crash if disabled during a gesture', (WidgetTester tester) async { @@ -814,6 +825,37 @@ void main() { await tester.pumpAndSettle(); _checkPhysicalLayer(buttonElement, fillColor.withAlpha(0x00)); }); + + testWidgets('OutlineButton onPressed and onLongPress callbacks are distinctly recognized', (WidgetTester tester) async { + bool didPressButton = false; + bool didLongPressButton = false; + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: OutlineButton( + onPressed: () { + didPressButton = true; + }, + onLongPress: () { + didLongPressButton = true; + }, + child: const Text('button'), + ), + ), + ); + + final Finder outlineButton = find.byType(OutlineButton); + expect(tester.widget(outlineButton).enabled, true); + + expect(didPressButton, isFalse); + await tester.tap(outlineButton); + expect(didPressButton, isTrue); + + expect(didLongPressButton, isFalse); + await tester.longPress(outlineButton); + expect(didLongPressButton, isTrue); + }); } PhysicalModelLayer _findPhysicalLayer(Element element) { diff --git a/packages/flutter/test/material/raised_button_test.dart b/packages/flutter/test/material/raised_button_test.dart index 0a5f2dbd88..c80ed5d47d 100644 --- a/packages/flutter/test/material/raised_button_test.dart +++ b/packages/flutter/test/material/raised_button_test.dart @@ -250,6 +250,81 @@ void main() { expect(textColor(), equals(disabledColor)); expect(textColor(), isNot(unusedDisabledTextColor)); }); + + testWidgets('RaisedButton onPressed and onLongPress callbacks are correctly called when non-null', (WidgetTester tester) async { + + bool wasPressed; + Finder raisedButton; + + Widget buildFrame({ VoidCallback onPressed, VoidCallback onLongPress }) { + return Directionality( + textDirection: TextDirection.ltr, + child: RaisedButton( + child: const Text('button'), + onPressed: onPressed, + onLongPress: onLongPress, + ), + ); + } + + // onPressed not null, onLongPress null. + wasPressed = false; + await tester.pumpWidget( + buildFrame(onPressed: () { wasPressed = true; }, onLongPress: null), + ); + raisedButton = find.byType(RaisedButton); + expect(tester.widget(raisedButton).enabled, true); + await tester.tap(raisedButton); + expect(wasPressed, true); + + // onPressed null, onLongPress not null. + wasPressed = false; + await tester.pumpWidget( + buildFrame(onPressed: null, onLongPress: () { wasPressed = true; }), + ); + raisedButton = find.byType(RaisedButton); + expect(tester.widget(raisedButton).enabled, true); + await tester.longPress(raisedButton); + expect(wasPressed, true); + + // onPressed null, onLongPress null. + await tester.pumpWidget( + buildFrame(onPressed: null, onLongPress: null), + ); + raisedButton = find.byType(RaisedButton); + expect(tester.widget(raisedButton).enabled, false); + }); + + testWidgets('RaisedButton onPressed and onLongPress callbacks are distinctly recognized', (WidgetTester tester) async { + bool didPressButton = false; + bool didLongPressButton = false; + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: RaisedButton( + onPressed: () { + didPressButton = true; + }, + onLongPress: () { + didLongPressButton = true; + }, + child: const Text('button'), + ), + ), + ); + + final Finder raisedButton = find.byType(RaisedButton); + expect(tester.widget(raisedButton).enabled, true); + + expect(didPressButton, isFalse); + await tester.tap(raisedButton); + expect(didPressButton, isTrue); + + expect(didLongPressButton, isFalse); + await tester.longPress(raisedButton); + expect(didLongPressButton, isTrue); + }); } TextStyle _iconStyle(WidgetTester tester, IconData icon) { diff --git a/packages/flutter/test/material/raw_material_button_test.dart b/packages/flutter/test/material/raw_material_button_test.dart index bcca5d26ab..020dd11fc0 100644 --- a/packages/flutter/test/material/raw_material_button_test.dart +++ b/packages/flutter/test/material/raw_material_button_test.dart @@ -435,4 +435,79 @@ void main() { expect(box, paints..rect(color: hoverColor)); }); + + testWidgets('RawMaterialButton onPressed and onLongPress callbacks are correctly called when non-null', (WidgetTester tester) async { + + bool wasPressed; + Finder rawMaterialButton; + + Widget buildFrame({ VoidCallback onPressed, VoidCallback onLongPress }) { + return Directionality( + textDirection: TextDirection.ltr, + child: RawMaterialButton( + child: const Text('button'), + onPressed: onPressed, + onLongPress: onLongPress, + ), + ); + } + + // onPressed not null, onLongPress null. + wasPressed = false; + await tester.pumpWidget( + buildFrame(onPressed: () { wasPressed = true; }, onLongPress: null), + ); + rawMaterialButton = find.byType(RawMaterialButton); + expect(tester.widget(rawMaterialButton).enabled, true); + await tester.tap(rawMaterialButton); + expect(wasPressed, true); + + // onPressed null, onLongPress not null. + wasPressed = false; + await tester.pumpWidget( + buildFrame(onPressed: null, onLongPress: () { wasPressed = true; }), + ); + rawMaterialButton = find.byType(RawMaterialButton); + expect(tester.widget(rawMaterialButton).enabled, true); + await tester.longPress(rawMaterialButton); + expect(wasPressed, true); + + // onPressed null, onLongPress null. + await tester.pumpWidget( + buildFrame(onPressed: null, onLongPress: null), + ); + rawMaterialButton = find.byType(RawMaterialButton); + expect(tester.widget(rawMaterialButton).enabled, false); + }); + + testWidgets('RawMaterialButton onPressed and onLongPress callbacks are distinctly recognized', (WidgetTester tester) async { + bool didPressButton = false; + bool didLongPressButton = false; + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: RawMaterialButton( + onPressed: () { + didPressButton = true; + }, + onLongPress: () { + didLongPressButton = true; + }, + child: const Text('button'), + ), + ), + ); + + final Finder rawMaterialButton = find.byType(RawMaterialButton); + expect(tester.widget(rawMaterialButton).enabled, true); + + expect(didPressButton, isFalse); + await tester.tap(rawMaterialButton); + expect(didPressButton, isTrue); + + expect(didLongPressButton, isFalse); + await tester.longPress(rawMaterialButton); + expect(didLongPressButton, isTrue); + }); }