如何修剪颤振文本字段中的空白?

3 textfield flutter

我在颤振应用程序中使用以下代码作为电子邮件字段以及电子邮件验证器,该代码工作正常,直到用户在文本字段中输入电子邮件后给出空格,而我无法使用 来修剪该空格,我应该如何.trim()处理如果用户输入了空格,则修剪空格?

String emailValidator(String value) {
    Pattern pattern =
        r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
    RegExp regex = new RegExp(pattern);
    if (!regex.hasMatch(value)) {
      return 'Email format is invalid';
    } else {
      return null;
    }
  }

final email = TextFormField(
      decoration: InputDecoration(
        labelText: "Email",
        labelStyle: TextStyle(color: Colors.black),
        prefixIcon: Icon(
          LineIcons.envelope,
          color: Colors.black38,
        ),
        enabledBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: Colors.black38),
        ),
        focusedBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: Colors.orange),
        ),
      ),
      keyboardType: TextInputType.text,
      style: TextStyle(color: Colors.black),
      cursorColor: Colors.black,
      controller: emailInputController,
      validator: emailValidator,
    );
Run Code Online (Sandbox Code Playgroud)

Flo*_*scu 14

对于 Flutter 1.20.0 及最高版本:

TextFormField(
              validator: (value),
              inputFormatters: [
                FilteringTextInputFormatter.deny(new RegExp(r"\s\b|\b\s"))
              ],
)
Run Code Online (Sandbox Code Playgroud)


KuK*_*uKu 6

您如何使用“inputFormatters 和 BlacklistingTextInputFormatter”来防止用户输入空格?

TextFormField(
                validator: _validateInput,
                inputFormatters: [BlacklistingTextInputFormatter(
                    new RegExp(r"\s\b|\b\s")
                )],
                ...
Run Code Online (Sandbox Code Playgroud)