在 Flutter 中限制第一个字符为“0”

Restrict "0" in First Character in Flutter

Flutter中text field如何限制“0”不能放在第一个字符?以下是飞镖代码示例。

TextField(
                                          inputFormatters: <TextInputFormatter>[
                                            FilteringTextInputFormatter.allow(
                                                RegExp("[0-9\u0660-\u0669]")),
                                          ],
                                          keyboardType: TextInputType.number,
                                          focusNode: _nodeText1,
                                          controller: mobileNumber,
                                          decoration: InputDecoration.collapsed(
                                              fillColor: TuxedoColor.whiteColor,
                                              hintText: "5xxxxxxxx"),
                                        ),

使用这个:

   inputFormatters: <TextInputFormatter>[
                                FilteringTextInputFormatter.allow(
                                  RegExp(r'[0-9]'),
                                ),
                                FilteringTextInputFormatter.deny(
                                  RegExp(
                                      r'^0+'), //users can't type 0 at 1st position
                                ),
                              ],

尝试使用下面的代码 FilteringTextInputFormatter.deny

TextField(
      inputFormatters: <TextInputFormatter>[
        FilteringTextInputFormatter.allow(RegExp("[0-9\u0660-\u0669]")),
        FilteringTextInputFormatter.deny(RegExp(r'^0+')),
      ],
      keyboardType: TextInputType.number,
      decoration: InputDecoration.collapsed(hintText: "5xxxxxxxx"),
    ),