Flutter Templates
How to Build an AI Voice Translator App Using Flutter and Back4App
30 min
introduction in this tutorial, you will learn how to build an ai voice translator app using flutter as the frontend framework and back4app as the backend service the app will allow users to translate speech, text, and images across multiple languages it will feature ai powered translation, optical character recognition (ocr), and speech recognition functionalities by the end of this tutorial, you'll have built an app that can handle real time translations and store history on back4app for future use prerequisites to complete this tutorial, you will need a back4app account sign up for a free account at back4app https //www back4app com/ flutter sdk installed on your local machine follow the official flutter installation guide https //flutter dev/docs/get started/install basic knowledge of dart and flutter if you're new to flutter, review the flutter documentation https //flutter dev/docs before proceeding an account with a translation api provider like google cloud translation or deepl familiarity with rest apis and asynchronous programming in dart step 1 — setting up your back4app backend in this step, you will set up a back4app application, define the data models, and configure the backend to store translation history, languages, and user data 1 1 create a new application on back4app log in to your back4app dashboard https //dashboard back4app com/ click on "create new app" enter an app name (e g , "ai voice translator") and select your app icon choose your server location if prompted click "create" 1 2 retrieve application keys navigate to app settings > security & keys in your app's dashboard note down the application id and client key these will be required for the flutter app configuration 1 3 define your data models we will create the following classes in back4app user (default) translation language phrase dictionaryentry 1 3 1 create the translation class go to database > browser click "create a class" choose "custom" and name it translation click "create class" add the following columns to translation user (pointer< user>) points to the user object sourcelanguage (string) source language code targetlanguage (string) target language code sourcetext (string) the original text to translate translatedtext (string) the translated text translationtype (string) type of translation ( voice , text , image ) timestamp (datetime) 1 3 2 create the language class repeat the steps to create a new class named language add the following columns to language name (string) the name of the language (e g , "english") code (string) iso code of the language (e g , "en") flagiconurl (string) url of the flag image representing the language 1 3 3 create the phrase class create a class named phrase add the following columns to phrase category (string) the category of the phrase (e g , "greetings") sourcelanguage (string) source language code targetlanguage (string) target language code sourcetext (string) the original phrase translatedtext (string) the translated phrase 1 3 4 create the dictionaryentry class create a class named dictionaryentry add the following columns to dictionaryentry word (string) the word being defined language (string) the language of the word definition (string) the word’s definition examples (array) example sentences using the word 1 4 set class level permissions ensure that only authenticated users can access their own data for each class, go to security > class level permissions (clp) set the clp to allow read/write access only to authenticated users step 2 — initializing your flutter project in this step, you'll set up a flutter project and configure it to connect to back4app 2 1 create a new flutter project open your terminal and run flutter create ai voice translator navigate to the project directory cd ai voice translator 2 2 add required dependencies open pubspec yaml and add the following dependencies dependencies flutter sdk flutter parse server sdk flutter ^4 0 1 flutter tts ^3 3 1 speech to text ^5 3 0 google ml kit ^0 7 3 provider ^6 0 2 http ^0 13 4 run flutter pub get to install the packages 2 3 initialize parse in your flutter app create a new file lib/config/back4app config dart // lib/config/back4app config dart const string keyapplicationid = 'your application id'; const string keyclientkey = 'your client key'; const string keyparseserverurl = 'https //parseapi back4app com'; replace 'your application id' and 'your client key' with the keys from back4app in lib/main dart , initialize parse // lib/main dart import 'package\ flutter/material dart'; import 'package\ parse server sdk flutter/parse server sdk dart'; import 'config/back4app config dart'; import 'app dart'; void main() async { widgetsflutterbinding ensureinitialized(); await parse() initialize( keyapplicationid, keyparseserverurl, clientkey keyclientkey, autosendsessionid true, debug true, ); runapp(myapp()); } create lib/app dart // lib/app dart import 'package\ flutter/material dart'; import 'screens/home screen dart'; class myapp extends statelesswidget { @override widget build(buildcontext context) { return materialapp( title 'ai voice translator', theme themedata(primaryswatch colors blue), home homescreen(), ); } } step 3 — implementing text translation the first feature to implement is basic text translation 3 1 configure translation service create lib/services/translation service dart to manage interactions with a translation api (google cloud translation api or deepl) // lib/services/translation service dart import 'dart\ convert'; import 'package\ http/http dart' as http; class translationservice { final string apikey = 'your translation api key'; final string apiurl = 'https //translation googleapis com/language/translate/v2'; future\<string> translatetext(string text, string sourcelang, string targetlang) async { final response = await http post( uri parse(apiurl), headers { 'content type' 'application/json', 'authorization' 'bearer $apikey', }, body jsonencode({ 'q' text, 'source' sourcelang, 'target' targetlang, 'format' 'text', }), ); if (response statuscode == 200) { final data = jsondecode(response body); return data\['data']\['translations']\[0]\['translatedtext']; } else { throw exception('translation failed'); } } } 3 2 implement language model create lib/models/language dart to represent supported languages // lib/models/language dart class language { final string name; final string code; final string flagiconurl; language({ required this name, required this code, required this flagiconurl, }); } 3 3 create text translation screen create lib/screens/text translation screen dart to handle text input and translation // lib/screens/text translation screen dart import 'package\ flutter/material dart'; import ' /services/translation service dart'; import ' /models/language dart'; class texttranslationscreen extends statefulwidget { @override texttranslationscreenstate createstate() => texttranslationscreenstate(); } class texttranslationscreenstate extends state\<texttranslationscreen> { final translationservice translationservice = translationservice(); final texteditingcontroller textcontroller = texteditingcontroller(); string translatedtext = ''; language? sourcelanguage; language? targetlanguage; future\<void> translate() async { if (sourcelanguage != null && targetlanguage != null) { final translation = await translationservice translatetext( textcontroller text, sourcelanguage! code, targetlanguage! code, ); setstate(() { translatedtext = translation; }); } } @override widget build(buildcontext context) { return scaffold( appbar appbar( title text('text translation'), ), body padding( padding const edgeinsets all(16), child column( children \[ textfield( controller textcontroller, decoration inputdecoration(labeltext 'enter text to translate'), ), sizedbox(height 10), // implement language selectors here elevatedbutton(onpressed translate, child text('translate')), sizedbox(height 20), text(translatedtext), ], ), ), ); } } step 4 — implementing voice translation in this step, you'll add voice translation by integrating speech to text and text to speech functionalities 4 1 set up speech recognition create lib/services/speech recognition service dart using the speech to text package // lib/services/speech recognition service dart import 'package\ speech to text/speech to text dart' as stt; class speechrecognitionservice { final stt speechtotext speech = stt speechtotext(); future\<string?> listenforspeech() async { bool available = await speech initialize(); if (available) { await speech listen(); return await future delayed(duration(seconds 5), () { speech stop(); return speech lastrecognizedwords; }); } return null; } } 4 2 add text to speech (tts) create lib/services/tts service dart using the flutter tts package // lib/services/tts service dart import 'package\ flutter tts/flutter tts dart'; class ttsservice { final fluttertts fluttertts = fluttertts(); future\<void> speak(string text, string languagecode) async { await fluttertts setlanguage(languagecode); await fluttertts speak(text); } } 4 3 create voice translation screen create lib/screens/voice translation screen dart to handle voice translation // lib/screens/voice translation screen dart import 'package\ flutter/material dart'; import ' /services/speech recognition service dart'; import ' /services/translation service dart'; import ' /services/tts service dart'; class voicetranslationscreen extends statefulwidget { @override voicetranslationscreenstate createstate() => voicetranslationscreenstate(); } class voicetranslationscreenstate extends state\<voicetranslationscreen> { final speechrecognitionservice speechrecognitionservice = speechrecognitionservice(); final translationservice translationservice = translationservice(); final ttsservice ttsservice = ttsservice(); string? recognizedtext; string? translatedtext; future\<void> translatespeech() async { recognizedtext = await speechrecognitionservice listenforspeech(); if (recognizedtext != null) { translatedtext = await translationservice translatetext( recognizedtext!, 'en', // hardcoded source language (english) 'es', // hardcoded target language (spanish) ); setstate(() {}); ttsservice speak(translatedtext!, 'es'); } } @override widget build(buildcontext context) { return scaffold( appbar appbar( title text('voice translation'), ), body center( child column( mainaxisalignment mainaxisalignment center, children \[ if (recognizedtext != null) text('recognized $recognizedtext'), if (translatedtext != null) text('translated $translatedtext'), sizedbox(height 20), elevatedbutton(onpressed translatespeech, child text('start voice translation')), ], ), ), ); } } step 5 — implementing image translation (ocr) 5 1 set up ocr service create lib/services/ocr service dart using the google ml kit package // lib/services/ocr service dart import 'package\ google ml kit/google ml kit dart'; class ocrservice { final textrecognizer = googlemlkit vision textrecognizer(); future\<string?> extracttextfromimage(string imagepath) async { final inputimage = inputimage fromfilepath(imagepath); final recognizedtext = await textrecognizer processimage(inputimage); return recognizedtext text; } } 5 2 create image translation screen create lib/screens/image translation screen dart to handle image input and ocr // lib/screens/image translation screen dart import 'package\ flutter/material dart'; import 'package\ image picker/image picker dart'; import ' /services/ocr service dart'; import ' /services/translation service dart'; class imagetranslationscreen extends statefulwidget { @override imagetranslationscreenstate createstate() => imagetranslationscreenstate(); } class imagetranslationscreenstate extends state\<imagetranslationscreen> { final ocrservice ocrservice = ocrservice(); final translationservice translationservice = translationservice(); string? extractedtext; string? translatedtext; future\<void> pickandtranslateimage() async { final picker = imagepicker(); final pickedfile = await picker getimage(source imagesource gallery); if (pickedfile != null) { extractedtext = await ocrservice extracttextfromimage(pickedfile path); if (extractedtext != null) { translatedtext = await translationservice translatetext(extractedtext!, 'en', 'es'); setstate(() {}); } } } @override widget build(buildcontext context) { return scaffold( appbar appbar( title text('image translation'), ), body center( child column( mainaxisalignment mainaxisalignment center, children \[ if (extractedtext != null) text('extracted $extractedtext'), if (translatedtext != null) text('translated $translatedtext'), sizedbox(height 20), elevatedbutton(onpressed pickandtranslateimage, child text('pick image for translation')), ], ), ), ); } } step 6 — translation history and phrase book in this step, you'll implement saving translation history and managing a phrase book 6 1 save translation history update lib/services/translation service dart to save translation history in back4app import 'package\ parse server sdk flutter/parse server sdk dart'; import ' /models/translation dart'; class translationservice { // existing translation logic future\<void> savetranslation(translation translation) async { final translationobject = parseobject('translation') set('user', parseuser currentuser) set('sourcelanguage', translation sourcelanguage) set('targetlanguage', translation targetlanguage) set('sourcetext', translation sourcetext) set('translatedtext', translation translatedtext) set('translationtype', translation type); await translationobject save(); } } 6 2 implement phrase book create lib/screens/phrase book screen dart to manage common phrases // lib/screens/phrase book screen dart import 'package\ flutter/material dart'; import ' /models/phrase dart'; import ' /services/phrase service dart'; class phrasebookscreen extends statelesswidget { final phraseservice phraseservice = phraseservice(); @override widget build(buildcontext context) { return scaffold( appbar appbar( title text('phrase book'), ), body futurebuilder\<list\<phrase>>( future phraseservice fetchphrases(), builder (context, snapshot) { if (snapshot connectionstate == connectionstate waiting) { return center(child circularprogressindicator()); } if (!snapshot hasdata || snapshot data! isempty) { return center(child text('no phrases found ')); } return listview\ builder( itemcount snapshot data! length, itembuilder (context, index) { final phrase = snapshot data!\[index]; return listtile( title text('${phrase sourcetext} → ${phrase translatedtext}'), subtitle text('category ${phrase category}'), ); }, ); }, ), ); } } conclusion by following this tutorial, you have built a feature rich ai voice translator app with flutter and back4app you implemented basic and advanced text translation voice to voice translation using speech recognition and text to speech image to text translation using ocr translation history management and a phrase book for common expressions next steps enhance ui/ux improve the app’s interface for a smoother user experience improve error handling add error handling and fallback mechanisms for api failures implement offline mode cache common translations, phrases, and history for offline access deploy the app prepare the app for deployment to ios and android platforms