如何限制文本字段中的值颤音中的值?

发布于 2025-02-06 07:02:39 字数 969 浏览 0 评论 0原文

我想将用户输入的值限制为flutter文本字段中的预期值。

例子, 如果来自API的值是10,并且想要限制输入不超过10,如果客户端类型11或10以上的内容,则希望显示警报或使用户不输入。 如何控制这个?

TextFormField(
              style: TextStyle(fontSize: 20),
              onChanged: (value) {
                if (value != "") {
                  int _checkValue = int.parse(value);
                  if (_checkValue >
                      Provider.of<SaleProvider>(context, listen: false)
                              .remainNewQuantity(
                                  this.currentProductItemSelected.id)) {
                    return 'error';
                  } else {
                    setState(() {
                      this.qty = int.parse(value);
                      updateByQty();
                    });
                  }
                } else {
                  setState(() {
                    
                  });
                }
              },
            ),

这是我的尝试,但不能做我想要的。

I want to limit the value that user input not to over a expected value in flutter text field.

Example,
If there is a value come from API is 10 and want to limit the input not over 10, if client type 11 or something over 10, want to show alert or make user not to type.
How to control this?

TextFormField(
              style: TextStyle(fontSize: 20),
              onChanged: (value) {
                if (value != "") {
                  int _checkValue = int.parse(value);
                  if (_checkValue >
                      Provider.of<SaleProvider>(context, listen: false)
                              .remainNewQuantity(
                                  this.currentProductItemSelected.id)) {
                    return 'error';
                  } else {
                    setState(() {
                      this.qty = int.parse(value);
                      updateByQty();
                    });
                  }
                } else {
                  setState(() {
                    
                  });
                }
              },
            ),

This is my trying, but can't do that I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪若未夕 2025-02-13 07:02:39

请检查下面的方法。我认为这将解决您的问题。如果仍然不起作用,请告诉我

  Widget getTextField({required int maxValue}) {
    return TextFormField(
      controller: _textController,
      keyboardType: TextInputType.number,
      onChanged: (text) {
        if (int.parse(text) > maxValue) {
          // show popup here.
          _textController.text = validText;
          _textController.selection = TextSelection.fromPosition(TextPosition(offset: _textController.text.length));
        }else{
          validText = text;
        }
      },
    );
  }

Please check below method. I think this will resolve your issue. If still not work, please let me know

  Widget getTextField({required int maxValue}) {
    return TextFormField(
      controller: _textController,
      keyboardType: TextInputType.number,
      onChanged: (text) {
        if (int.parse(text) > maxValue) {
          // show popup here.
          _textController.text = validText;
          _textController.selection = TextSelection.fromPosition(TextPosition(offset: _textController.text.length));
        }else{
          validText = text;
        }
      },
    );
  }
城歌 2025-02-13 07:02:39

作为我自己学习的一种练习,我给了它,并提出了以下方法。创建一个定制的小部件,它看起来像是如此简单的东西,看起来像是很多代码……但是它似乎按照预期的方式工作,并且可以以各种方式修改,扩展和集成与其他元素。

用法:const Maxintfield(最大:100),

实现:

class MaxIntField extends StatefulWidget {
  const MaxIntField({Key? key, this.max = 1}) : super(key: key);
  final int max;

  @override
  State<MaxIntField> createState() => _MaxIntFieldState();
}

class _MaxIntFieldState extends State<MaxIntField> {
  final TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    _controller.value.copyWith(text: '0');
    _controller.addListener(() {
      if (_controller.text.isNotEmpty && _controller.text != '0') {
        int intVal = int.parse(_controller.text);
        if (intVal > widget.max) {
          setState(() {
            _controller.value =
                _controller.value.copyWith(text: widget.max.toString());
            _showMyDialog();
          });
        } else if (_controller.text != intVal.toString()) {
          //remove leading '0'
          setState(() {
            _controller.value =
                _controller.value.copyWith(text: intVal.toString());
          });
        }
      }
    });
  }

// assuming using Material
  _showMyDialog() async {
    showDialog<String>(
      context: context,
      builder: (BuildContext context) => AlertDialog(
        title: const Text('AlertDialog Title'),
        content: Text('This field is limited to ${widget.max}'),
        actions: <Widget>[
          TextButton(
            onPressed: () => Navigator.pop(context, 'OK'),
            child: const Text('OK'),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: _controller,
      keyboardType: TextInputType.number,
    );
  }
}

As an exercise for my own learning I gave it a go and came up with the following approach; creating a bespoke widget which admittedly looks like a lot of code for something so simple... but it appears to work as expected I think and one could modify, expand and integrate it with other elements in various ways.

Usage: const MaxIntField(max: 100),

Implementation:

class MaxIntField extends StatefulWidget {
  const MaxIntField({Key? key, this.max = 1}) : super(key: key);
  final int max;

  @override
  State<MaxIntField> createState() => _MaxIntFieldState();
}

class _MaxIntFieldState extends State<MaxIntField> {
  final TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    _controller.value.copyWith(text: '0');
    _controller.addListener(() {
      if (_controller.text.isNotEmpty && _controller.text != '0') {
        int intVal = int.parse(_controller.text);
        if (intVal > widget.max) {
          setState(() {
            _controller.value =
                _controller.value.copyWith(text: widget.max.toString());
            _showMyDialog();
          });
        } else if (_controller.text != intVal.toString()) {
          //remove leading '0'
          setState(() {
            _controller.value =
                _controller.value.copyWith(text: intVal.toString());
          });
        }
      }
    });
  }

// assuming using Material
  _showMyDialog() async {
    showDialog<String>(
      context: context,
      builder: (BuildContext context) => AlertDialog(
        title: const Text('AlertDialog Title'),
        content: Text('This field is limited to ${widget.max}'),
        actions: <Widget>[
          TextButton(
            onPressed: () => Navigator.pop(context, 'OK'),
            child: const Text('OK'),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: _controller,
      keyboardType: TextInputType.number,
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文