第一次点击颤动后如何禁用按钮?

8 dart flutter

同时多次点击按钮,多次打开页面。如何解决这个问题?我还在我的应用程序中上传了 gif 文件(双击图像)。

     Container(
                            padding: EdgeInsets.all(10.0),
                            child: ButtonTheme(
                              minWidth: 10.0,
                              height: 40.0,
                              child: RaisedButton(
                                child: Text(
                                  AppTranslations.of(context)
                                      .text("loginpage_button"),
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 15.0),
                                ),
                                onPressed: () async{
                                  (isOffline)
                                    ? _showSnackBar()
                                      : checking2(usernameController, context, _url);
                                },
                                color: Colors.blue,
                                padding: EdgeInsets.all(20.0),
                              ),
                            ),
margin: EdgeInsets.only(top: 0.0),
Run Code Online (Sandbox Code Playgroud)

)

我使用了这段代码,它可以工作,但是用户输入的用户名不正确,用户无法单击第二个类型的按钮。这是我的代码。

onPressed: () async {
 if (_firstClick) {
_firstClick = false;
(isOffline)
? _showSnackBar()
: checking2(usernameController, context, _url);
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Cop*_*oad 12

创建一个bool变量,该变量将在按下按钮时出现true(因此,初始值设置为false)。

bool _clicked = false; 
  
@override
Widget build(BuildContext context) {
  return Scaffold(
    body: RaisedButton(
      child: Text('Button'),
      onPressed: _clicked
        ? null
        : () {
            setState(() => _clicked = true); // set it to true now
          },
    ),
  );
}
Run Code Online (Sandbox Code Playgroud)


Maz*_*him 6

您可以使用bool变量来保存您的状态RaisedButton

首先创建变量 a 设置其初始值:

  var _firstPress = true ;
Run Code Online (Sandbox Code Playgroud)

然后_firstPress在你的onPressed函数中添加:

                  Container(
                    padding: EdgeInsets.all(10.0),
                    child: ButtonTheme(
                      minWidth: 10.0,
                      height: 40.0,
                      child: RaisedButton(
                        child: Text(
                          AppTranslations.of(context)
                              .text("loginpage_button"),
                          style: TextStyle(
                              color: Colors.white, fontSize: 15.0),
                        ),
                        onPressed: () async{
                        // This is what you should add in your code
                        if(_firstPress){
                          _firstPress = false ;
                          (isOffline) ? _showSnackBar() :checking2(usernameController, context, _url);
                          }
                        },
                        color: Colors.blue,
                        padding: EdgeInsets.all(20.0),
                      ),
                    ),

                    margin: EdgeInsets.only(top: 0.0),
                    )
Run Code Online (Sandbox Code Playgroud)

这样你的onPressed函数只会响应 的RaisedButton第一次点击。


小智 5

基于计算时差在我的应用程序中解决了这个问题。

首先,声明一个 DateTime 变量并定义函数如下:-

DateTime loginClickTime;

bool isRedundentClick(DateTime currentTime){
if(loginClickTime==null){
  loginClickTime = currentTime;
  print("first click");
  return false;
}
print('diff is ${currentTime.difference(loginClickTime).inSeconds}');
if(currentTime.difference(loginClickTime).inSeconds<10){//set this difference time in seconds
  return true;
}

loginClickTime = currentTime;
return false;
}
Run Code Online (Sandbox Code Playgroud)

在登录按钮中调用如下函数来检查冗余:-

RaisedButton(
        child:Text('Login'),
        onPressed: (){
          if(isRedundentClick(DateTime.now())){
            print('hold on, processing');
            return;
          }
          print('run process');
          },
      )
Run Code Online (Sandbox Code Playgroud)

  • 它给出:“必须初始化不可为空的实例字段‘loginClickTime’。” 错误。 (2认同)