From 07fdfb70d37e04fc2bcf3838453a65f1bdc92975 Mon Sep 17 00:00:00 2001 From: jslavitz Date: Fri, 15 Feb 2019 10:55:06 -0800 Subject: [PATCH] Fixes switch vibration (#27900) * Fixes Cupertino switch vibration --- .../flutter/lib/src/cupertino/switch.dart | 27 +-- .../flutter/test/cupertino/switch_test.dart | 166 +++++++++++++++++- 2 files changed, 182 insertions(+), 11 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/switch.dart b/packages/flutter/lib/src/cupertino/switch.dart index 91dac05a87..1dee9efd49 100644 --- a/packages/flutter/lib/src/cupertino/switch.dart +++ b/packages/flutter/lib/src/cupertino/switch.dart @@ -297,14 +297,6 @@ class _RenderCupertinoSwitch extends RenderConstrainedBox { markNeedsPaint(); markNeedsSemanticsUpdate(); } - switch(defaultTargetPlatform) { - case TargetPlatform.iOS: - HapticFeedback.lightImpact(); - break; - case TargetPlatform.fuchsia: - case TargetPlatform.android: - break; - } } TextDirection get textDirection => _textDirection; @@ -375,8 +367,10 @@ class _RenderCupertinoSwitch extends RenderConstrainedBox { } void _handleTap() { - if (isInteractive) + if (isInteractive) { onChanged(!_value); + _emitVibration(); + } } void _handleTapUp(TapUpDetails details) { @@ -390,8 +384,10 @@ class _RenderCupertinoSwitch extends RenderConstrainedBox { } void _handleDragStart(DragStartDetails details) { - if (isInteractive) + if (isInteractive) { _reactionController.forward(); + _emitVibration(); + } } void _handleDragUpdate(DragUpdateDetails details) { @@ -419,6 +415,17 @@ class _RenderCupertinoSwitch extends RenderConstrainedBox { _reactionController.reverse(); } + void _emitVibration(){ + switch(defaultTargetPlatform) { + case TargetPlatform.iOS: + HapticFeedback.lightImpact(); + break; + case TargetPlatform.fuchsia: + case TargetPlatform.android: + break; + } + } + @override bool hitTestSelf(Offset position) => true; diff --git a/packages/flutter/test/cupertino/switch_test.dart b/packages/flutter/test/cupertino/switch_test.dart index fa6073002d..44926724f5 100644 --- a/packages/flutter/test/cupertino/switch_test.dart +++ b/packages/flutter/test/cupertino/switch_test.dart @@ -73,13 +73,177 @@ void main() { ); await tester.tap(find.byKey(switchKey)); - await tester.pumpAndSettle(); + await tester.pump(); expect(log, hasLength(1)); expect(log.single, isMethodCall('HapticFeedback.vibrate', arguments: 'HapticFeedbackType.lightImpact')); debugDefaultTargetPlatformOverride = null; }); + testWidgets('Using other widgets that rebuild the switch will not cause vibrations', (WidgetTester tester) async { + debugDefaultTargetPlatformOverride = TargetPlatform.iOS; + final Key switchKey = UniqueKey(); + final Key switchKey2 = UniqueKey(); + bool value = false; + bool value2 = false; + final List log = []; + + SystemChannels.platform.setMockMethodCallHandler((MethodCall methodCall) async { + log.add(methodCall); + }); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return Center( + child: Column( + children: [ + CupertinoSwitch( + key: switchKey, + value: value, + onChanged: (bool newValue) { + setState(() { + value = newValue; + }); + }, + ), + CupertinoSwitch( + key: switchKey2, + value: value2, + onChanged: (bool newValue) { + setState(() { + value2 = newValue; + }); + }, + ), + ] + ), + ); + }, + ), + ), + ); + + await tester.tap(find.byKey(switchKey)); + await tester.pump(); + + expect(log, hasLength(1)); + expect(log[0], isMethodCall('HapticFeedback.vibrate', arguments: 'HapticFeedbackType.lightImpact')); + + await tester.tap(find.byKey(switchKey2)); + await tester.pump(); + + expect(log, hasLength(2)); + expect(log[1], isMethodCall('HapticFeedback.vibrate', arguments: 'HapticFeedbackType.lightImpact')); + + await tester.tap(find.byKey(switchKey)); + await tester.pump(); + + expect(log, hasLength(3)); + expect(log[2], isMethodCall('HapticFeedback.vibrate', arguments: 'HapticFeedbackType.lightImpact')); + + await tester.tap(find.byKey(switchKey2)); + await tester.pump(); + + expect(log, hasLength(4)); + expect(log[3], isMethodCall('HapticFeedback.vibrate', arguments: 'HapticFeedbackType.lightImpact')); + debugDefaultTargetPlatformOverride = null; + }); + + testWidgets('Haptic vibration triggers on drag', (WidgetTester tester) async { + debugDefaultTargetPlatformOverride = TargetPlatform.iOS; + bool value = false; + final List log = []; + + SystemChannels.platform.setMockMethodCallHandler((MethodCall methodCall) async { + log.add(methodCall); + }); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return Center( + child: CupertinoSwitch( + value: value, + dragStartBehavior: DragStartBehavior.down, + onChanged: (bool newValue) { + setState(() { + value = newValue; + }); + }, + ), + ); + }, + ), + ), + ); + + await tester.drag(find.byType(CupertinoSwitch), const Offset(30.0, 0.0)); + expect(value, isTrue); + await tester.pump(); + + expect(log, hasLength(1)); + expect(log[0], isMethodCall('HapticFeedback.vibrate', arguments: 'HapticFeedbackType.lightImpact')); + debugDefaultTargetPlatformOverride = null; + }); + + testWidgets('No haptic vibration triggers from a programmatic value change', (WidgetTester tester) async { + debugDefaultTargetPlatformOverride = TargetPlatform.iOS; + final Key switchKey = UniqueKey(); + bool value = false; + + final List log = []; + SystemChannels.platform.setMockMethodCallHandler((MethodCall methodCall) async { + log.add(methodCall); + }); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return Center( + child: Column( + children: [ + CupertinoButton( + child: const Text('Button'), + onPressed: (){ + setState(() { + value = !value; + }); + }, + ), + CupertinoSwitch( + key: switchKey, + value: value, + onChanged: (bool newValue) { + setState(() { + value = newValue; + }); + }, + ), + ] + ), + ); + }, + ), + ), + ); + + expect(value, isFalse); + + await tester.tap(find.byType(CupertinoButton)); + expect(value, isTrue); + await tester.pump(); + + expect(log, hasLength(0)); + debugDefaultTargetPlatformOverride = null; + }); + testWidgets('Switch can drag (LTR)', (WidgetTester tester) async { bool value = false;