mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Adds haptic vibration to Cupertino switch (#27114)
* adds haptic vibration to switch
This commit is contained in:
parent
237fc2fb45
commit
9f20bd6cb9
@ -9,6 +9,7 @@ import 'package:flutter/foundation.dart';
|
|||||||
import 'package:flutter/gestures.dart';
|
import 'package:flutter/gestures.dart';
|
||||||
import 'package:flutter/rendering.dart';
|
import 'package:flutter/rendering.dart';
|
||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
import 'colors.dart';
|
import 'colors.dart';
|
||||||
import 'thumb_painter.dart';
|
import 'thumb_painter.dart';
|
||||||
@ -296,6 +297,14 @@ class _RenderCupertinoSwitch extends RenderConstrainedBox {
|
|||||||
markNeedsPaint();
|
markNeedsPaint();
|
||||||
markNeedsSemanticsUpdate();
|
markNeedsSemanticsUpdate();
|
||||||
}
|
}
|
||||||
|
switch(defaultTargetPlatform) {
|
||||||
|
case TargetPlatform.iOS:
|
||||||
|
HapticFeedback.lightImpact();
|
||||||
|
break;
|
||||||
|
case TargetPlatform.fuchsia:
|
||||||
|
case TargetPlatform.android:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TextDirection get textDirection => _textDirection;
|
TextDirection get textDirection => _textDirection;
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:flutter/gestures.dart';
|
import 'package:flutter/gestures.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
testWidgets('Switch can toggle on tap', (WidgetTester tester) async {
|
testWidgets('Switch can toggle on tap', (WidgetTester tester) async {
|
||||||
@ -37,6 +39,47 @@ void main() {
|
|||||||
expect(value, isTrue);
|
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 {
|
testWidgets('Switch can drag (LTR)', (WidgetTester tester) async {
|
||||||
bool value = false;
|
bool value = false;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user