Flutter - Dart

Онлайн ағылшын - қазақша аударма

main.dart

				
					import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: TranslatorPage(),
    );
  }
}

class TranslatorPage extends StatefulWidget {
  @override
  _TranslatorPageState createState() => _TranslatorPageState();
}

class _TranslatorPageState extends State<TranslatorPage> {
  final TextEditingController _textEditingController = TextEditingController();
  String _translation = '';

  Future<void> _translate(String text) async {
    final url = Uri.parse('https://api.mymemory.translated.net/get?q=$text&langpair=en|kk');

    final response = await http.get(url);
    if (response.statusCode == 200) {
      final data = jsonDecode(response.body);
      final translatedText = data['responseData']['translatedText'];
      setState(() {
        _translation = translatedText;
      });
    } else {
      setState(() {
        _translation = 'Аудармада қате бар';
      });
    }
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Ағылшын - Қазақша аударма'),
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(20.0),
            child: TextField(
              controller: _textEditingController,
              decoration: InputDecoration(
                labelText: 'English',
              ),
            ),
          ),
          ElevatedButton(
            onPressed: () {
              _translate(_textEditingController.text);
            },
            child: Text('Аудару'),
          ),
          SizedBox(height: 20),
          Text(
            'Қазақша: $_translation',
            style: TextStyle(fontSize: 18),
          ),
        ],
      ),
    );
  }
}