Skip to content

Commit

Permalink
Add bonus feature: swipe to lock recording
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodepapaya committed Sep 25, 2021
1 parent 92f0b4f commit a199c2e
Showing 1 changed file with 69 additions and 8 deletions.
77 changes: 69 additions & 8 deletions lib/src/widgets/record_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class _RecordButtonState extends State<RecordButton> {
Timer? timer;
String recordDuration = "00:00";
late Record record;
bool isLocked = false;

@override
void initState() {
Expand Down Expand Up @@ -75,6 +76,7 @@ class _RecordButtonState extends State<RecordButton> {
@override
void dispose() {
record.dispose();
timer?.cancel();
super.dispose();
}

Expand All @@ -84,8 +86,9 @@ class _RecordButtonState extends State<RecordButton> {
clipBehavior: Clip.none,
children: [
lockSlider(),
timerSlider(context),
cancelSlider(),
audioButton(),
if (isLocked) timerLocked(),
],
);
}
Expand Down Expand Up @@ -122,7 +125,7 @@ class _RecordButtonState extends State<RecordButton> {
);
}

Widget timerSlider(BuildContext context) {
Widget cancelSlider() {
return Positioned(
right: -timerAnimation.value,
child: Container(
Expand Down Expand Up @@ -152,6 +155,58 @@ class _RecordButtonState extends State<RecordButton> {
);
}

Widget timerLocked() {
return Positioned(
right: 0,
child: Container(
height: size,
width: timerWidth,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(Globals.borderRadius),
color: Colors.black,
),
child: Padding(
padding: const EdgeInsets.only(left: 15, right: 25),
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () async {
var filePath = await Record().stop();
AudioState.files.add(filePath!);
Globals.audioListKey.currentState!
.insertItem(AudioState.files.length - 1);
debugPrint(filePath);
setState(() {
isLocked = false;
});
startTime = null;
recordDuration = "00:00";
timer?.cancel();
},
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: [
Text(recordDuration),
FlowShader(
child: const Text("Tap lock to stop"),
duration: const Duration(seconds: 3),
flowColors: const [Colors.white, Colors.grey],
),
const Center(
child: FaIcon(
FontAwesomeIcons.lock,
size: 18,
color: Colors.green,
),
),
],
),
),
),
),
);
}

Widget audioButton() {
return GestureDetector(
child: Transform.scale(
Expand All @@ -174,19 +229,25 @@ class _RecordButtonState extends State<RecordButton> {
onLongPressEnd: (details) async {
debugPrint("onLongPressEnd");
widget.controller.reverse();
debugPrint(details.localPosition.toString());
startTime = null;
recordDuration = "00:00";
timer?.cancel();

if (isCancelled(details.localPosition)) {
startTime = null;
recordDuration = "00:00";
timer?.cancel();
debugPrint("Cancelled recording");
var filePath = await record.stop();
debugPrint(filePath);
File(filePath!).delete();
debugPrint("Deleted $filePath");
} else if (isLocked(details.localPosition)) {
} else if (checkIsLocked(details.localPosition)) {
debugPrint("Locked recording");
setState(() {
isLocked = true;
});
} else {
startTime = null;
recordDuration = "00:00";
timer?.cancel();
var filePath = await Record().stop();
AudioState.files.add(filePath!);
Globals.audioListKey.currentState!
Expand Down Expand Up @@ -224,7 +285,7 @@ class _RecordButtonState extends State<RecordButton> {
);
}

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

Expand Down

0 comments on commit a199c2e

Please sign in to comment.