Spaces:
Runtime error
Runtime error
Commit
·
6d1f94e
1
Parent(s):
ba01968
added basic vibrating for web
Browse files- client/lib/HomeScreen.dart +22 -4
- client/lib/vibrate_web.dart +16 -0
- client/web/app.js +6 -0
- client/web/index.html +1 -0
client/lib/HomeScreen.dart
CHANGED
|
@@ -10,6 +10,10 @@ import 'package:web_socket_channel/status.dart' as status;
|
|
| 10 |
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
|
| 11 |
|
| 12 |
import 'package:vibration/vibration.dart'; // for the haptics
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
import "dart:convert";
|
| 15 |
import "dart:async";
|
|
@@ -95,6 +99,10 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 95 |
// )
|
| 96 |
// );
|
| 97 |
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
}
|
| 99 |
|
| 100 |
@override
|
|
@@ -566,21 +574,31 @@ class HomeScreenState extends State<HomeScreen>{
|
|
| 566 |
}
|
| 567 |
|
| 568 |
void startHapticFeedback() async{
|
| 569 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 570 |
if (await Vibration.hasVibrator()==true) {
|
| 571 |
Vibration.vibrate(pattern:[100,100,20], repeat: 1); // short fast
|
| 572 |
// Vibration.vibrate(pattern:[100, 200, 400],repeat: 1); // slow
|
| 573 |
}
|
| 574 |
}
|
| 575 |
-
|
| 576 |
}
|
| 577 |
void endHapticFeedback() async{
|
| 578 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 579 |
if (await Vibration.hasVibrator()==true) {
|
| 580 |
Vibration.cancel();
|
| 581 |
}
|
| 582 |
}
|
| 583 |
-
catch(e){}
|
| 584 |
}
|
| 585 |
|
| 586 |
void detectTouchStart(DragStartDetails details){
|
|
|
|
| 10 |
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
|
| 11 |
|
| 12 |
import 'package:vibration/vibration.dart'; // for the haptics
|
| 13 |
+
// import 'package:vibration_web/vibration_web.dart'; // for haptics on web
|
| 14 |
+
import 'vibrate_web.dart'; // for haptics on web
|
| 15 |
+
|
| 16 |
+
import 'package:flutter/foundation.dart' show kIsWeb; // to check of platform is web
|
| 17 |
|
| 18 |
import "dart:convert";
|
| 19 |
import "dart:async";
|
|
|
|
| 99 |
// )
|
| 100 |
// );
|
| 101 |
// });
|
| 102 |
+
// vibration_web.test();
|
| 103 |
+
// vibration_web.test();
|
| 104 |
+
// vibration_web.vibrate(pattern:[200,100]);
|
| 105 |
+
// vibration_web.vibrate(pattern:[100]);
|
| 106 |
}
|
| 107 |
|
| 108 |
@override
|
|
|
|
| 574 |
}
|
| 575 |
|
| 576 |
void startHapticFeedback() async{
|
| 577 |
+
|
| 578 |
+
if(kIsWeb){
|
| 579 |
+
// different handler for web
|
| 580 |
+
// vibration_web.test();
|
| 581 |
+
vibration_web.vibrate(pattern:[100,100,20]);
|
| 582 |
+
// HapticFeedback.heavyImpact();
|
| 583 |
+
}
|
| 584 |
+
else{
|
| 585 |
if (await Vibration.hasVibrator()==true) {
|
| 586 |
Vibration.vibrate(pattern:[100,100,20], repeat: 1); // short fast
|
| 587 |
// Vibration.vibrate(pattern:[100, 200, 400],repeat: 1); // slow
|
| 588 |
}
|
| 589 |
}
|
| 590 |
+
|
| 591 |
}
|
| 592 |
void endHapticFeedback() async{
|
| 593 |
+
if(kIsWeb){
|
| 594 |
+
// different handler for web
|
| 595 |
+
vibration_web.cancel();
|
| 596 |
+
}
|
| 597 |
+
else{
|
| 598 |
if (await Vibration.hasVibrator()==true) {
|
| 599 |
Vibration.cancel();
|
| 600 |
}
|
| 601 |
}
|
|
|
|
| 602 |
}
|
| 603 |
|
| 604 |
void detectTouchStart(DragStartDetails details){
|
client/lib/vibrate_web.dart
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import 'dart:js' as js;
|
| 2 |
+
// import 'dart:async';
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class vibration_web{
|
| 6 |
+
|
| 7 |
+
static void vibrate({List<int> pattern=const [100]}){
|
| 8 |
+
|
| 9 |
+
var jsArray = js.JsObject.jsify(pattern);
|
| 10 |
+
js.context.callMethod('vibrate', [jsArray]);
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
static void cancel(){
|
| 14 |
+
js.context.callMethod('vibrate', [0]);
|
| 15 |
+
}
|
| 16 |
+
}
|
client/web/app.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
function vibrate(flutter_value) {
|
| 2 |
+
if ("vibrate" in navigator){
|
| 3 |
+
// alert("supported");
|
| 4 |
+
navigator.vibrate(flutter_value);
|
| 5 |
+
}
|
| 6 |
+
}
|
client/web/index.html
CHANGED
|
@@ -38,6 +38,7 @@
|
|
| 38 |
</script>
|
| 39 |
<!-- This script adds the flutter initialization JS code -->
|
| 40 |
<script src="flutter.js" defer></script>
|
|
|
|
| 41 |
</head>
|
| 42 |
<body>
|
| 43 |
<script>
|
|
|
|
| 38 |
</script>
|
| 39 |
<!-- This script adds the flutter initialization JS code -->
|
| 40 |
<script src="flutter.js" defer></script>
|
| 41 |
+
<script src="app.js" defer></script> <!-- added custom js file-->
|
| 42 |
</head>
|
| 43 |
<body>
|
| 44 |
<script>
|