Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onLongPress to Buttons #40641

Merged
merged 29 commits into from Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
fe69fff
Add onLongPress to Buttons.
bernaferrari Sep 17, 2019
e0c73e7
Button enabled status will now respond to onLongPress
bernaferrari Sep 27, 2019
5151ca0
Improve documentation as per Hans Muller review.
bernaferrari Sep 27, 2019
7db8303
Tests
bernaferrari Sep 30, 2019
3135153
Merge branch 'master' of https://github.com/flutter/flutter into onlo…
bernaferrari Oct 4, 2019
36a633f
fix button onPressed test. Replace tester.pressed with tester.tap
bernaferrari Oct 4, 2019
3640f39
make tests full flutter analyze compliant
bernaferrari Oct 4, 2019
49427e3
Fix whitespace after comments line
bernaferrari Oct 4, 2019
c21d2dd
Merge branch 'master' into onlongpressed2
bernaferrari Oct 4, 2019
ae4231d
Fix whitespace
bernaferrari Oct 4, 2019
1e7dcc0
Improve test to use enabled instead of default color.
bernaferrari Oct 15, 2019
5da494f
Add tests for Flatbutton, OutlineButton, RawMaterialButton and Raised…
bernaferrari Oct 15, 2019
7180850
Fix whitespace detected by analyzer
bernaferrari Oct 15, 2019
2ac3fc0
Replace ObjectFlagProperty with FlagProperty.
bernaferrari Oct 15, 2019
f681826
Fix last commit.
bernaferrari Oct 15, 2019
9c2a5b1
Merge branch 'master' into onlongpressed2
bernaferrari Oct 15, 2019
3db4aa3
Migrated tests to the correct files. Improved outline_button_test and…
bernaferrari Oct 16, 2019
837b2db
Fix additional ) which slipped.
bernaferrari Oct 16, 2019
aaa268e
Fix analyzer issues
bernaferrari Oct 16, 2019
a7d072d
Fix additional analyzer issues.
bernaferrari Oct 16, 2019
9766af2
Fix remaining issues with analyzer. Took me a while to make flutter w…
bernaferrari Oct 17, 2019
3be4aeb
Last analyzer fix. Use const and single quotes.
bernaferrari Oct 17, 2019
95c6ff8
Fix whitespace thing.
bernaferrari Oct 17, 2019
bc6d7e5
Fix reviewed issues
bernaferrari Oct 28, 2019
577d1dd
Remove pumpAndSettle, simplify onPressed null, onLongPress null.
bernaferrari Oct 28, 2019
016da04
Final commit, cosmetic improvement.
bernaferrari Oct 29, 2019
a22c696
Merge branch 'master' into onlongpressed2
Oct 29, 2019
f03b92f
Remove whitespace
Oct 29, 2019
d9acc3e
Remove more whitespace
Oct 29, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions packages/flutter/lib/src/material/button.dart
Expand Up @@ -38,6 +38,7 @@ class RawMaterialButton extends StatefulWidget {
const RawMaterialButton({
Key key,
@required this.onPressed,
this.onLongPress,
this.onHighlightChanged,
this.textStyle,
this.fillColor,
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -352,6 +366,7 @@ class _RawMaterialButtonState extends State<RawMaterialButton> {
hoverColor: widget.hoverColor,
onHover: _handleHoveredChanged,
onTap: widget.onPressed,
onLongPress: widget.onLongPress,
customBorder: effectiveShape,
child: IconTheme.merge(
data: IconThemeData(color: effectiveTextColor),
Expand Down
8 changes: 7 additions & 1 deletion packages/flutter/lib/src/material/flat_button.dart
Expand Up @@ -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
Expand Down Expand Up @@ -102,6 +102,7 @@ class FlatButton extends MaterialButton {
const FlatButton({
Key key,
@required VoidCallback onPressed,
VoidCallback onLongPress,
ValueChanged<bool> onHighlightChanged,
ButtonTextTheme textTheme,
Color textColor,
Expand All @@ -125,6 +126,7 @@ class FlatButton extends MaterialButton {
super(
key: key,
onPressed: onPressed,
onLongPress: onLongPress,
onHighlightChanged: onHighlightChanged,
textTheme: textTheme,
textColor: textColor,
Expand Down Expand Up @@ -155,6 +157,7 @@ class FlatButton extends MaterialButton {
factory FlatButton.icon({
Key key,
@required VoidCallback onPressed,
VoidCallback onLongPress,
ValueChanged<bool> onHighlightChanged,
ButtonTextTheme textTheme,
Color textColor,
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -215,6 +219,7 @@ class _FlatButtonWithIcon extends FlatButton with MaterialButtonWithIconMixin {
_FlatButtonWithIcon({
Key key,
@required VoidCallback onPressed,
VoidCallback onLongPress,
ValueChanged<bool> onHighlightChanged,
ButtonTextTheme textTheme,
Color textColor,
Expand All @@ -241,6 +246,7 @@ class _FlatButtonWithIcon extends FlatButton with MaterialButtonWithIconMixin {
super(
key: key,
onPressed: onPressed,
onLongPress: onLongPress,
onHighlightChanged: onHighlightChanged,
textTheme: textTheme,
textColor: textColor,
Expand Down
27 changes: 21 additions & 6 deletions packages/flutter/lib/src/material/material_button.dart
Expand Up @@ -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
Expand Down Expand Up @@ -51,6 +51,7 @@ class MaterialButton extends StatelessWidget {
const MaterialButton({
Key key,
@required this.onPressed,
this.onLongPress,
this.onHighlightChanged,
this.textTheme,
this.textColor,
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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].
///
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -377,7 +392,7 @@ class MaterialButton extends StatelessWidget {
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(ObjectFlagProperty<VoidCallback>('onPressed', onPressed, ifNull: 'disabled'));
properties.add(FlagProperty('enabled', value: enabled, ifFalse: 'disabled'));
properties.add(DiagnosticsProperty<ButtonTextTheme>('textTheme', textTheme, defaultValue: null));
properties.add(ColorProperty('textColor', textColor, defaultValue: null));
properties.add(ColorProperty('disabledTextColor', disabledTextColor, defaultValue: null));
Expand Down
13 changes: 11 additions & 2 deletions packages/flutter/lib/src/material/outline_button.dart
Expand Up @@ -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
Expand Down Expand Up @@ -61,6 +61,7 @@ class OutlineButton extends MaterialButton {
const OutlineButton({
Key key,
@required VoidCallback onPressed,
VoidCallback onLongPress,
ButtonTextTheme textTheme,
Color textColor,
Color disabledTextColor,
Expand All @@ -85,6 +86,7 @@ class OutlineButton extends MaterialButton {
super(
key: key,
onPressed: onPressed,
onLongPress: onLongPress,
textTheme: textTheme,
textColor: textColor,
disabledTextColor: disabledTextColor,
Expand Down Expand Up @@ -113,6 +115,7 @@ class OutlineButton extends MaterialButton {
factory OutlineButton.icon({
Key key,
@required VoidCallback onPressed,
VoidCallback onLongPress,
ButtonTextTheme textTheme,
Color textColor,
Color disabledTextColor,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -207,6 +211,7 @@ class _OutlineButtonWithIcon extends OutlineButton with MaterialButtonWithIconMi
_OutlineButtonWithIcon({
Key key,
@required VoidCallback onPressed,
VoidCallback onLongPress,
ButtonTextTheme textTheme,
Color textColor,
Color disabledTextColor,
Expand Down Expand Up @@ -234,6 +239,7 @@ class _OutlineButtonWithIcon extends OutlineButton with MaterialButtonWithIconMi
super(
key: key,
onPressed: onPressed,
onLongPress: onLongPress,
textTheme: textTheme,
textColor: textColor,
disabledTextColor: disabledTextColor,
Expand Down Expand Up @@ -266,6 +272,7 @@ class _OutlineButton extends StatefulWidget {
const _OutlineButton({
Key key,
@required this.onPressed,
this.onLongPress,
this.brightness,
this.textTheme,
this.textColor,
Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions packages/flutter/lib/src/material/raised_button.dart
Expand Up @@ -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.
Expand Down Expand Up @@ -108,6 +108,7 @@ class RaisedButton extends MaterialButton {
const RaisedButton({
Key key,
@required VoidCallback onPressed,
VoidCallback onLongPress,
ValueChanged<bool> onHighlightChanged,
ButtonTextTheme textTheme,
Color textColor,
Expand Down Expand Up @@ -142,6 +143,7 @@ class RaisedButton extends MaterialButton {
super(
key: key,
onPressed: onPressed,
onLongPress: onLongPress,
onHighlightChanged: onHighlightChanged,
textTheme: textTheme,
textColor: textColor,
Expand Down Expand Up @@ -179,6 +181,7 @@ class RaisedButton extends MaterialButton {
factory RaisedButton.icon({
Key key,
@required VoidCallback onPressed,
VoidCallback onLongPress,
ValueChanged<bool> onHighlightChanged,
ButtonTextTheme textTheme,
Color textColor,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -252,6 +256,7 @@ class _RaisedButtonWithIcon extends RaisedButton with MaterialButtonWithIconMixi
_RaisedButtonWithIcon({
Key key,
@required VoidCallback onPressed,
VoidCallback onLongPress,
ValueChanged<bool> onHighlightChanged,
ButtonTextTheme textTheme,
Color textColor,
Expand Down Expand Up @@ -284,6 +289,7 @@ class _RaisedButtonWithIcon extends RaisedButton with MaterialButtonWithIconMixi
super(
key: key,
onPressed: onPressed,
onLongPress: onLongPress,
onHighlightChanged: onHighlightChanged,
textTheme: textTheme,
textColor: textColor,
Expand Down
1 change: 1 addition & 0 deletions packages/flutter/test/material/buttons_test.dart
Expand Up @@ -1187,6 +1187,7 @@ void main() {
child: MaterialButton(
disabledColor: Color(0xff00ff00),
onPressed: null,
onLongPress: null,
child: Text('button'),
),
),
Expand Down