اريد ان يتم تفريغ حقل او ادخل مبلغ جديد بعد اضافة المبلغ عبر ايقونة الاضافة
await showDialog(
  context: context,
  barrierDismissible: false,
  builder: (context) {
    return StatefulBuilder(
      builder: (context, setState) {
        WidgetsBinding.instance.addPostFrameCallback((_) {
          if (_savedAmounts.isNotEmpty && amountsController.text.isEmpty) {
            amountsController.text = _formatWithThousandsSeparator(_savedAmounts.first);
          }
        });

        return Dialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(24),
          ),
          elevation: 8,
          insetPadding: const EdgeInsets.all(20),
          child: SingleChildScrollView(
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 20),
              decoration: BoxDecoration(
                color: Theme.of(context).colorScheme.surface,
                borderRadius: BorderRadius.circular(24),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // Header
                  Row(
                    children: [
                      Container(
                        padding: const EdgeInsets.all(8),
                        decoration: BoxDecoration(
                          color: Theme.of(context).colorScheme.primary.withOpacity(0.1),
                          shape: BoxShape.circle,
                        ),
                        child: Icon(
                          Icons.monetization_on_rounded,
                          size: 28,
                          color: Theme.of(context).colorScheme.primary,
                        ),
                      ),
                      const SizedBox(width: 12),
                      Text(
                        'المحفظة الرقمية',
                        style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.bold,
                          fontFamily: tajawalFont,
                          color: Theme.of(context).colorScheme.onSurface,
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 24),

                  // Saved Amounts List
                  Container(
                    width: double.infinity,
                    padding: const EdgeInsets.all(16),
                    decoration: BoxDecoration(
                      color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.1),
                      borderRadius: BorderRadius.circular(16),
                      border: Border.all(
                        color: Theme.of(context).colorScheme.outline.withOpacity(0.1),
                      ),
                    ),
                    child: _savedAmounts.isEmpty
                        ? Column(
                            children: [
                              Icon(
                                Icons.wallet_rounded,
                                size: 40,
                                color: Theme.of(context).colorScheme.primary.withOpacity(0.5),
                              ),
                              const SizedBox(height: 12),
                              Text(
                                'أضف المبالغ التي تريد استخدامها بشكل متكرر',
                                style: TextStyle(
                                  fontFamily: tajawalFont,
                                  color: Theme.of(context).colorScheme.onSurfaceVariant,
                                  fontSize: 14,
                                ),
                                textAlign: TextAlign.center,
                              ),
                            ],
                          )
                        : Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                'اختر من المبالغ المحفوظة:',
                                style: TextStyle(
                                  fontFamily: tajawalFont,
                                  color: Theme.of(context).colorScheme.onSurfaceVariant,
                                ),
                              ),
                              const SizedBox(height: 12),
                              SizedBox(
                                height: 120,
                                child: ListView.builder(
                                  itemCount: _savedAmounts.length,
                                  itemBuilder: (context, index) {
                                    final amount = _savedAmounts[index];
                                    return ListTile(
                                      contentPadding: EdgeInsets.zero,
                                      leading: Radio<int>(
                                        value: amount,
                                        groupValue: _savedAmounts.first,
                                        onChanged: (value) {
                                          setState(() {
                                            amountsController.text = _formatWithThousandsSeparator(value!);
                                          });
                                        },
                                      ),
                                      title: Text(
                                        _formatWithThousandsSeparator(amount),
                                        style: TextStyle(
                                          fontFamily: tajawalFont,
                                          fontWeight: FontWeight.w600,
                                        ),
                                      ),
                                      trailing: IconButton(
                                        icon: Icon(
                                          Icons.delete_outline,
                                          color: Theme.of(context).colorScheme.error,
                                        ),
                                        onPressed: () {
                                          setState(() {
                                            _savedAmounts.remove(amount);
                                            if (_savedAmounts.isNotEmpty) {
                                              amountsController.text =
                                                  _formatWithThousandsSeparator(_savedAmounts.first);
                                            } else {
                                              amountsController.clear();
                                            }
                                          });
                                        },
                                      ),
                                    );
                                  },
                                ),
                              ),
                            ],
                          ),
                  ),
                  const SizedBox(height: 20),

                  // Amount Input Field
                  TextField(
                    controller: amountsController,
                    keyboardType: TextInputType.number,
                    inputFormatters: [ThousandsSeparatorInputFormatter()],
                    decoration: InputDecoration(
                      labelText: 'أو أدخل مبلغًا جديدًا',
                      labelStyle: TextStyle(
                        fontFamily: tajawalFont,
                        color: Theme.of(context).colorScheme.onSurfaceVariant,
                      ),
                      suffixIcon: IconButton(
                        icon: Icon(
                          Icons.add_circle_rounded,
                          color: Theme.of(context).colorScheme.primary,
                          size: 28,
                        ),
                        onPressed: () {
                          final raw = amountsController.text.replaceAll(',', '');
                          final numAmount = int.tryParse(raw);
                          if (numAmount == null || numAmount <= 0) {
                            _showSnackBar('يرجى إدخال مبلغ صحيح أكبر من صفر');
                            return;
                          }
                          if (_savedAmounts.contains(numAmount)) {
                            _showSnackBar('هذا المبلغ موجود بالفعل');
                            return;
                          }
                          setState(() {
                            _savedAmounts.add(numAmount);
                            _savedAmounts.sort();
                          });
                          amountsController.clear(); // تفريغ الحقل خارج setState
                        },
                      ),
                      filled: true,
                      fillColor: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.1),
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(14),
                        borderSide: BorderSide.none,
                      ),
                    ),
                    onSubmitted: (value) {
                      final raw = value.replaceAll(',', '');
                      final numAmount = int.tryParse(raw);
                      if (numAmount == null || numAmount <= 0) {
                        _showSnackBar('يرجى إدخال مبلغ صحيح أكبر من صفر');
                        return;
                      }
                      if (_savedAmounts.contains(numAmount)) {
                        _showSnackBar('هذا المبلغ موجود بالفعل');
                        return;
                      }
                      setState(() {
                        _savedAmounts.add(numAmount);
                        _savedAmounts.sort();
                      });
                      amountsController.clear(); // تفريغ الحقل خارج setState
                    },
                  ),
                  const SizedBox(height: 24),

                  // Action Buttons
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      OutlinedButton(
                        onPressed: () => Navigator.pop(context),
                        style: OutlinedButton.styleFrom(
                          foregroundColor: Theme.of(context).colorScheme.onSurface,
                          side: BorderSide(
                            color: Theme.of(context).colorScheme.outline.withOpacity(0.4),
                          ),
                          padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(12),
                          ),
                        ),
                        child: Text(
                          'تخطي',
                          style: TextStyle(
                            fontFamily: tajawalFont,
                            fontSize: 15,
                          ),
                        ),
                      ),
                      const SizedBox(width: 12),
                      ElevatedButton(
                        onPressed: () async {
                          if (_savedAmounts.isEmpty) {
                            _showSnackBar('الرجاء إدخال مبلغ واحد على الأقل');
                            return;
                          }
                          await _saveSavedAmounts();
                          Navigator.pop(context);
                        },
                        style: ElevatedButton.styleFrom(
                          backgroundColor: Theme.of(context).colorScheme.primary,
                          foregroundColor: Theme.of(context).colorScheme.onPrimary,
                          padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(12),
                          ),
                        ),
                        child: Text(
                          'حفظ',
                          style: TextStyle(
                            fontFamily: tajawalFont,
                            fontSize: 15,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  },
);