我正在尝试创建自定义 InputTextFormatter。格式化程序用空格分隔数千个并将点后的字符数限制为 2 个字符。
我想将光标移动到 TextField 值的末尾,但根据我在达到限制(2 个字符)后尝试输入附加字符的次数,实际光标看起来会进一步移动。看起来选择不适用于生成的 TextEditingValue。
重现步骤:
预期行为:按一次退格键必须删除 TextField 中的最后一个字符。
class MoneyFormatter extends TextInputFormatter {
MoneyFormatter({this.maxLength = 30, this.decimals = 0});
final int maxLength;
final int decimals;
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
print('---------- newValue.selection.extentOffset : ${newValue.selection.extentOffset}');
String output = newValue.text.formatAsCurrency(decimals: decimals);
var result = TextEditingValue(
text: output,
//this line doesn't have any effect
selection: TextSelection.collapsed(offset: output.length),
);
print('---------- result.selection.extentOffset : ${result.selection.extentOffset}');
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
每增加一个字符都result.selection.extentOffset …
到目前为止,我在代码中使用单例模式,但我正在切换到 Remi Rousselet\xe2\x80\x99s Provider。我有一个业务逻辑类,目前依赖于其他 7 个类。ProxyProvider 最多允许使用 6 个。在这种情况下如何实现 Provider 模式?
\n\nclass BlocAuth {\n BlocAuth(this.serviceChatFirestore);\n ServiceChatFirestore serviceChatFirestore;\n var _state = AuthState();\n var blocUser = BlocUser();\n var user = UserModel();\n var blocRouting = BlocRouting();\n var blocBrands = BlocBrands();\n var blocNotifications = BlocNotifications();\n}\n
Run Code Online (Sandbox Code Playgroud)\n