如何禁用 ElevatedButton?

发布于 2025-01-19 15:29:37 字数 2654 浏览 5 评论 0原文

我希望按钮在字段中输入 10 个字符之前处于非活动状态。当输入 10 个字符时,该按钮处于活动状态。当它不活动时,它是灰色的,当它活动时,它是蓝色的。我怎样才能做到这一点? 这是带有按钮的输入代码:

      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            child: Padding(
              padding: EdgeInsets.fromLTRB(
                  20, MediaQuery.of(context).size.height * 0, 20, 0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  TextField(
                    onChanged: (String value) {
                      setState(() {
                        _showIcon = value.isNotEmpty;
                      });
                    },
                    controller: _inputController,
                    decoration: InputDecoration(
                      focusedBorder: UnderlineInputBorder(
                        borderSide: BorderSide(color: Colors.black, width: 2.0),
                      ),
                      hintText: "(1201) 565-0123 ",
                      hintStyle: TextStyle(color: Colors.grey, fontSize: 15),
                      helperText: 'Enter your phone number',
                      helperStyle: TextStyle(color: Colors.grey, fontSize: 15),
                      suffixIcon: _showIcon
                          ? IconButton(
                        onPressed: () {
                          setState(() {
                            _inputController.clear();
                            _showIcon = false;
                          });
                        },
                        icon: const Icon(Icons.close, color: Colors.grey),
                      ) : null,
                    ),
                    keyboardType: TextInputType.number,
                    inputFormatters: [maskFormatter],
                  ),
                 Row(
                   mainAxisAlignment: MainAxisAlignment.end,
                   children: [
                     ElevatedButton(
                         onPressed: () {
                           
                         },
                         child: const Icon(Icons.arrow_forward_rounded, size: 25),
                       style: ElevatedButton.styleFrom(
                           shape: CircleBorder(),
                           padding: EdgeInsets.all(15)
                       )
                     ),
                   ],
                 )
                ],
              ),
            ),
          ),
        );
      }
    }

I want the button to be inactive until 10 characters are entered in the field. When 10 characters were entered, the button was active. And when it is inactive it is gray, and when it is active it is blue. How can I do that?
Here is the input code with the button:

      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            child: Padding(
              padding: EdgeInsets.fromLTRB(
                  20, MediaQuery.of(context).size.height * 0, 20, 0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  TextField(
                    onChanged: (String value) {
                      setState(() {
                        _showIcon = value.isNotEmpty;
                      });
                    },
                    controller: _inputController,
                    decoration: InputDecoration(
                      focusedBorder: UnderlineInputBorder(
                        borderSide: BorderSide(color: Colors.black, width: 2.0),
                      ),
                      hintText: "(1201) 565-0123 ",
                      hintStyle: TextStyle(color: Colors.grey, fontSize: 15),
                      helperText: 'Enter your phone number',
                      helperStyle: TextStyle(color: Colors.grey, fontSize: 15),
                      suffixIcon: _showIcon
                          ? IconButton(
                        onPressed: () {
                          setState(() {
                            _inputController.clear();
                            _showIcon = false;
                          });
                        },
                        icon: const Icon(Icons.close, color: Colors.grey),
                      ) : null,
                    ),
                    keyboardType: TextInputType.number,
                    inputFormatters: [maskFormatter],
                  ),
                 Row(
                   mainAxisAlignment: MainAxisAlignment.end,
                   children: [
                     ElevatedButton(
                         onPressed: () {
                           
                         },
                         child: const Icon(Icons.arrow_forward_rounded, size: 25),
                       style: ElevatedButton.styleFrom(
                           shape: CircleBorder(),
                           padding: EdgeInsets.all(15)
                       )
                     ),
                   ],
                 )
                ],
              ),
            ),
          ),
        );
      }
    }

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

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

发布评论

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

评论(2

只怪假的太真实 2025-01-26 15:29:37

您可以调用setState((){}); on on Changed更新UI,或在_InputController上添加侦听器。

ElevatedButton(
    onPressed:
        _inputController.text.length < 10 ? null : () {},
...

传递开启:null将提供禁用状态。

更新UI可以完成

TextField(
  onChanged: (String value) {
    setState(() {});
  },
....)

late final TextEditingController _inputController;

@override
void initState() {
  super.initState();
  _inputController = TextEditingController()
    ..addListener(() {
      setState(() {});
    });
}

You can call setState(() {}); on onChanged to update the UI, or add listener on _inputController.

ElevatedButton(
    onPressed:
        _inputController.text.length < 10 ? null : () {},
...

Passing onPressed:null will provide disable state.

Updating UI can be done

TextField(
  onChanged: (String value) {
    setState(() {});
  },
....)

Or

late final TextEditingController _inputController;

@override
void initState() {
  super.initState();
  _inputController = TextEditingController()
    ..addListener(() {
      setState(() {});
    });
}
时常饿 2025-01-26 15:29:37

使用类似类似的变量,然后将null传递到OnPress函数将禁用该按钮。

bool isEnabled=false;
void callbackfunction(){
   // add your logic here.
}
....
....
 TextField(
                onChanged: (String value) {
                  if (value.length == 10){
                       setState(()=> isEnabled = true;)
                  }
                  else{
                      isEnabled=false;
                  }
                  setState(() {
                    _showIcon = value.isNotEmpty;
                  });
                },
....
  Row(
               mainAxisAlignment: MainAxisAlignment.end,
               children: [
                 ElevatedButton(
                     onPressed: isEnabled ? callbackfunction : null,
                     child: const Icon(Icons.arrow_forward_rounded, size: 25),
                   style: ElevatedButton.styleFrom(
                       color: isEnabled ? Colors.blue : Colors.grey,
                       shape: CircleBorder(),
                       padding: EdgeInsets.all(15)
                   )
                 ),
               ],
             )
            ],
          ),


PS请检查我刚刚为您提供概念的语法。

Use a variable like isEnabled and passing null to the onPress function will disable the button.

bool isEnabled=false;
void callbackfunction(){
   // add your logic here.
}
....
....
 TextField(
                onChanged: (String value) {
                  if (value.length == 10){
                       setState(()=> isEnabled = true;)
                  }
                  else{
                      isEnabled=false;
                  }
                  setState(() {
                    _showIcon = value.isNotEmpty;
                  });
                },
....
  Row(
               mainAxisAlignment: MainAxisAlignment.end,
               children: [
                 ElevatedButton(
                     onPressed: isEnabled ? callbackfunction : null,
                     child: const Icon(Icons.arrow_forward_rounded, size: 25),
                   style: ElevatedButton.styleFrom(
                       color: isEnabled ? Colors.blue : Colors.grey,
                       shape: CircleBorder(),
                       padding: EdgeInsets.all(15)
                   )
                 ),
               ],
             )
            ],
          ),


P.S Please check the syntax I have just provided you the concept.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文