【问题标题】:Textfield input formatter no longer work after I use TextEditingController使用 TextEditingController 后,文本字段输入格式化程序不再工作
【发布时间】:2021-10-03 08:30:55
【问题描述】:

我正在使用pattern formatter 包以便使用ThousandsFormatter。所以当我有值10000000 时,它会自动显示为10.000.000。最初的代码是这样的,它按预期工作

TextFormField(     
     autofocus: false,
     autocorrect: false,
     maxLines: 1,
     textAlign: TextAlign.end,
     keyboardType: TextInputType.number,
     onChanged: (newNominal) {
         final newValue = newNominal.replaceAll(".", "");
         controller.selectedNominal = int.tryParse(newValue) ?? 0;
     },
     inputFormatters: [
         ThousandsFormatter(formatter: NumberFormat("#,###.##", "in_ID")),
     ],
),

然后我做一些这样的按钮

如您所见,当我选择 $4000 时,文本字段将显示 4000,我希望它会显示 4.000

从按钮中选择值后,我使用TextEditingController 填充文本字段中的值,所以我的 TeextField 将是这样的

final _textEditingController = TextEditingController();

TextFormField(  
     controller: _textEditingController, // I add this line   
     autofocus: false,
     autocorrect: false,
     maxLines: 1,
     textAlign: TextAlign.end,
     keyboardType: TextInputType.number,
     onChanged: (newNominal) {
         final newValue = newNominal.replaceAll(".", "");
         controller.selectedNominal = int.tryParse(newValue) ?? 0;
     },
     inputFormatters: [
         ThousandsFormatter(formatter: NumberFormat("#,###.##", "in_ID")),
     ],
),

当我点击其中一个按钮时,build 将再次调用,所以我在文本字段中设置了这样的文本

@override
  Widget build(context) {

      final selectedNominal = pageController.selectedNominal.toString();
      final editingValue = TextEditingValue(
        text: selectedNominal,
        selection: TextSelection.fromPosition(
          TextPosition(offset: selectedNominal.length),
        ),
      );

      _textEditingController.value = editingValue;

    return Scaffold(
      appBar: AppBar(
        title: Text("Title here"),
      ),
      body: _buildWidgets(),
    );
  }
}

我不明白为什么ThousandsFormatter_textEditingController.value 之后不再起作用,即使我使用键盘输入数值,ThousandsFormatter 也不起作用

名义上的控制器是这样的

class NominalController with ChangeNotifier {


  int _selectedNominal = 0;
  int get selectedNominal => _selectedNominal;

  set selectedNominal(int newValue) {
    _selectedNominal = newValue;
    notifyListeners();
  }

}

【问题讨论】:

    标签: flutter inputformatter


    【解决方案1】:

    在将字符串值分配给 textEditing 控制器之前,我必须先给出分隔符

    所以在将值分配给 textEditing 控制器之前,我必须“手动”将 10000 设置为 10.000

    final selectedNominal = pageController.selectedNominal; // 10000
    var currencyFormat = NumberFormat.currency(locale: "in_ID", symbol: "");
    final formattedNominal = "${currencyFormat.format(selectedNominal).replaceAll(",00", "")}"; // 10.000
    
                
    final editingValue = TextEditingValue(
         text: formattedNominal, // 10.000
         selection: TextSelection.fromPosition(TextPosition(offset:formattedNominal.length)),
    );
    
    _textEditingController.value = editingValue;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      相关资源
      最近更新 更多