【问题标题】:How to disable and grey out a flutter button until the form is completely filled and then enable it如何禁用颤振按钮并将其变灰直到表单完全填充然后启用它
【发布时间】:2021-08-30 17:28:49
【问题描述】:

我一直在设计一个付款提醒应用程序,其中添加了多个文本字段。所以我想让 next 按钮变灰,除非所有的 TextField() 小部件都已正确填充。

【问题讨论】:

  • 您能否添加示例代码 sn-p 以重现该问题,
  • 那么,到目前为止,您可以做什么以及究竟在哪里卡住了?你能发布你的代码吗?

标签: flutter dart flutter-layout


【解决方案1】:

试试这个:

  final TextEditingController _controller1 = TextEditingController();
  final TextEditingController _controller2 = TextEditingController();
  final TextEditingController _controller3 = TextEditingController();
  bool _isFullyEntered = false;

  @override
  void initState() {
    super.initState();
    _controller1.addListener(() {
      _isFullyEntered = _checkFullyEntered();
      setState((){});
    });
    _controller2.addListener(() {
      _isFullyEntered = _checkFullyEntered();
      setState((){});
    });
    _controller3.addListener(() {
      _isFullyEntered = _checkFullyEntered();
      setState((){});
    });
  }

  bool _checkFullyEntered(String text){
    if(_controller1.text == '') return false;
    if(_controller2.text == '') return false;
    if(_controller3.text == '') return false;
    
    return true;
  }

  Button(
    color: _isFullyEntered ? Colors.grey : Colors.orange,
    onPressed: _isFullyEntered ? () {/*your function*/} : () {};

【讨论】:

    猜你喜欢
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    • 2019-10-28
    • 2016-05-26
    • 2018-01-22
    • 2018-03-28
    相关资源
    最近更新 更多