瀏覽代碼

Format inital value of TextInputWidget

ashilkn 2 年之前
父節點
當前提交
36c5052a6d
共有 1 個文件被更改,包括 38 次插入6 次删除
  1. 38 6
      lib/ui/components/text_input_widget.dart

+ 38 - 6
lib/ui/components/text_input_widget.dart

@@ -94,12 +94,8 @@ class _TextInputWidgetState extends State<TextInputWidget> {
     widget.cancelNotifier?.addListener(_onCancel);
     _textController = widget.textEditingController ?? TextEditingController();
 
-    if (widget.initialValue != null) {
-      _textController.value = TextEditingValue(
-        text: widget.initialValue!,
-        selection: TextSelection.collapsed(offset: widget.initialValue!.length),
-      );
-    }
+    _setInitialValue();
+
     if (widget.onChange != null) {
       _textController.addListener(() {
         widget.onChange!.call(_textController.text);
@@ -349,6 +345,42 @@ class _TextInputWidgetState extends State<TextInputWidget> {
   void _popNavigatorStack(BuildContext context, {Exception? e}) {
     Navigator.of(context).canPop() ? Navigator.of(context).pop(e) : null;
   }
+
+  void _setInitialValue() {
+    if (widget.initialValue != null) {
+      final formattedInitialValue = _formatInitialValue(
+        widget.initialValue!,
+        widget.textInputFormatter,
+      );
+      _textController.value = TextEditingValue(
+        text: formattedInitialValue,
+        selection:
+            TextSelection.collapsed(offset: formattedInitialValue.length),
+      );
+    }
+  }
+
+  String _formatInitialValue(
+    String initialValue,
+    List<TextInputFormatter>? formatters,
+  ) {
+    if (formatters == null || formatters.isEmpty) {
+      return initialValue;
+    }
+
+    String formattedValue = initialValue;
+
+    for (final formatter in formatters) {
+      formattedValue = formatter
+          .formatEditUpdate(
+            TextEditingValue.empty,
+            TextEditingValue(text: formattedValue),
+          )
+          .text;
+    }
+
+    return formattedValue;
+  }
 }
 
 //todo: Add clear and custom icon for suffic icon