smart_translate_text 0.0.2
smart_translate_text: ^0.0.2 copied to clipboard
A Flutter package that auto-translates Text, AutoSizeText, AnimatedText, and RichText widgets based on selected language.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:smart_translate_text/smart_translate_text.dart';
import 'package:provider/provider.dart';
void main() {
/// App wants Hindi as default (not system language)
LanguageService.setDefault("hi");
runApp(const ExampleApp());
}
/// Suppose app has a localization module using Provider / BLoC / GetX / Cubit
/// Here we implement a simple example localization controller
class AppLocalizationController extends ChangeNotifier {
String _currentLang = LanguageService.current;
String get currentLanguage => _currentLang;
void changeLanguage(String langCode) {
_currentLang = langCode;
/// Tell the package we switched language
LanguageService.change(langCode);
notifyListeners();
}
}
class ExampleApp extends StatelessWidget {
const ExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Auto Translate Text Demo",
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: ChangeNotifierProvider(
create: (_) => AppLocalizationController(),
child: const ExampleHomePage(),
),
);
}
}
class ExampleHomePage extends StatelessWidget {
const ExampleHomePage({super.key});
@override
Widget build(BuildContext context) {
final localization = Provider.of<AppLocalizationController>(context);
final languages = <String, String>{
'en': 'English',
'hi': 'Hindi',
'ar': 'Arabic',
'fr': 'French',
'es': 'Spanish',
};
return Scaffold(
appBar: AppBar(title: const Text("Auto Translate Text Example")),
body: Padding(
padding: const EdgeInsets.all(18),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Dropdown
const Text(
"Select App Language:",
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
DropdownButton<String>(
value: localization.currentLanguage,
items:
languages.entries.map((map) {
return DropdownMenuItem(
value: map.key,
child: Text(map.value),
);
}).toList(),
onChanged: (value) {
if (value != null) localization.changeLanguage(value);
},
),
const Divider(height: 32),
const Text(
"LangText",
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 6),
const LangText(
"Hello, how are you today?",
style: TextStyle(fontSize: 18),
),
const SizedBox(height: 24),
const Text(
"LangAutoSizeText",
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 6),
const LangAutoSizeText(
"This is a long text that will be translated and also auto-resized.",
maxLines: 2,
),
const SizedBox(height: 24),
const Text(
"LangAnimatedText",
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 6),
const LangAnimatedText(
"This message is being auto-translated and typed like an AI bot.",
),
const SizedBox(height: 24),
const Text(
"LangRichText",
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 6),
LangRichText(
textSpans: const [
TextSpan(
text: "This is ",
style: TextStyle(color: Colors.black),
),
TextSpan(
text: "bold",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
TextSpan(
text: " and this is ",
style: TextStyle(color: Colors.grey),
),
TextSpan(
text: "italic.",
style: TextStyle(
fontStyle: FontStyle.italic,
color: Colors.green,
),
),
],
),
],
),
),
);
}
}