اريد توفير اسلوب لنسخ المستخدم الذي تم انشائة واريد تغيير اسلوب عرض  الواجهه واجعلها احترافية وتغيير الالوان الى الوان ملائمة
للعلم الكود
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:encrypt/encrypt.dart' as encrypt;
import 'dart:math';
import 'package:flutter/services.dart';
import 'package:iconsax/iconsax.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'توليد مستخدمين',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        fontFamily: 'Tajawal',
        appBarTheme: const AppBarTheme(
          elevation: 0,
          centerTitle: true,
          iconTheme: IconThemeData(color: Colors.white),
        ),
      ),
      home: const UserGeneratorScreen(),
    );
  }
}

class UserGeneratorScreen extends StatefulWidget {
  const UserGeneratorScreen({super.key});

  @override
  _UserGeneratorScreenState createState() => _UserGeneratorScreenState();
}

class _UserGeneratorScreenState extends State<UserGeneratorScreen> {
  final String _apiUrl = "api/MicroSerial";
  final String _encryptionKey = "my32lengthsupersecretnooneknows1";
  final String _iv = "8bytesiv12345678";

  bool _isLoading = false;
  String _resultMessage = '';
  bool _isSuccess = false;
  String? _urlApi;
  Map<String, String>? _lastGeneratedUser;

  final String firebaseUrl =
      'https://micropanel-b67e0-default-rtdb.firebaseio.com/information.json';

  late final encrypt.Key _key;
  late final encrypt.IV _ivObj;
  late final encrypt.Encrypter _encrypter;

  final Random _rand = Random.secure();

  @override
  void initState() {
    super.initState();
    _fetchAndDecryptUrlApi();

    _key = encrypt.Key.fromUtf8(_encryptionKey);
    _ivObj = encrypt.IV.fromUtf8(_iv);
    _encrypter = encrypt.Encrypter(
      encrypt.AES(_key, mode: encrypt.AESMode.cbc),
    );
  }

  Future<void> _fetchAndDecryptUrlApi() async {
    try {
      final response = await http.get(Uri.parse(firebaseUrl));
      if (response.statusCode == 200) {
        final data = jsonDecode(response.body);
        if (data != null && data['Urlapi'] != null) {
          _urlApi = _decryptAES(data['Urlapi']);
          setState(() {});
        }
      }
    } catch (_) {
      // يمكن إضافة معالجة الخطأ هنا
    }
  }

  String _decryptAES(String encryptedText) {
    final cleaned = encryptedText.replaceAll(RegExp(r'\s+'), '');
    final encrypted = encrypt.Encrypted.fromBase64(cleaned);
    return _encrypter.decrypt(encrypted, iv: _ivObj);
  }

  String _generateSerialPart(int length) {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    return String.fromCharCodes(
      Iterable.generate(
        length,
        (_) => chars.codeUnitAt(_rand.nextInt(chars.length)),
      ),
    );
  }

  String _encryptAES(String plainText) {
    final encrypted = _encrypter.encrypt(plainText, iv: _ivObj);
    return encrypted.base64
        .replaceAll('+', '-')
        .replaceAll('/', '_')
        .replaceAll('=', '');
  }

