Flutter 颤振-TextFormField-从TextInputFormatter调用Future

Flutter 颤振-TextFormField-从TextInputFormatter调用Future,flutter,dart,Flutter,Dart,我正在尝试制作一个TextFormField,在您键入电话号码时对其进行格式化。我正试图使用libphonenumber包来实现这一点,但它提供的方法返回的是Future,而不是字符串。我不知道该怎么做 我的输入字段如下所示: TextFormField( decoration: InputDecoration( hintText: "Phone Number", ), controller: _textController, keyboardType

我正在尝试制作一个TextFormField,在您键入电话号码时对其进行格式化。我正试图使用libphonenumber包来实现这一点,但它提供的方法返回的是
Future
,而不是字符串。我不知道该怎么做

我的输入字段如下所示:

TextFormField(
    decoration: InputDecoration(
      hintText: "Phone Number",
    ),
    controller: _textController,
    keyboardType: TextInputType.phone,
    inputFormatters: <TextInputFormatter>[_PhoneFormatter('US')],
  )
import 'package:libphonenumber/libphonenumber.dart';

class _PhoneFormatter extends TextInputFormatter {
  final String _countryCode;

  _PhoneFormatter(this._countryCode);

  @override
  TextEditingValue formatEditUpdate(oldPhone, newPhone) {
    final phoneNumber = PhoneNumberUtil.formatAsYouType(
        phoneNumber: newPhone.text, isoCode: _countryCode);
    return TextEditingValue(
        text: phoneNumber,
        selection: TextSelection.collapsed(offset: phoneNumber.length));
  }
}
但我得到了一个错误:

参数类型
'Future您可以使用该软件包进行灵活的电话格式设置:

TextFormField(
  decoration: InputDecoration(
    hintText: "Phone Number",
  ),
  controller: _textController,
  keyboardType: TextInputType.phone,
  inputFormatters: [phoneFormatter()],
)

//...

MaskTextInputFormatter phoneFormatter(){
  return MaskTextInputFormatter(mask: '+#-###-###-####', filter: { "#": RegExp(r'[0-9]') });
}

所以我找到了解决问题的办法。我发现另一个包(intl_phone_number_input)使用了libphonenumber的相同功能,我看到了他们是如何做到的(多亏了oluwatobi)。ogunye@bankly.ng). 它们需要一个异步回调方法作为TextInputFormatter的参数,然后在编辑过程中调用它。再过一百万年,我自己也不会明白这一点

以下是我更新的格式化程序代码:

import 'package:libphonenumber/libphonenumber.dart';

class _PhoneFormatter extends TextInputFormatter {
  final String _countryCode;
  final void Function(TextEditingValue value) onPhoneFormatted;

  _PhoneFormatter(this._countryCode, {@required this.onPhoneFormatted});

  @override
  TextEditingValue formatEditUpdate(oldPhone, newPhone) {
    _formatPhone(newPhone.text.replaceAll(RegExp(r'[^\d\+]+'), ''))
        .then((String phone) {
      onPhoneFormatted(TextEditingValue(
          text: phone,
          selection: TextSelection.collapsed(offset: phone.length)));
    });
    return newPhone;
  }

  Future<String> _formatPhone(String phoneNumber) async {
    try {
      String formattedPhoneNumber = await PhoneNumberUtil.formatAsYouType(
          phoneNumber: phoneNumber, isoCode: _countryCode);
      return formattedPhoneNumber ?? phoneNumber;
    } on Exception {
      return phoneNumber;
    }
  }
}

import'包:libphonenumber/libphonenumber.dart';
类\u PhoneFormatter扩展了TextInputFormatter{
最终字符串\u国家代码;
PhoneFormatted上的最终void函数(TextEditingValue值);
_PhoneFormatter(this.\u countryCode,{@required this.onPhoneFormatted});
@凌驾
文本编辑值格式编辑更新(旧电话、新电话){
_formatPhone(newPhone.text.replaceAll(RegExp(r'[^\d\+]+'),'')
.然后((字符串电话){
onPhoneFormatted(文本编辑值(
短信:电话,
选择:TextSelection.collapsed(偏移量:phone.length));
});
返回新电话;
}
未来格式电话(字符串电话号码)异步{
试一试{
String formattedPhoneNumber=等待PhoneNumberUtil.FormatAsyOutType(
电话号码:电话号码,等码:_countryCode);
返回格式化的电话号码??电话号码;
}例外{
返回电话号码;
}
}
}
以及我使用它的输入字段:

final _countryCode = 'US'

TextFormField(
  decoration: InputDecoration(
    hintText: "Phone Number",
  ),
  controller: _textController,
  keyboardType: TextInputType.phone,
  inputFormatters: <TextInputFormatter>[
    _PhoneFormatter(
      _countryCode,
      onPhoneFormatted: (TextEditingValue value) {
        setState(() {
          _textController.value = value;
        });
      },
    )
  ],
)
final\u countryCode='US'
TextFormField(
装饰:输入装饰(
hintText:“电话号码”,
),
控制器:_textController,
键盘类型:TextInputType.phone,
输入格式化程序:[
_电话格式化程序(
_国家代码,
onPhoneFormatted:(文本编辑值){
设置状态(){
_textController.value=值;
});
},
)
],
)

似乎效果很好。

那不起作用。我现在得到的错误是:“\u PhoneFormatter.formatEditUpdate”(“Future Function(TextEditingValue,TextEditingValue)”)不是对“TextInputFormatter.formatEditUpdate”(“TextEditingValue Function(TextEditingValue,TextEditingValue)”)的有效重写。因此,这里我们有两个问题:由于
Future
,我们无法重写方法;由于
wait
,我们无法调用
PhoneNumberUtil.FormatAsyOutType
。检查我的更新答案,我建议的方法只使用纯颤振包,不使用本机Java调用谢谢,我知道这会起作用。但是我需要支持所有的国家代码和每个国家代码的不同格式(我的例子就是“US”),所以我希望使用Google的libphonenumber或同等产品。@ScottSmith我理解你的意思。不幸的是,我现在没有适合你的想法。
final _countryCode = 'US'

TextFormField(
  decoration: InputDecoration(
    hintText: "Phone Number",
  ),
  controller: _textController,
  keyboardType: TextInputType.phone,
  inputFormatters: <TextInputFormatter>[
    _PhoneFormatter(
      _countryCode,
      onPhoneFormatted: (TextEditingValue value) {
        setState(() {
          _textController.value = value;
        });
      },
    )
  ],
)