From c96ab53b42eb74eae58bf43db9f9486163a0b1cb Mon Sep 17 00:00:00 2001 From: David Yang Date: Thu, 6 Apr 2017 02:52:24 -0400 Subject: [PATCH] Allowing more flexibility to the active opacity of the Cupertino Button (#9071) * Allowing overrides to the activeOpacity of the button * adding tests * fix * fixing animation * fixes * moar fixes --- .../flutter/lib/src/cupertino/button.dart | 38 ++++++++++++++---- .../flutter/test/cupertino/button_test.dart | 40 +++++++++++++++++++ 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/button.dart b/packages/flutter/lib/src/cupertino/button.dart index 7e6b99ea6f..2e10e0ed83 100644 --- a/packages/flutter/lib/src/cupertino/button.dart +++ b/packages/flutter/lib/src/cupertino/button.dart @@ -47,8 +47,11 @@ class CupertinoButton extends StatefulWidget { this.padding, this.color, this.minSize: 44.0, + this.pressedOpacity: 0.1, @required this.onPressed, - }); + }) { + assert(pressedOpacity >= 0.0 && pressedOpacity <= 1.0); + } /// The widget below this widget in the tree. /// @@ -80,6 +83,12 @@ class CupertinoButton extends StatefulWidget { /// * final double minSize; + /// The opacity that the button will fade to when it is pressed. + /// The button will have an opacity of 1.0 when it is not pressed. + /// + /// This defaults to 0.1. + final double pressedOpacity; + /// 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; @@ -99,17 +108,26 @@ class _CupertinoButtonState extends State with SingleTickerProv // Eyeballed values. Feel free to tweak. static const Duration kFadeOutDuration = const Duration(milliseconds: 10); static const Duration kFadeInDuration = const Duration(milliseconds: 350); + Tween _opacityTween; AnimationController _animationController; + void _setTween() { + _opacityTween = new Tween( + begin: 1.0, + end: config.pressedOpacity, + ); + } + @override void initState() { super.initState(); _animationController = new AnimationController( duration: const Duration(milliseconds: 200), - value: 1.0, + value: 0.0, vsync: this, ); + _setTween(); } @override @@ -119,16 +137,22 @@ class _CupertinoButtonState extends State with SingleTickerProv super.dispose(); } + @override + void didUpdateConfig(CupertinoButton old) { + super.didUpdateConfig(old); + _setTween(); + } + void _handleTapDown(PointerDownEvent event) { - _animationController.animateTo(0.1, duration: kFadeOutDuration); + _animationController.animateTo(1.0, duration: kFadeOutDuration); } void _handleTapUp(PointerUpEvent event) { - _animationController.animateTo(1.0, duration: kFadeInDuration); + _animationController.animateTo(0.0, duration: kFadeInDuration); } void _handleTapCancel(PointerCancelEvent event) { - _animationController.animateTo(1.0, duration: kFadeInDuration); + _animationController.animateTo(0.0, duration: kFadeInDuration); } @override @@ -148,10 +172,10 @@ class _CupertinoButtonState extends State with SingleTickerProv minHeight: config.minSize, ), child: new FadeTransition( - opacity: new CurvedAnimation( + opacity: _opacityTween.animate(new CurvedAnimation( parent: _animationController, curve: Curves.decelerate, - ), + )), child: new DecoratedBox( decoration: new BoxDecoration( borderRadius: const BorderRadius.all(const Radius.circular(8.0)), diff --git a/packages/flutter/test/cupertino/button_test.dart b/packages/flutter/test/cupertino/button_test.dart index cb21fc6182..ba8e73176c 100644 --- a/packages/flutter/test/cupertino/button_test.dart +++ b/packages/flutter/test/cupertino/button_test.dart @@ -125,4 +125,44 @@ void main() { // Still doesn't animate. expect(SchedulerBinding.instance.transientCallbackCount, equals(0)); }); + + testWidgets('pressedOpacity defaults to 0.1', (WidgetTester tester) async { + await tester.pumpWidget(new Center(child: new CupertinoButton( + child: new Text('Tap me'), + onPressed: () { }, + ))); + + // Keep a "down" gesture on the button + final Point center = tester.getCenter(find.byType(CupertinoButton)); + await tester.startGesture(center); + await tester.pumpAndSettle(); + + // Check opacity + final Opacity opacity = tester.widget(find.descendant( + of: find.byType(CupertinoButton), + matching: find.byType(Opacity), + )); + expect(opacity.opacity, 0.1); + }); + + testWidgets('pressedOpacity parameter', (WidgetTester tester) async { + final double pressedOpacity = 0.5; + await tester.pumpWidget(new Center(child: new CupertinoButton( + pressedOpacity: pressedOpacity, + child: new Text('Tap me'), + onPressed: () { }, + ))); + + // Keep a "down" gesture on the button + final Point center = tester.getCenter(find.byType(CupertinoButton)); + await tester.startGesture(center); + await tester.pumpAndSettle(); + + // Check opacity + final Opacity opacity = tester.widget(find.descendant( + of: find.byType(CupertinoButton), + matching: find.byType(Opacity), + )); + expect(opacity.opacity, pressedOpacity); + }); }