  Future<void> _copyToClipboard(String text) async {
    await Clipboard.setData(ClipboardData(text: text));
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(
        content: Text('تم النسخ إلى الحافظة'),
        duration: Duration(seconds: 1),
      ),
    );
  }

  Future<void> _generateAndAddUser() async {
    if (_urlApi == null) return;

    setState(() {
      _isLoading = true;
      _resultMessage = '';
      _isSuccess = false;
      _lastGeneratedUser = null;
    });

    try {
      final randomId = _rand.nextInt(1000000).toString();

      String serialPart1 = _generateSerialPart(6);
      String serialPart2;
      do {
        serialPart2 = _generateSerialPart(6);
      } while (serialPart2 == serialPart1);

      final encryptedSerial1 = _encryptAES(serialPart1);
      final encryptedSerial2 = _encryptAES(serialPart2);

      final userData = {
        "id": randomId,
        "serialPart1": encryptedSerial1,
        "serialPart2": encryptedSerial2,
      };

      final response = await http.post(
        Uri.parse('$_urlApi/$_apiUrl'),
        headers: {'accept': '*/*', 'Content-Type': 'application/json'},
        body: jsonEncode(userData),
      );

      setState(() {
        if (response.statusCode == 201) {
          _isSuccess = true;
          _resultMessage = 'تم إضافة المستخدم بنجاح!';
          _lastGeneratedUser = {
            'ID': randomId,
            'Serial Part 1': serialPart1,
            'Serial Part 2': serialPart2,
          };
        } else {
          _isSuccess = false;
          _resultMessage =
              'فشل في إضافة المستخدم. رمز الخطأ: ${response.statusCode}';
        }
      });
    } catch (e) {
      setState(() {
        _isSuccess = false;
        _resultMessage = 'حدث خطأ: ${e.toString()}';
      });
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('توليد مستخدمين'),
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [Colors.blue.shade700, Colors.blue.shade400],
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
            ),
          ),
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          children: [
            Card(
              elevation: 4,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(15),
              ),
              child: Padding(
                padding: const EdgeInsets.all(20),
                child: Column(
                  children: [
                    const Text(
                      'توليد مستخدم جديد',
                      style: TextStyle(
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: 20),
                    SizedBox(
                      width: double.infinity,
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          padding: const EdgeInsets.symmetric(vertical: 16),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(12),
                          ),
                          backgroundColor: Colors.blue.shade600,
                        ),
                        onPressed:
                            (!_isLoading && _urlApi != null)
                                ? _generateAndAddUser
                                : null,
                        child:
                            _isLoading
                                ? const SizedBox(
                                  width: 24,
                                  height: 24,
                                  child: CircularProgressIndicator(
                                    strokeWidth: 3,
                                    color: Colors.white,
                                  ),
                                )
                                : const Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Icon(Iconsax.user_add, size: 20),
                                    SizedBox(width: 10),
                                    Text(
                                      'توليد مستخدم جديد',
                                      style: TextStyle(fontSize: 16),
                                    ),
                                  ],
                                ),
                      ),
                    ),
                    if (_urlApi == null)
                      const Padding(
                        padding: EdgeInsets.only(top: 10),
                        child: LinearProgressIndicator(),
                      ),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 20),
            if (_resultMessage.isNotEmpty)
              AnimatedSwitcher(
                duration: const Duration(milliseconds: 300),
                child: Card(
                  key: ValueKey(_isSuccess),
                  color: _isSuccess ? Colors.green.shade50 : Colors.red.shade50,
                  elevation: 3,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(15),
                    side: BorderSide(
                      color:
                          _isSuccess
                              ? Colors.green.shade200
                              : Colors.red.shade200,
                      width: 1,
                    ),
                  ),
                  child: Padding(
                    padding: const EdgeInsets.all(16),
                    child: Column(
                      children: [
                        Row(
                          children: [
                            Icon(
                              _isSuccess
                                  ? Iconsax.tick_circle
                                  : Iconsax.close_circle,
                              color: _isSuccess ? Colors.green : Colors.red,
                            ),
                            const SizedBox(width: 10),
                            Expanded(
                              child: Text(
                                _resultMessage,
                                style: TextStyle(
                                  color:
                                      _isSuccess
                                          ? Colors.green.shade800
                                          : Colors.red.shade800,
                                  fontSize: 16,
                                ),
                              ),
                            ),
                          ],
                        ),
                        if (_lastGeneratedUser != null)
                          ..._lastGeneratedUser!.entries.map(
                            (entry) => Padding(
                              padding: const EdgeInsets.only(top: 12),
                              child: Row(
                                children: [
                                  Expanded(
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        Text(
                                          entry.key,
                                          style: TextStyle(
                                            color: Colors.grey.shade600,
                                            fontSize: 14,
                                          ),
                                        ),
                                        const SizedBox(height: 4),
                                        Container(
                                          padding: const EdgeInsets.symmetric(
                                            horizontal: 12,
                                            vertical: 8,
                                          ),
                                          decoration: BoxDecoration(
                                            color: Colors.grey.shade100,
                                            borderRadius: BorderRadius.circular(
                                              8,
                                            ),
                                          ),
                                          child: Row(
                                            children: [
                                              Expanded(
                                                child: Text(
                                                  entry.value,
                                                  style: const TextStyle(
                                                    fontSize: 16,
                                                    fontWeight: FontWeight.w500,
                                                  ),
                                                ),
                                              ),
                                              IconButton(
                                                icon: const Icon(
                                                  Iconsax.copy,
                                                  size: 20,
                                                ),
                                                onPressed:
                                                    () => _copyToClipboard(
                                                      entry.value,
                                                    ),
                                                padding: EdgeInsets.zero,
                                                visualDensity:
                                                    VisualDensity.compact,
                                              ),
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                      ],
                    ),
                  ),
                ),
              ),
            if (_lastGeneratedUser != null)
              Padding(
                padding: const EdgeInsets.only(top: 20),
                child: ElevatedButton.icon(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.blue.shade50,
                    foregroundColor: Colors.blue.shade700,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12),
                    ),
                    padding: const EdgeInsets.symmetric(
                      horizontal: 20,
                      vertical: 12,
                    ),
                  ),
                  onPressed: () {
                    final allData = _lastGeneratedUser!.entries
                        .map((e) => '${e.key}: ${e.value}')
                        .join('\n');
                    _copyToClipboard(allData);
                  },
                  icon: const Icon(Iconsax.copy, size: 20),
                  label: const Text('نسخ جميع البيانات'),
                ),
              ),
          ],
        ),
      ),
    );
  }
}