simple_translator 0.0.2 copy "simple_translator: ^0.0.2" to clipboard
simple_translator: ^0.0.2 copied to clipboard

A simple and lightweight package for translating text using the Google Translate API.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:simple_translator/simple_translator.dart';

void main() {
  runApp(TranslateApp());
}

class TranslateApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Translator Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: TranslatorScreen(),
    );
  }
}

class TranslatorScreen extends StatefulWidget {
  @override
  _TranslatorScreenState createState() => _TranslatorScreenState();
}

class _TranslatorScreenState extends State<TranslatorScreen> {
  final _inputController = TextEditingController();
  String _translatedText = '';
  String _hardcodedResult = '';
  bool _loading = false;

  final _translator = GoogleTranslator(client: ClientType.siteGT);

  Future<void> _translateInput() async {
    setState(() => _loading = true);
    try {
      final result = await _translator.translate(
        _inputController.text,
        from: 'auto',
        to: 'en',
      );
      setState(() {
        _translatedText = result.text;
      });
    } catch (e) {
      setState(() {
        _translatedText = 'Error: $e';
      });
    } finally {
      setState(() => _loading = false);
    }
  }

  Future<void> _translateHardcoded() async {
    setState(() => _loading = true);
    try {
      final translation = await _translator.translate(
        "Dart is very cool!",
        to: 'pl',
      );
      setState(() {
        _hardcodedResult =
            'Original: ${translation.source}\nTranslated (to Polish): ${translation.text}';
      });
    } catch (e) {
      setState(() {
        _hardcodedResult = 'Hardcoded translation error: $e';
      });
    } finally {
      setState(() => _loading = false);
    }
  }

  @override
  void dispose() {
    _inputController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Translator')),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            TextField(
              controller: _inputController,
              decoration: InputDecoration(
                labelText: 'Enter text',
                border: OutlineInputBorder(),
              ),
              minLines: 2,
              maxLines: 5,
            ),
            SizedBox(height: 16),
            ElevatedButton(
              onPressed: _loading ? null : _translateInput,
              child: _loading
                  ? CircularProgressIndicator(color: Colors.white)
                  : Text('Translate to English'),
            ),
            SizedBox(height: 24),
            Text('Translated:', style: TextStyle(fontWeight: FontWeight.bold)),
            SizedBox(height: 8),
            SelectableText(_translatedText),
            Divider(height: 40),
            ElevatedButton(
              onPressed: _loading ? null : _translateHardcoded,
              child: Text('Translate Example: "Dart is very cool!" → Polish'),
            ),
            SizedBox(height: 16),
            Text(
              'Programmatic Translation:',
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 8),
            SelectableText(_hardcodedResult),
          ],
        ),
      ),
    );
  }
}
6
likes
150
points
32
downloads

Publisher

verified publisherrahulreza.com

Weekly Downloads

A simple and lightweight package for translating text using the Google Translate API.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, http

More

Packages that depend on simple_translator