auto_translate_plugin 1.0.1
auto_translate_plugin: ^1.0.1 copied to clipboard
A Flutter plugin that extends the standard Text widget to automatically translate text using Google ML Kit on-device translation.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:auto_translate_plugin/auto_translate_plugin.dart';
void main() {
runApp(
ChangeNotifierProvider(
create: (_) => TranslatorProvider(),
child: const MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Auto Translate Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: const TranslationScreen(),
);
}
}
class TranslationScreen extends StatelessWidget {
const TranslationScreen({super.key});
@override
Widget build(BuildContext context) {
final languageCode = context.watch<TranslatorProvider>().languageCode;
return Scaffold(
appBar: AppBar(title: const Text('AutoTranslate Text')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const AutoTranslateText(
'Hello! Welcome to the auto translation example.',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
const SizedBox(height: 20),
const AutoTranslateText(
'This text is magically translated into the language you select below. '
'It uses Google ML Kit for on-device translation without needing internet.',
style: TextStyle(fontSize: 16),
textAlign: TextAlign.center,
),
const SizedBox(height: 40),
DropdownButton<String>(
value: languageCode,
items: const [
DropdownMenuItem(value: 'en', child: Text('English')),
DropdownMenuItem(value: 'ta', child: Text('Tamil')),
DropdownMenuItem(value: 'hi', child: Text('Hindi')),
DropdownMenuItem(value: 'te', child: Text('Telugu')),
DropdownMenuItem(value: 'ml', child: Text('Malayalam')),
],
onChanged: (String? newLanguage) {
if (newLanguage != null) {
context.read<TranslatorProvider>().changeLanguage(
newLanguage,
);
}
},
),
],
),
),
),
);
}
}