aicoco icon indicating copy to clipboard operation
aicoco copied to clipboard

How can i add tts feature to my below flutter code , so that once user selects data on pdf it starts reading it aloud ??? <flutter>

Open alex5055 opened this issue 5 years ago • 1 comments


I am trying to add voice for the selected text in pdf but not able to detect in text of my pdf.


> Dart code

import 'dart:io';

import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:path_provider/path_provider.dart'; import 'package:flutter_pdfview/flutter_pdfview.dart'; import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); }

class _MyAppState extends State<MyApp> { String assetPDFPath = ""; String urlPDFPath = "";

@override void initState() { super.initState();

getFileFromAsset("assets/mypdf.pdf").then((f) {
  setState(() {
    assetPDFPath = f.path;
    print(assetPDFPath);
  });
});

getFileFromUrl("http://www.pdf995.com/samples/pdf.pdf").then((f) {
  setState(() {
    urlPDFPath = f.path;
    print(urlPDFPath);
  });
});

}

Future<File> getFileFromAsset(String asset) async { try { var data = await rootBundle.load(asset); var bytes = data.buffer.asUint8List(); var dir = await getApplicationDocumentsDirectory(); File file = File("${dir.path}/mypdf.pdf");

  File assetFile = await file.writeAsBytes(bytes);
  return assetFile;
} catch (e) {
  throw Exception("Error opening asset file");
}

}

Future<File> getFileFromUrl(String url) async { try { var data = await http.get(url); var bytes = data.bodyBytes; var dir = await getApplicationDocumentsDirectory(); File file = File("${dir.path}/mypdfonline.pdf");

  File urlFile = await file.writeAsBytes(bytes);
  return urlFile;
} catch (e) {
  throw Exception("Error opening url file");
}

}

@override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( appBar: AppBar( title: Text("Flutter PDF Tutorial"), ), body: Center( child: Builder( builder: (context) => Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ RaisedButton( color: Colors.amber, child: Text("Open from URL"), onPressed: () { if (urlPDFPath != null) { Navigator.push( context, MaterialPageRoute( builder: (context) => PdfViewPage(path: assetPDFPath))); } }, ), SizedBox( height: 20, ), RaisedButton( color: Colors.cyan, child: Text("Open from Asset"), onPressed: () { if (assetPDFPath != null) { Navigator.push( context, MaterialPageRoute( builder: (context) => PdfViewPage(path: assetPDFPath))); } }, ) ], ), ), ), ), ); } }

class PdfViewPage extends StatefulWidget { final String path;

const PdfViewPage({Key key, this.path}) : super(key: key); @override _PdfViewPageState createState() => _PdfViewPageState(); }

class _PdfViewPageState extends State<PdfViewPage> { int _totalPages = 0; int _currentPage = 0; bool pdfReady = false; PDFViewController _pdfViewController;

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("My Document"), ), body: Stack( children: <Widget>[ PDFView( filePath: widget.path, autoSpacing: true, enableSwipe: true, pageSnap: true, swipeHorizontal: true, nightMode: false, onError: (e) { print(e); }, onRender: (_pages) { setState(() { _totalPages = _pages; pdfReady = true; }); }, onViewCreated: (PDFViewController vc) { _pdfViewController = vc; }, onPageChanged: (int page, int total) { setState(() {}); }, onPageError: (page, e) {}, ), !pdfReady ? Center( child: CircularProgressIndicator(), ) : Offstage() ], ), floatingActionButton: Row( mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[ _currentPage > 0 ? FloatingActionButton.extended( backgroundColor: Colors.red, label: Text("Go to ${_currentPage - 1}"), onPressed: () { _currentPage -= 1; _pdfViewController.setPage(_currentPage); }, ) : Offstage(), _currentPage+1 < _totalPages ? FloatingActionButton.extended( backgroundColor: Colors.green, label: Text("Go to ${_currentPage + 1}"), onPressed: () { _currentPage += 1; _pdfViewController.setPage(_currentPage); }, ) : Offstage(), ], ), ); } }

alex5055 avatar May 24 '20 19:05 alex5055