Advertisement
etekumoses

Recording screen

May 8th, 2022
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 48.40 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'dart:typed_data';
  4.  
  5. import 'package:audioplayers/audioplayers.dart';
  6. import 'package:avatar_glow/avatar_glow.dart';
  7. import 'package:connectivity/connectivity.dart';
  8. import 'package:flutter_audio_recorder2/flutter_audio_recorder2.dart';
  9. import 'package:http/http.dart';
  10. // import 'package:just_audio/just_audio.dart';
  11. import 'package:nlp/data/model/response/report_model.dart';
  12. import 'package:nlp/helper/route_helper.dart';
  13. import 'package:nlp/provider/auth_provider.dart';
  14. import 'package:nlp/provider/sentence_provider.dart';
  15. import 'package:nlp/provider/sql_provider.dart';
  16. import 'package:nlp/utill/images.dart';
  17. import 'package:nlp/view/base/custom_app_bar.dart';
  18. import 'package:nlp/view/base/custom_snackbar.dart';
  19. import 'package:nlp/view/screens/home/home_screen.dart';
  20. import 'package:flutter/material.dart';
  21. import 'package:nlp/utill/color_resources.dart';
  22. import 'package:nlp/utill/dimensions.dart';
  23. import 'package:nlp/utill/styles.dart';
  24. import 'package:path_provider/path_provider.dart';
  25. import 'package:provider/provider.dart';
  26. import 'package:shared_preferences/shared_preferences.dart';
  27.  
  28. class RecordScreen extends StatefulWidget {
  29.   final Function onSaved;
  30.  
  31.   const RecordScreen({Key key, @required this.onSaved}) : super(key: key);
  32.  
  33.   @override
  34.   _RecordScreenState createState() => _RecordScreenState();
  35. }
  36.  
  37. enum RecordingState {
  38.   UnSet,
  39.   Set,
  40.   Recording,
  41.   Stopped,
  42. }
  43.  
  44. class _RecordScreenState extends State<RecordScreen> {
  45.   IconData _recordIcon = Icons.mic_none;
  46.   RecordingState _recordingState = RecordingState.UnSet;
  47. // Recorder properties
  48.   FlutterAudioRecorder2 audioRecorder;
  49.   final PageController _pageController = PageController();
  50.   StreamSubscription<ConnectivityResult> _onConnectivityChanged;
  51.   bool isNotConnected = false;
  52.   List<Map<String, dynamic>> _localdb = [];
  53.   List<Map<String, dynamic>> _sentencedb = [];
  54.   Directory appDirectory;
  55.   List<String> records = [];
  56.  
  57.   //
  58.   AudioPlayer audioPlayer = AudioPlayer();
  59.   AudioCache audioCache;
  60.   @override
  61.   void initState() {
  62.     super.initState();
  63.     FlutterAudioRecorder2.hasPermissions.then((hasPermision) {
  64.       if (hasPermision) {
  65.         _recordingState = RecordingState.Set;
  66.         _recordIcon = Icons.mic;
  67.       }
  68.     });
  69.  
  70.     //
  71.     _onConnectivityChanged = Connectivity()
  72.         .onConnectivityChanged
  73.         .listen((ConnectivityResult result) {
  74.       setState(() {
  75.         isNotConnected = true;
  76.       });
  77.       isNotConnected = result != ConnectivityResult.wifi &&
  78.           result != ConnectivityResult.mobile;
  79.  
  80.       print('-----------------${isNotConnected ? 'Not' : 'Yes'}');
  81.       isNotConnected
  82.           ? SizedBox()
  83.           : ScaffoldMessenger.of(context).hideCurrentSnackBar();
  84.       ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  85.         backgroundColor: isNotConnected ? Colors.red : Colors.green,
  86.         duration: Duration(seconds: isNotConnected ? 6000 : 3),
  87.         content: Text(
  88.           isNotConnected ? 'no connection' : 'connected',
  89.           textAlign: TextAlign.center,
  90.         ),
  91.       ));
  92.       setState(() {
  93.         isNotConnected = false;
  94.       });
  95.     });
  96.  
  97.     print('==========');
  98.     print(_localdb);
  99.     print('============');
  100.     _refreshJournals();
  101.     _getsentences();
  102.     getApplicationDocumentsDirectory().then((value) {
  103.       appDirectory = value;
  104.       appDirectory.list().listen((onData) {
  105.         if (onData.path.contains('.wav')) records.add(onData.path);
  106.       }).onDone(() {
  107.         records = records.reversed.toList();
  108.         setState(() {});
  109.       });
  110.     });
  111.  
  112.     print('=======');
  113.     print(records.length);
  114.     print('=======');
  115.   }
  116.  
  117.   void _refreshJournals() async {
  118.     final data = await SQLHelper.getItems();
  119.     setState(() {
  120.       _localdb = data;
  121.     });
  122.   }
  123.  
  124.   void _getsentences() async {
  125.     final data = await SQLHelper.getsentence();
  126.     setState(() {
  127.       _sentencedb = data;
  128.     });
  129.     print('====sentences===');
  130.     print(_sentencedb.length);
  131.     print('=======');
  132.   }
  133.  
  134.   final GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey<ScaffoldState>();
  135.  
  136.   String _convertToString(int number) {
  137.     switch (number) {
  138.       case 0:
  139.         return '1';
  140.       case 1:
  141.         return '2';
  142.       case 2:
  143.         return '3';
  144.       case 3:
  145.         return '4';
  146.       case 4:
  147.         return '5';
  148.     }
  149.     assert(false, 'invalid: $number');
  150.     return '';
  151.   }
  152.  
  153.   @override
  154.   Widget build(BuildContext context) {
  155.     final ScrollController _scrollController = ScrollController();
  156.  
  157.     return Consumer<SentenceProvider>(
  158.         builder: (context, sentence, child) => Scaffold(
  159.               key: _scaffoldkey,
  160.               appBar: CustomAppBar(
  161.                 title: 'Speak',
  162.                 onBackPressed: () {
  163.                   Navigator.of(context).pushReplacementNamed(RouteHelper.home,
  164.                       arguments: HomeScreen());
  165.                 },
  166.               ),
  167.               backgroundColor: Colors.white,
  168.               body: SafeArea(
  169.                 child: _sentencedb.length != 0
  170.                     ? Column(
  171.                         crossAxisAlignment: CrossAxisAlignment.start,
  172.                         children: [
  173.                             Padding(
  174.                                 padding: const EdgeInsets.symmetric(
  175.                                     horizontal: 20, vertical: 5),
  176.                                 child: Container(
  177.                                   width: double.infinity,
  178.                                   padding: EdgeInsets.all(
  179.                                       Dimensions.PADDING_SIZE_DEFAULT),
  180.                                   margin: EdgeInsets.only(
  181.                                       bottom: Dimensions.PADDING_SIZE_SMALL),
  182.                                   height:
  183.                                       MediaQuery.of(context).size.height * .3,
  184.                                   child: Column(
  185.                                       crossAxisAlignment:
  186.                                           CrossAxisAlignment.center,
  187.                                       mainAxisAlignment:
  188.                                           MainAxisAlignment.spaceEvenly,
  189.                                       children: [
  190.                                         SizedBox(height: 10),
  191.                                         Text(
  192.                                             '${_sentencedb[sentence.sentenceNumber]['text']}',
  193.                                             textAlign: TextAlign.center,
  194.                                             style: poppinsSemiBold.copyWith(
  195.                                                 fontSize: Dimensions
  196.                                                     .FONT_SIZE_DEFAULT,
  197.                                                 color:
  198.                                                     ColorResources.getTextColor(
  199.                                                         context)))
  200.                                       ]),
  201.                                 )),
  202.                             Container(
  203.                               height: 60,
  204.                               child: new GridView.builder(
  205.                                   gridDelegate:
  206.                                       SliverGridDelegateWithFixedCrossAxisCount(
  207.                                           crossAxisSpacing: 2,
  208.                                           mainAxisSpacing: 2,
  209.                                           childAspectRatio: 2,
  210.                                           crossAxisCount: 5),
  211.                                   itemCount: _sentencedb.length < 6
  212.                                       ? _sentencedb.length
  213.                                       : 5,
  214.                                   padding: EdgeInsets.symmetric(
  215.                                       horizontal:
  216.                                           Dimensions.PADDING_SIZE_DEFAULT),
  217.                                   physics: NeverScrollableScrollPhysics(),
  218.                                   shrinkWrap: true,
  219.                                   itemBuilder: (_, i) {
  220.                                     return Container(
  221.                                       height: 50,
  222.                                       width: 50,
  223.                                       decoration: BoxDecoration(
  224.                                           shape: BoxShape.circle,
  225.                                           boxShadow: [
  226.                                             BoxShadow(
  227.                                               color:
  228.                                                   ColorResources.getGreyColor(
  229.                                                           context)
  230.                                                       .withOpacity(0.8),
  231.                                               spreadRadius: 3,
  232.                                               blurRadius: 8,
  233.                                               offset: Offset(0,
  234.                                                   3), // changes position of shadow
  235.                                             ),
  236.                                           ],
  237.                                           color: sentence.sentenceNumber != i
  238.                                               ? ColorResources
  239.                                                   .getBackgroundColor(context)
  240.                                               : ColorResources
  241.                                                   .getSecondaryColor(context)),
  242.                                       child: Center(
  243.                                         child: Text(_convertToString(i),
  244.                                             textAlign: TextAlign.center,
  245.                                             style: poppinsSemiBold.copyWith(
  246.                                                 fontSize: Dimensions
  247.                                                     .FONT_SIZE_DEFAULT,
  248.                                                 color:
  249.                                                     sentence.sentenceNumber != i
  250.                                                         ? Colors.black
  251.                                                         : Colors.white)),
  252.                                       ),
  253.                                     );
  254.                                   }),
  255.                             ),
  256.                             Padding(
  257.                               padding:
  258.                                   const EdgeInsets.symmetric(horizontal: 20),
  259.                               child: Row(
  260.                                   crossAxisAlignment: CrossAxisAlignment.center,
  261.                                   children: [
  262.                                     Expanded(
  263.                                       child: Padding(
  264.                                         padding: EdgeInsets.only(
  265.                                           left: Dimensions.PADDING_SIZE_SMALL,
  266.                                         ),
  267.                                         child: Column(
  268.                                             crossAxisAlignment:
  269.                                                 CrossAxisAlignment.start,
  270.                                             mainAxisAlignment:
  271.                                                 MainAxisAlignment.start,
  272.                                             children: [
  273.                                               Row(
  274.                                                 children: [
  275.                                                   Flexible(
  276.                                                     child: Text(
  277.                                                         '"Click the record button then read the sentence aloud"',
  278.                                                         maxLines: 4,
  279.                                                         softWrap: true,
  280.                                                         textAlign:
  281.                                                             TextAlign.center,
  282.                                                         overflow: TextOverflow
  283.                                                             .ellipsis,
  284.                                                         style: poppinsMedium.copyWith(
  285.                                                             fontSize: Dimensions
  286.                                                                 .FONT_SIZE_SMALL,
  287.                                                             color: ColorResources
  288.                                                                 .getTextColor(
  289.                                                                     context))),
  290.                                                   ),
  291.                                                 ],
  292.                                               ),
  293.                                             ]),
  294.                                       ),
  295.                                     ),
  296.                                   ]),
  297.                             ),
  298.                             _sentencedb.length != 0
  299.                                 ? Align(
  300.                                     alignment: Alignment.center,
  301.                                     child: Container(
  302.                                       alignment: Alignment.center,
  303.                                       width: double.infinity,
  304.                                       child: Column(
  305.                                         mainAxisSize: MainAxisSize.min,
  306.                                         crossAxisAlignment:
  307.                                             CrossAxisAlignment.center,
  308.                                         children: <Widget>[
  309.                                           AvatarGlow(
  310.                                               animate: true,
  311.                                               glowColor: Colors.blueAccent,
  312.                                               endRadius: 80.0,
  313.                                               duration: const Duration(
  314.                                                   milliseconds: 2000),
  315.                                               repeatPauseDuration:
  316.                                                   const Duration(
  317.                                                       milliseconds: 100),
  318.                                               repeat: true,
  319.                                               child: InkWell(
  320.                                                 onTap: () async {
  321.                                                   _localdb.length == 0
  322.                                                       ? await _onRecordButtonPressed(
  323.                                                           _sentencedb[sentence
  324.                                                                   .sentenceNumber]
  325.                                                               ['id'],
  326.                                                           _sentencedb[sentence
  327.                                                                   .sentenceNumber]
  328.                                                               ['text'])
  329.                                                       : showCustomSnackBar(
  330.                                                           'Please submit your saved recordings',
  331.                                                           context,
  332.                                                           isError: true);
  333.                                                 },
  334.                                                 child: Container(
  335.                                                   height: 60,
  336.                                                   width: 60,
  337.                                                   decoration: BoxDecoration(
  338.                                                     shape: BoxShape.circle,
  339.                                                     color: ColorResources
  340.                                                         .getBackgroundColor(
  341.                                                             context),
  342.                                                     boxShadow: [
  343.                                                       BoxShadow(
  344.                                                         color: ColorResources
  345.                                                                 .getGreyColor(
  346.                                                                     context)
  347.                                                             .withOpacity(0.8),
  348.                                                         spreadRadius: 3,
  349.                                                         blurRadius: 8,
  350.                                                         offset: Offset(0,
  351.                                                             3), // changes position of shadow
  352.                                                       ),
  353.                                                     ],
  354.                                                   ),
  355.                                                   child: Icon(
  356.                                                     _recordIcon,
  357.                                                     color: ColorResources
  358.                                                         .getPrimaryColor(
  359.                                                             context),
  360.                                                   ),
  361.                                                 ),
  362.                                               )),
  363.                                           Container(height: 5),
  364.                                           Padding(
  365.                                             padding: const EdgeInsets.all(15.0),
  366.                                             child: Row(
  367.                                               crossAxisAlignment:
  368.                                                   CrossAxisAlignment.start,
  369.                                               mainAxisAlignment:
  370.                                                   MainAxisAlignment
  371.                                                       .spaceBetween,
  372.                                               children: [
  373.                                                 InkWell(
  374.                                                     onTap: () async {
  375.                                                       await showDialog(
  376.                                                           context: context,
  377.                                                           builder: (_) {
  378.                                                             return ReportDialog(
  379.                                                               sentenceid: _sentencedb[
  380.                                                                       sentence
  381.                                                                           .sentenceNumber]['id']
  382.                                                                   .toString(),
  383.                                                               sentences:
  384.                                                                   _sentencedb
  385.                                                                       .length,
  386.                                                             );
  387.                                                           });
  388.                                                       // ignore: await_only_futures
  389.                                                       await _getsentences();
  390.                                                     },
  391.                                                     child: Container(
  392.                                                       height: 48,
  393.                                                       width:
  394.                                                           MediaQuery.of(context)
  395.                                                                   .size
  396.                                                                   .width *
  397.                                                               .4,
  398.                                                       decoration: BoxDecoration(
  399.                                                         border: Border.all(
  400.                                                           width: 1,
  401.                                                           style:
  402.                                                               BorderStyle.solid,
  403.                                                           color: ColorResources
  404.                                                               .getPrimaryColor(
  405.                                                                   context),
  406.                                                         ),
  407.                                                         borderRadius:
  408.                                                             const BorderRadius
  409.                                                                 .all(
  410.                                                           Radius.circular(5.0),
  411.                                                         ),
  412.                                                       ),
  413.                                                       child: Row(
  414.                                                         crossAxisAlignment:
  415.                                                             CrossAxisAlignment
  416.                                                                 .center,
  417.                                                         mainAxisAlignment:
  418.                                                             MainAxisAlignment
  419.                                                                 .center,
  420.                                                         children: [
  421.                                                           Icon(Icons.flag,
  422.                                                               color:
  423.                                                                   Colors.black),
  424.                                                           SizedBox(
  425.                                                             width: 5,
  426.                                                           ),
  427.                                                           Text(
  428.                                                             'Report',
  429.                                                             textAlign:
  430.                                                                 TextAlign.left,
  431.                                                             style: poppinsLight.copyWith(
  432.                                                                 fontSize: Dimensions
  433.                                                                     .FONT_SIZE_LARGE,
  434.                                                                 color: ColorResources
  435.                                                                     .getPrimaryColor(
  436.                                                                         context)),
  437.                                                           ),
  438.                                                         ],
  439.                                                       ),
  440.                                                     )),
  441.                                                 InkWell(
  442.                                                     onTap: () async {
  443.                                                       // if (_sentencedb.length <
  444.                                                       //     5) {
  445.                                                       //   setState(() {
  446.                                                       //     _getsentences();
  447.                                                       //   });
  448.                                                       //   sentence.nextSentence();
  449.                                                       // } else {
  450.                                                       await sentence.skipsentence(
  451.                                                           _sentencedb[sentence
  452.                                                                   .sentenceNumber]
  453.                                                               ['id'],
  454.                                                           context);
  455.                                                       // ignore: await_only_futures
  456.                                                       // await _getsentences();
  457.                                                       // }
  458.                                                       setState(() {
  459.                                                         _getsentences();
  460.                                                       });
  461.                                                     },
  462.                                                     child: Container(
  463.                                                       height: 48,
  464.                                                       width:
  465.                                                           MediaQuery.of(context)
  466.                                                                   .size
  467.                                                                   .width *
  468.                                                               .4,
  469.                                                       decoration: BoxDecoration(
  470.                                                         border: Border.all(
  471.                                                           width: 1,
  472.                                                           style:
  473.                                                               BorderStyle.solid,
  474.                                                           color: ColorResources
  475.                                                               .getPrimaryColor(
  476.                                                                   context),
  477.                                                         ),
  478.                                                         borderRadius:
  479.                                                             const BorderRadius
  480.                                                                 .all(
  481.                                                           Radius.circular(5.0),
  482.                                                         ),
  483.                                                       ),
  484.                                                       child: !sentence.isLoading
  485.                                                           ? Row(
  486.                                                               crossAxisAlignment:
  487.                                                                   CrossAxisAlignment
  488.                                                                       .center,
  489.                                                               mainAxisAlignment:
  490.                                                                   MainAxisAlignment
  491.                                                                       .center,
  492.                                                               children: [
  493.                                                                 Icon(
  494.                                                                     Icons
  495.                                                                         .forward,
  496.                                                                     color: Colors
  497.                                                                         .black),
  498.                                                                 SizedBox(
  499.                                                                   width: 5,
  500.                                                                 ),
  501.                                                                 Text(
  502.                                                                   'Skip',
  503.                                                                   textAlign:
  504.                                                                       TextAlign
  505.                                                                           .left,
  506.                                                                   style: poppinsLight.copyWith(
  507.                                                                       fontSize:
  508.                                                                           Dimensions
  509.                                                                               .FONT_SIZE_LARGE,
  510.                                                                       color: ColorResources
  511.                                                                           .getPrimaryColor(
  512.                                                                               context)),
  513.                                                                 ),
  514.                                                               ],
  515.                                                             )
  516.                                                           : Center(
  517.                                                               child:
  518.                                                                   CircularProgressIndicator(
  519.                                                               valueColor: AlwaysStoppedAnimation<
  520.                                                                   Color>(Theme.of(
  521.                                                                       context)
  522.                                                                   .primaryColor),
  523.                                                             )),
  524.                                                     ))
  525.                                               ],
  526.                                             ),
  527.                                           ),
  528.                                         ],
  529.                                       ),
  530.                                     ),
  531.                                   )
  532.                                 : SizedBox()
  533.                           ])
  534.                     : Column(
  535.                         crossAxisAlignment: CrossAxisAlignment.start,
  536.                         children: [
  537.                           Align(
  538.                             alignment: Alignment.center,
  539.                             child: Container(
  540.                               color: Colors.white,
  541.                               alignment: Alignment.center,
  542.                               width: double.infinity,
  543.                               height: MediaQuery.of(context).size.height * .5,
  544.                               child: Column(
  545.                                 mainAxisSize: MainAxisSize.min,
  546.                                 crossAxisAlignment: CrossAxisAlignment.center,
  547.                                 children: <Widget>[
  548.                                   Image.asset(
  549.                                     Images.empty,
  550.                                     scale: 8,
  551.                                     color: ColorResources.getHintColor(context),
  552.                                   ),
  553.                                   Container(height: 5),
  554.                                   Padding(
  555.                                     padding: const EdgeInsets.all(15.0),
  556.                                     child: Text('Oops!!',
  557.                                         textAlign: TextAlign.center,
  558.                                         style: poppinsBold.copyWith(
  559.                                             fontSize: 14, color: Colors.black)),
  560.                                   ),
  561.                                   Padding(
  562.                                       padding: const EdgeInsets.all(15.0),
  563.                                       child: Text(
  564.                                         'There are no sentences, Please try again later',
  565.                                         textAlign: TextAlign.center,
  566.                                         style: poppinsRegular.copyWith(
  567.                                             fontSize: 14,
  568.                                             color: ColorResources.getHintColor(
  569.                                                 context)),
  570.                                       )),
  571.                                 ],
  572.                               ),
  573.                             ),
  574.                           )
  575.                         ],
  576.                       ),
  577.               ),
  578.             ));
  579.   }
  580.  
  581.   //
  582.  
  583.   processingDialog(message) {
  584.     return showDialog(
  585.         barrierDismissible: false,
  586.         context: context,
  587.         builder: (context) {
  588.           return AlertDialog(
  589.             content: Row(
  590.               mainAxisSize: MainAxisSize.min,
  591.               children: [
  592.                 CircularProgressIndicator(
  593.                   strokeWidth: 2,
  594.                 ),
  595.                 SizedBox(
  596.                   width: 10,
  597.                 ),
  598.                 Expanded(
  599.                   child: Text(
  600.                     message,
  601.                     style: poppinsRegular.copyWith(
  602.                         fontSize: Dimensions.FONT_SIZE_SMALL,
  603.                         color: ColorResources.getHintColor(context)),
  604.                   ),
  605.                 )
  606.               ],
  607.             ),
  608.           );
  609.         });
  610.   }
  611.  
  612. //
  613.   Future<void> _onRecordButtonPressed(int sentenceid, String sentence) async {
  614.     switch (_recordingState) {
  615.       case RecordingState.Set:
  616.         await _recordVoice(sentenceid, sentence);
  617.         break;
  618.  
  619.       case RecordingState.Recording:
  620.         await _stopRecording(sentenceid, sentence);
  621.         _recordingState = RecordingState.Stopped;
  622.         _recordIcon = Icons.mic;
  623.         break;
  624.  
  625.       case RecordingState.Stopped:
  626.         await _recordVoice(sentenceid, sentence);
  627.         break;
  628.  
  629.       case RecordingState.UnSet:
  630.         ScaffoldMessenger.of(context).hideCurrentSnackBar();
  631.  
  632.         ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  633.           content: Text('Please allow recording from settings.'),
  634.         ));
  635.         break;
  636.     }
  637.   }
  638.  
  639.   String audiopathurl;
  640.  
  641.   _initRecorder(int id, String sentence) async {
  642.     SharedPreferences preferences = await SharedPreferences.getInstance();
  643.     String lan = preferences.getString('lang');
  644.     print('======');
  645.     print(lan);
  646.     print('======');
  647.     Directory appDirectory = await getApplicationDocumentsDirectory();
  648.     String filePath =
  649.         appDirectory.path + '/' + lan + '_' + id.toString() + '.wav';
  650.     audioRecorder =
  651.         FlutterAudioRecorder2(filePath, audioFormat: AudioFormat.WAV);
  652.  
  653.     await audioRecorder.initialized;
  654.     setState(() {
  655.       audiopathurl = filePath;
  656.     });
  657.     print(audiopathurl);
  658.   }
  659.  
  660.   _startRecording() async {
  661.     await audioRecorder.start();
  662.   }
  663.  
  664.   // ignore: missing_return
  665.   Future<int> deleteFile(String filepath) async {
  666.     try {
  667.       final file = File(filepath);
  668.       await file.delete();
  669.     } catch (e) {
  670.       return 0;
  671.     }
  672.   }
  673.  
  674.   _stopRecording(int id, String sentence) async {
  675.     SharedPreferences preferences = await SharedPreferences.getInstance();
  676.     int currentindex = preferences.setInt('currentindex', id) as int;
  677.     print('======');
  678.     print(currentindex);
  679.     print('======');
  680.     var result = await audioRecorder.stop();
  681.     var p = result.duration.inSeconds;
  682.     print("Stop recording: $p");
  683.     print('=======seconds=========');
  684.     if (result.duration.inSeconds < 1) {
  685.       showCustomSnackBar('Try recording again!', context);
  686.       deleteFile(audiopathurl);
  687.     } else {
  688.       SQLHelper.createItem(id, sentence, audiopathurl);
  689.       print('=================');
  690.       print(audiopathurl);
  691.       print('=============');
  692.       if (Provider.of<SentenceProvider>(context, listen: false).sentenceNumber <
  693.           _sentencedb.length - 1) {
  694.         // ignore: unnecessary_statements
  695.         Provider.of<SentenceProvider>(context, listen: false).sentenceNumber +
  696.             1;
  697.         Provider.of<SentenceProvider>(context, listen: false).nextSentence();
  698.       } else if (isNotConnected == true) {
  699.         print(
  700.             '${Provider.of<SentenceProvider>(context, listen: false).sentenceNumber}');
  701.  
  702.         processingDialog('Please wait while submitting clips');
  703.         setState(() {
  704.           _refreshJournals();
  705.         });
  706.         Provider.of<SentenceProvider>(context, listen: false)
  707.             .sendAudios(
  708.               _localdb,
  709.               Provider.of<AuthProvider>(context, listen: false).getUserToken(),
  710.             )
  711.             .then((value) => SQLHelper.deleteItems().then((value) => showDialog(
  712.                 context: context,
  713.                 builder: (_) {
  714.                   return Dialog(
  715.                     shape: RoundedRectangleBorder(
  716.                       borderRadius: BorderRadius.circular(10),
  717.                     ),
  718.                     elevation: 0,
  719.                     backgroundColor: Colors.transparent,
  720.                     child: Stack(
  721.                       children: <Widget>[
  722.                         Container(
  723.                           padding: EdgeInsets.only(
  724.                               left: 20, top: 40, right: 20, bottom: 20),
  725.                           margin: EdgeInsets.only(top: 20),
  726.                           decoration: BoxDecoration(
  727.                               shape: BoxShape.rectangle,
  728.                               color: Colors.white,
  729.                               borderRadius: BorderRadius.circular(10),
  730.                               boxShadow: [
  731.                                 BoxShadow(
  732.                                     color: Colors.black,
  733.                                     offset: Offset(0, 10),
  734.                                     blurRadius: 10),
  735.                               ]),
  736.                           child: Column(
  737.                             mainAxisSize: MainAxisSize.min,
  738.                             children: <Widget>[
  739.                               Text(
  740.                                 'Success',
  741.                                 style: TextStyle(
  742.                                     fontSize: 22, fontWeight: FontWeight.w600),
  743.                               ),
  744.                               SizedBox(
  745.                                 height: 15,
  746.                               ),
  747.                               Text(
  748.                                   'Thank you for contributing ,your clips have been captured',
  749.                                   textAlign: TextAlign.center,
  750.                                   style: poppinsRegular.copyWith(
  751.                                       fontSize: Dimensions.FONT_SIZE_SMALL,
  752.                                       color: ColorResources.getHintColor(
  753.                                           context))),
  754.                               SizedBox(
  755.                                 height: 22,
  756.                               ),
  757.                               InkWell(
  758.                                 onTap: () {
  759.                                   Navigator.of(context).pushReplacementNamed(
  760.                                       RouteHelper.home,
  761.                                       arguments: HomeScreen());
  762.                                 },
  763.                                 child: Align(
  764.                                     alignment: Alignment.center,
  765.                                     child: Container(
  766.                                       width: MediaQuery.of(context).size.width *
  767.                                           .5,
  768.                                       height: 50,
  769.                                       decoration: new BoxDecoration(
  770.                                         color: Theme.of(context).primaryColor,
  771.                                         shape: BoxShape.rectangle,
  772.                                         borderRadius: BorderRadius.circular(5),
  773.                                       ),
  774.                                       child: Center(
  775.                                         child: Text(
  776.                                           'ok',
  777.                                           style: poppinsMedium.copyWith(
  778.                                               fontSize:
  779.                                                   Dimensions.FONT_SIZE_DEFAULT,
  780.                                               color: Colors.white),
  781.                                         ),
  782.                                       ),
  783.                                     )),
  784.                               ),
  785.                             ],
  786.                           ),
  787.                         ),
  788.                         Positioned(
  789.                           left: 20,
  790.                           right: 20,
  791.                           child: CircleAvatar(
  792.                             backgroundColor: Theme.of(context).primaryColor,
  793.                             radius: 30,
  794.                             child: ClipRRect(
  795.                                 borderRadius:
  796.                                     BorderRadius.all(Radius.circular(20)),
  797.                                 child: Icon(Icons.check,
  798.                                     size: 30, color: Colors.white)),
  799.                           ),
  800.                         ),
  801.                       ],
  802.                     ),
  803.                   );
  804.                 })));
  805.       } else {
  806.         Navigator.pushNamedAndRemoveUntil(
  807.             context, RouteHelper.getRecordingsRoute(), (route) => false);
  808.       }
  809.     }
  810.   }
  811.  
  812.   Future<void> _recordVoice(int id, String sentence) async {
  813.     final hasPermission = await FlutterAudioRecorder2.hasPermissions;
  814.     if (hasPermission ?? false) {
  815.       await _initRecorder(id, sentence);
  816.  
  817.       await _startRecording();
  818.       _recordingState = RecordingState.Recording;
  819.       _recordIcon = Icons.stop;
  820.     } else {
  821.       // ScaffoldMessenger.of(context).hideCurrentSnackBar();
  822.       // ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  823.       //   content: Text('Please allow recording from settings.'),
  824.       // ));
  825.       showCustomSnackBar('Please allow recording from settings', context);
  826.     }
  827.   }
  828. }
  829.  
  830. class ReportDialog extends StatefulWidget {
  831.   final String sentenceid;
  832.   final int sentences;
  833.   const ReportDialog({Key key, this.sentenceid, this.sentences})
  834.       : super(key: key);
  835.  
  836.   @override
  837.   State<ReportDialog> createState() => _ReportDialogState();
  838. }
  839.  
  840. class _ReportDialogState extends State<ReportDialog> {
  841.   List<String> titledata = [
  842.     "Offensive language",
  843.     "Grammatical / spelling error",
  844.     "Different language",
  845.     "Difficult to pronounce",
  846.     "Others"
  847.   ];
  848.   List<String> subtitledata = [
  849.     "The sentence has disrespectful or offensive language.",
  850.     "The sentence has a grammatical or spelling error.",
  851.     "It is written in a language different than what I’m speaking.",
  852.     "It contains words or phrases that are hard to read or pronounce.",
  853.     " "
  854.   ];
  855.  
  856.   List userChecked = [];
  857.  
  858.   //
  859.   void _onSelected(bool selected, String dataName) {
  860.     if (selected == true) {
  861.       setState(() {
  862.         userChecked.add(dataName);
  863.       });
  864.     } else {
  865.       setState(() {
  866.         userChecked.remove(dataName);
  867.       });
  868.     }
  869.   }
  870.  
  871. // test stepper
  872.  
  873.   @override
  874.   Widget build(BuildContext context) {
  875.     return Consumer<SentenceProvider>(
  876.       builder: (_, sentenceProvider, child) => Dialog(
  877.           shape: RoundedRectangleBorder(
  878.             borderRadius: BorderRadius.circular(10),
  879.           ),
  880.           elevation: 0,
  881.           backgroundColor: Colors.transparent,
  882.           child: Container(
  883.             height: MediaQuery.of(context).size.height * .8,
  884.             padding: EdgeInsets.only(left: 20, top: 40, right: 20, bottom: 20),
  885.             margin: EdgeInsets.only(top: 20),
  886.             decoration: BoxDecoration(
  887.                 shape: BoxShape.rectangle,
  888.                 color: Colors.white,
  889.                 borderRadius: BorderRadius.circular(10),
  890.                 boxShadow: [
  891.                   BoxShadow(
  892.                       color: Colors.black,
  893.                       offset: Offset(0, 10),
  894.                       blurRadius: 10),
  895.                 ]),
  896.             child: Column(
  897.               mainAxisSize: MainAxisSize.min,
  898.               children: <Widget>[
  899.                 Text(
  900.                   'Submit a report',
  901.                   style: TextStyle(fontSize: 22, fontWeight: FontWeight.w600),
  902.                 ),
  903.                 SizedBox(
  904.                   height: 15,
  905.                 ),
  906.                 Text('What issues are you experiencing with this sentence?',
  907.                     textAlign: TextAlign.center,
  908.                     style: poppinsRegular.copyWith(
  909.                         fontSize: Dimensions.FONT_SIZE_SMALL,
  910.                         color: ColorResources.getHintColor(context))),
  911.                 SizedBox(height: Dimensions.FONT_SIZE_EXTRA_SMALL),
  912.                 Divider(),
  913.                 SizedBox(height: Dimensions.FONT_SIZE_EXTRA_SMALL),
  914.                 Container(
  915.                     height: MediaQuery.of(context).size.height * .4,
  916.                     child: new ListView.builder(
  917.                         itemCount: titledata.length,
  918.                         itemBuilder: (context, i) {
  919.                           return ListTile(
  920.                               title: Text(titledata[i],
  921.                                   style: poppinsSemiBold.copyWith(
  922.                                       fontSize: Dimensions.FONT_SIZE_SMALL,
  923.                                       color: ColorResources.getHintColor(
  924.                                           context))),
  925.                               subtitle: Text(subtitledata[i],
  926.                                   style: poppinsRegular.copyWith(
  927.                                       fontSize:
  928.                                           Dimensions.FONT_SIZE_EXTRA_SMALL,
  929.                                       color: ColorResources.getHintColor(
  930.                                           context))),
  931.                               trailing: Checkbox(
  932.                                 value: userChecked.contains(titledata[i]),
  933.                                 activeColor:
  934.                                     ColorResources.getSecondaryColor(context),
  935.                                 onChanged: (val) {
  936.                                   setState(() {
  937.                                     _onSelected(val, titledata[i]);
  938.                                   });
  939.                                   print(titledata[i]);
  940.                                 },
  941.                               ));
  942.                         })),
  943.                 SizedBox(height: Dimensions.FONT_SIZE_LARGE),
  944.                 !sentenceProvider.isLoading
  945.                     ? InkWell(
  946.                         onTap: () async {
  947.                           String commaSeparated = userChecked.join(', ');
  948.                           if (userChecked.isEmpty) {
  949.                             showCustomSnackBar(
  950.                                 'Select atleast one issue', context);
  951.                           } else {
  952.                             ReportModel reportModel = ReportModel(
  953.                                 id: widget.sentenceid, report: commaSeparated);
  954.                             print('=====issues====');
  955.                             print(reportModel.toJson());
  956.                             await sentenceProvider
  957.                                 .hasIssue(
  958.                                     reportModel,
  959.                                     Provider.of<AuthProvider>(context,
  960.                                             listen: false)
  961.                                         .getUserToken(),
  962.                                     context)
  963.                                 .then((value) {
  964.                               Navigator.pop(context);
  965.                               setState(() {});
  966.                             });
  967.                           }
  968.                         },
  969.                         child: Align(
  970.                             alignment: Alignment.center,
  971.                             child: Container(
  972.                               width: MediaQuery.of(context).size.width * .4,
  973.                               height: 40,
  974.                               decoration: new BoxDecoration(
  975.                                 color: userChecked.isEmpty
  976.                                     ? Colors.grey
  977.                                     : Theme.of(context).primaryColor,
  978.                                 shape: BoxShape.rectangle,
  979.                                 borderRadius: BorderRadius.circular(5),
  980.                               ),
  981.                               child: Center(
  982.                                 child: Text(
  983.                                   'Report',
  984.                                   style: poppinsMedium.copyWith(
  985.                                       fontSize: Dimensions.FONT_SIZE_DEFAULT,
  986.                                       color: Colors.white),
  987.                                 ),
  988.                               ),
  989.                             )),
  990.                       )
  991.                     : Center(
  992.                         child: CircularProgressIndicator(
  993.                         valueColor: new AlwaysStoppedAnimation<Color>(
  994.                             Theme.of(context).primaryColor),
  995.                       )),
  996.               ],
  997.             ),
  998.           )),
  999.     );
  1000.   }
  1001. }
  1002.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement