Skip to content

Commit

Permalink
Add haptics and reduce slide distance
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodepapaya committed Sep 26, 2021
1 parent 2fed8ff commit d8893e6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
3 changes: 1 addition & 2 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.thecodepapaya.audio_chat">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:label="audio_chat"
android:icon="@mipmap/ic_launcher">
Expand Down
2 changes: 1 addition & 1 deletion lib/src/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class _HomePageState extends State<HomePage>
super.initState();
controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
duration: const Duration(milliseconds: 600),
);
}

Expand Down
34 changes: 25 additions & 9 deletions lib/src/widgets/record_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:audio_chat/src/audio_state.dart';
import 'package:audio_chat/src/globals.dart';
import 'package:audio_chat/src/widgets/flow_shader.dart';
import 'package:flutter/material.dart';
import 'package:flutter_vibrate/flutter_vibrate.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:record/record.dart';

Expand Down Expand Up @@ -60,15 +61,15 @@ class _RecordButtonState extends State<RecordButton> {
.animate(
CurvedAnimation(
parent: widget.controller,
curve: const Interval(0.4, 1, curve: Curves.easeIn),
curve: const Interval(0.2, 1, curve: Curves.easeIn),
),
);
lockerAnimation =
Tween<double>(begin: lockerHeight + Globals.defaultPadding, end: 0)
.animate(
CurvedAnimation(
parent: widget.controller,
curve: const Interval(0.2, 0.8, curve: Curves.elasticInOut),
curve: const Interval(0.2, 1, curve: Curves.easeIn),
),
);
}
Expand Down Expand Up @@ -143,8 +144,14 @@ class _RecordButtonState extends State<RecordButton> {
mainAxisSize: MainAxisSize.max,
children: [
Text(recordDuration),
const SizedBox(width: size),
FlowShader(
child: const Text("Slide to cancel"),
child: Row(
children: const [
Icon(Icons.keyboard_arrow_left),
Text("Slide to cancel")
],
),
duration: const Duration(seconds: 3),
flowColors: const [Colors.white, Colors.grey],
),
Expand All @@ -171,11 +178,13 @@ class _RecordButtonState extends State<RecordButton> {
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () async {
var filePath = await Record().stop();
Vibrate.feedback(FeedbackType.success);
timer?.cancel();
timer = null;
startTime = null;
recordDuration = "00:00";

var filePath = await Record().stop();
AudioState.files.add(filePath!);
Globals.audioListKey.currentState!
.insertItem(AudioState.files.length - 1);
Expand Down Expand Up @@ -232,7 +241,9 @@ class _RecordButtonState extends State<RecordButton> {
debugPrint("onLongPressEnd");
widget.controller.reverse();

if (isCancelled(details.localPosition)) {
if (isCancelled(details.localPosition, context)) {
Vibrate.feedback(FeedbackType.heavy);

timer?.cancel();
timer = null;
startTime = null;
Expand All @@ -244,11 +255,15 @@ class _RecordButtonState extends State<RecordButton> {
File(filePath!).delete();
debugPrint("Deleted $filePath");
} else if (checkIsLocked(details.localPosition)) {
Vibrate.feedback(FeedbackType.heavy);
debugPrint("Locked recording");
debugPrint(details.localPosition.dy.toString());
setState(() {
isLocked = true;
});
} else {
Vibrate.feedback(FeedbackType.success);

timer?.cancel();
timer = null;
startTime = null;
Expand All @@ -267,6 +282,7 @@ class _RecordButtonState extends State<RecordButton> {
},
onLongPress: () async {
debugPrint("onLongPress");
Vibrate.feedback(FeedbackType.success);
if (await Record().hasPermission()) {
record = Record();
await record.start(
Expand All @@ -277,7 +293,7 @@ class _RecordButtonState extends State<RecordButton> {
samplingRate: 44100,
);
startTime = DateTime.now();
timer = Timer.periodic(const Duration(seconds: 1), (timer) {
timer = Timer.periodic(const Duration(seconds: 1), (_) {
final minDur = DateTime.now().difference(startTime!).inMinutes;
final secDur = DateTime.now().difference(startTime!).inSeconds % 60;
String min = minDur < 10 ? "0$minDur" : minDur.toString();
Expand All @@ -292,10 +308,10 @@ class _RecordButtonState extends State<RecordButton> {
}

bool checkIsLocked(Offset offset) {
return (offset.dy < -85 && offset.dy > -135);
return (offset.dy < -35);
}

bool isCancelled(Offset offset) {
return (offset.dx < -200);
bool isCancelled(Offset offset, BuildContext context) {
return (offset.dx < -(MediaQuery.of(context).size.width * 0.3));
}
}
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_vibrate:
dependency: "direct main"
description:
name: flutter_vibrate
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
flutter_web_plugins:
dependency: transitive
description: flutter
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies:
permission_handler: ^8.1.6
path_provider: ^2.0.5
record: ^3.0.0
flutter_vibrate: ^1.1.0

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down

0 comments on commit d8893e6

Please sign in to comment.