diff --git a/packages/flutter/lib/src/cupertino/switch.dart b/packages/flutter/lib/src/cupertino/switch.dart index 1d2553a21fb..91dac05a87b 100644 --- a/packages/flutter/lib/src/cupertino/switch.dart +++ b/packages/flutter/lib/src/cupertino/switch.dart @@ -9,6 +9,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; +import 'package:flutter/services.dart'; import 'colors.dart'; import 'thumb_painter.dart'; @@ -296,6 +297,14 @@ 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; diff --git a/packages/flutter/test/cupertino/switch_test.dart b/packages/flutter/test/cupertino/switch_test.dart index 08f4a787d6e..fa6073002da 100644 --- a/packages/flutter/test/cupertino/switch_test.dart +++ b/packages/flutter/test/cupertino/switch_test.dart @@ -5,6 +5,8 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/gestures.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter/foundation.dart'; void main() { testWidgets('Switch can toggle on tap', (WidgetTester tester) async { @@ -37,6 +39,47 @@ void main() { expect(value, isTrue); }); + testWidgets('Switch emits light haptic vibration on tap', (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: CupertinoSwitch( + key: switchKey, + value: value, + dragStartBehavior: DragStartBehavior.down, + onChanged: (bool newValue) { + setState(() { + value = newValue; + }); + }, + ), + ); + }, + ), + ), + ); + + await tester.tap(find.byKey(switchKey)); + await tester.pumpAndSettle(); + + expect(log, hasLength(1)); + expect(log.single, isMethodCall('HapticFeedback.vibrate', arguments: 'HapticFeedbackType.lightImpact')); + debugDefaultTargetPlatformOverride = null; + }); + testWidgets('Switch can drag (LTR)', (WidgetTester tester) async { bool value = false;