fluffychat/lib/components/dialogs/recording_dialog.dart

114 lines
2.8 KiB
Dart
Raw Normal View History

2020-03-15 11:27:51 +01:00
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-03-15 11:27:51 +01:00
import 'package:flutter_sound/flutter_sound.dart';
import 'package:intl/intl.dart';
class RecordingDialog extends StatefulWidget {
final Function onFinished;
2021-01-19 15:46:43 +01:00
final L10n l10n;
2020-03-15 11:27:51 +01:00
2021-01-19 15:46:43 +01:00
const RecordingDialog({
this.onFinished,
@required this.l10n,
Key key,
}) : super(key: key);
2020-03-15 11:27:51 +01:00
@override
_RecordingDialogState createState() => _RecordingDialogState();
}
class _RecordingDialogState extends State<RecordingDialog> {
FlutterSound flutterSound = FlutterSound();
2020-05-13 15:58:59 +02:00
String time = '00:00:00';
2020-03-15 11:27:51 +01:00
StreamSubscription _recorderSubscription;
2020-03-15 11:49:59 +01:00
bool error = false;
2020-03-15 11:27:51 +01:00
void startRecording() async {
2020-03-15 11:49:59 +01:00
try {
await flutterSound.startRecorder(
codec: t_CODEC.CODEC_AAC,
);
_recorderSubscription = flutterSound.onRecorderStateChanged.listen((e) {
2020-05-13 15:58:59 +02:00
var date =
2020-03-15 11:49:59 +01:00
DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
setState(() => time = DateFormat('mm:ss:SS', 'en_US').format(date));
});
} catch (e) {
error = true;
}
2020-03-15 11:27:51 +01:00
}
@override
void initState() {
super.initState();
startRecording();
}
@override
void dispose() {
if (flutterSound.isRecording) flutterSound.stopRecorder();
_recorderSubscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
2020-03-15 11:49:59 +01:00
if (error) {
Timer(Duration(seconds: 1), () {
Navigator.of(context).pop();
});
}
2020-03-15 11:27:51 +01:00
return AlertDialog(
content: Row(
children: <Widget>[
CircleAvatar(
backgroundColor: Colors.red,
radius: 8,
),
SizedBox(width: 8),
Expanded(
child: Text(
2021-01-19 15:46:43 +01:00
'${widget.l10n.recording}: $time',
2020-03-15 11:27:51 +01:00
style: TextStyle(
fontSize: 18,
),
),
),
],
),
actions: <Widget>[
FlatButton(
child: Text(
2021-01-19 15:46:43 +01:00
widget.l10n.cancel.toUpperCase(),
2020-03-15 11:27:51 +01:00
style: TextStyle(
2020-05-06 18:43:30 +02:00
color: Theme.of(context).textTheme.bodyText2.color.withAlpha(150),
2020-03-15 11:27:51 +01:00
),
),
onPressed: () => Navigator.of(context).pop(),
),
FlatButton(
child: Row(
children: <Widget>[
2021-01-19 15:46:43 +01:00
Text(widget.l10n.send.toUpperCase()),
2020-03-15 11:27:51 +01:00
SizedBox(width: 4),
2020-12-06 10:31:35 +01:00
Icon(Icons.send_outlined, size: 15),
2020-03-15 11:27:51 +01:00
],
),
onPressed: () async {
await _recorderSubscription?.cancel();
2020-05-13 15:58:59 +02:00
final result = await flutterSound.stopRecorder();
2020-03-15 11:27:51 +01:00
if (widget.onFinished != null) {
widget.onFinished(result);
}
Navigator.of(context).pop();
},
),
],
);
}
}