import 'package:flutter/services.dart'; import 'package:intl/intl.dart'; /// KRW 통화 포맷팅 관련 유틸리티 class CurrencyFormatter { static final NumberFormat _formatter = NumberFormat('#,###', 'ko_KR'); /// 숫자를 KRW 통화 형식으로 포맷팅 (₩2,000,000) static String formatKRW(double? value) { if (value == null || value == 0) return ''; return '₩${_formatter.format(value.toInt())}'; } /// 통화 포맷팅된 문자열에서 숫자만 추출 static double? parseKRW(String? text) { if (text == null || text.isEmpty) return null; // ₩, 쉼표 제거하고 숫자만 추출 final cleanText = text.replaceAll(RegExp(r'[₩,]'), ''); return double.tryParse(cleanText); } } /// KRW 통화 입력을 위한 TextInputFormatter class KRWTextInputFormatter extends TextInputFormatter { @override TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue, ) { // 빈 문자열인 경우 그대로 반환 if (newValue.text.isEmpty) { return newValue; } // 숫자만 추출 final numericText = newValue.text.replaceAll(RegExp(r'[^\d]'), ''); // 숫자가 없으면 빈 문자열 if (numericText.isEmpty) { return const TextEditingValue( text: '', selection: TextSelection.collapsed(offset: 0), ); } // 숫자 파싱 및 포맷팅 final numericValue = double.tryParse(numericText); if (numericValue == null) { return oldValue; } // KRW 포맷팅 적용 final formattedText = CurrencyFormatter.formatKRW(numericValue); // 커서 위치 계산 (맨 끝으로) final selectionOffset = formattedText.length; return TextEditingValue( text: formattedText, selection: TextSelection.collapsed(offset: selectionOffset), ); } }