app/lib/view/screens/content.dart

208 lines
6 KiB
Dart
Raw Normal View History

2021-08-21 14:51:10 +02:00
import 'dart:async';
import 'dart:developer';
import 'package:tflite_audio/tflite_audio.dart';
2021-08-16 12:42:41 +02:00
import 'package:flutter/material.dart';
import 'package:easy_learn/view/widgets/custom_button.dart';
2021-08-21 14:51:10 +02:00
import 'package:easy_learn/view/widgets/score_board.dart';
2021-08-24 17:16:49 +02:00
import 'package:easy_learn/view/widgets/content_box.dart';
2021-08-24 14:28:19 +02:00
import 'package:easy_learn/config.dart' show model, label, inputType, numOfInferences, sampleRate, recordingLength, bufferSize, detectionThreshold;
2021-08-21 14:51:10 +02:00
2021-08-16 12:42:41 +02:00
class Content extends StatefulWidget {
const Content({Key? key, required this.category}) : super(key: key);
final String category;
@override
_ContentState createState() => _ContentState();
}
class _ContentState extends State<Content> {
2021-08-21 14:51:10 +02:00
final isRecording = ValueNotifier<bool>(false);
Stream<Map<dynamic, dynamic>>? result;
2021-08-24 17:16:49 +02:00
int counter = 0;
final List<dynamic> contents = [
{
"title": "yes",
},
{
"title": "no",
},
{
"title": "up",
},
{
"title": "down",
},
{
"title": "left",
},
{
"title": "right",
},
{
"title": "on",
},
{
"title": "off",
},
{
"title": "stop",
},
{
"title": "go",
},
];
void next() {
setState(() {
counter++;
});
}
void previous() {
setState(() {
counter--;
});
}
2021-08-21 14:51:10 +02:00
@override
void initState() {
super.initState();
TfliteAudio.loadModel(
// numThreads: this.numThreads,
// isAsset: this.isAsset,
model: model,
label: label,
);
}
void getResult() {
result = TfliteAudio.startAudioRecognition(
inputType: inputType,
2021-08-24 14:28:19 +02:00
numOfInferences: numOfInferences,
2021-08-21 14:51:10 +02:00
sampleRate: sampleRate,
recordingLength: recordingLength,
bufferSize: bufferSize,
2021-08-24 14:28:19 +02:00
detectionThreshold: detectionThreshold
2021-08-21 14:51:10 +02:00
);
result ?.listen(
(event) => log(event.toString())
).onDone(() => isRecording.value = false);
}
2021-08-24 17:16:49 +02:00
String showResult(AsyncSnapshot snapshot, String key) {
if (snapshot.hasData) {
return snapshot.data[key].toString();
}
return '-';
}
2021-08-21 14:51:10 +02:00
2021-08-16 12:42:41 +02:00
@override
Widget build(BuildContext context) {
return Scaffold(
2021-08-21 14:51:10 +02:00
body: StreamBuilder<Map<dynamic, dynamic>>(
stream: result,
builder: (BuildContext context, AsyncSnapshot<Map<dynamic, dynamic>> snapshot) {
dynamic scoreBoard;
switch (snapshot.connectionState) {
case ConnectionState.none:
scoreBoard = const ScoreBoard(score: "-",);
break;
case ConnectionState.waiting:
scoreBoard = const ScoreBoard(score: "-",);
break;
default:
2021-08-24 17:16:49 +02:00
scoreBoard = ScoreBoard(score: showResult(snapshot, 'recognitionResult') == contents[counter]["title"] ? showResult(snapshot, 'inferenceTime') : '-',);
2021-08-21 14:51:10 +02:00
}
return Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/content_bg.png'),
fit: BoxFit.cover
)
2021-08-16 12:42:41 +02:00
),
2021-08-21 14:51:10 +02:00
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
2021-08-16 12:42:41 +02:00
children: [
2021-08-21 14:51:10 +02:00
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
margin: const EdgeInsets.only(left: 10.0, bottom: 60.0),
child: CustomButton(name: 'close', size: 60.0, navigator: () {
Navigator.pop(context);
},)
),
scoreBoard,
Container(
margin: const EdgeInsets.only(right: 10.0, bottom: 60.0),
child: CustomButton(name: 'sound', size: 60.0, navigator: () {},)
),
],
2021-08-16 12:42:41 +02:00
),
2021-08-21 14:51:10 +02:00
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
margin: const EdgeInsets.only(left: 5.0),
2021-08-24 17:16:49 +02:00
child: CustomButton(name: 'previous', size: 100.0, navigator: () {
if (counter != 0) {
previous();
}
},)
2021-08-21 14:51:10 +02:00
),
2021-08-24 17:16:49 +02:00
SizedBox(
2021-08-21 14:51:10 +02:00
width: 180,
height: 180,
2021-08-24 17:16:49 +02:00
child: ContentBox(
imagePath: 'assets/images/content_${contents[counter]["title"]}.png',
title: contents[counter]["title"]
2021-08-21 14:51:10 +02:00
),
),
Container(
margin: const EdgeInsets.only(right: 5.0),
2021-08-24 17:16:49 +02:00
child: CustomButton(name: 'next', size: 100.0, navigator: () {
if (counter != 9) {
next();
}
})
2021-08-21 14:51:10 +02:00
)
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ValueListenableBuilder(
valueListenable: isRecording,
builder: (context, value, widget) {
if (value == false) {
return CustomButton(name: 'microphone', size: 190.0, navigator: () {
isRecording.value = true;
setState(() {
getResult();
});
});
} else {
return CustomButton(name: 'microphone', size: 190.0, navigator: () {
TfliteAudio.stopAudioRecognition();
});
}
}
)
],
2021-08-24 17:16:49 +02:00
)
2021-08-16 12:42:41 +02:00
],
),
2021-08-21 14:51:10 +02:00
);
}
2021-08-16 12:42:41 +02:00
),
);
}
}