import 'dart:ui'; extension HexColor on String { Color get color => toColor(); Color toColor() { String hex = replaceAll('#', ''); if (hex.length == 6) { hex = 'FF$hex'; } return Color(int.parse(hex, radix: 16)); } } extension DoubleExtension on double { String toFormattedString(int fractionDigits) { if (this == toInt()) { return toInt().toString(); } else { return toStringAsFixed(fractionDigits); } } double toFormattedDouble(int fractionDigits) { return double.parse(toStringAsFixed(fractionDigits)); } } extension FutureMap on Future { Future map(R Function(T) transform) { return then(transform); } } extension TrimAllExtension on String { String trimAll() { return replaceAll(RegExp(r'\s+'), ''); } String trimAndReduceSpaces() { return trim().replaceAll(RegExp(r'\s+'), ' '); } } // RegExp regExp = RegExp(r"(\d+\.?\d*)(元/月)"); extension PriceExtractionExtension on String { /// 提取金额部分(如 "32元/月" → "32") String extractAmount() { final match = RegExp(r'(\d+\.?\d*)').firstMatch(this); return match?.group(1) ?? ''; // 返回数字部分,失败时返回空字符串 } /// 提取单位部分(如 "32元/月" → "元/月") String extractUnit() { final match = RegExp(r'(\d+\.?\d*)(.*)').firstMatch(this); return match?.group(2) ?? ''; // 返回单位部分,失败时返回空字符串 } /// 同时提取金额和单位(返回元组) (String amount, String unit) extractAmountAndUnit() { final match = RegExp(r'(\d+\.?\d*)(.*)').firstMatch(this); return ( match?.group(1) ?? '', // 金额 match?.group(2) ?? '' // 单位 ); } }