Unverified Commit 9f20bd6c authored by jslavitz's avatar jslavitz Committed by GitHub

Adds haptic vibration to Cupertino switch (#27114)

* adds haptic vibration to switch
parent 237fc2fb
......@@ -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;
......
......@@ -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<MethodCall> log = <MethodCall>[];
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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment