想要在用户单击按钮后禁用按钮30秒钟,然后自动启用它

时间:2020-06-04 15:39:05

标签: android flutter authentication flutter-web one-time-password

我正在使用一个登录系统,在该系统上我通过OTP对用户进行身份验证,在此我想在用户每次单击并显示剩余时间时禁用 Resend OTP (重新发送OTP)按钮 30秒 >

3 个答案:

答案 0 :(得分:2)

使用true声明布尔值onPressedValue变量, 在onPressed参数中添加条件。

Get-Content sample.txt

答案 1 :(得分:2)

如果您想拥有一个实时计数器来向用户显示秒数,则应该使用流构建器

            StreamBuilder(
              stream: _timerStream.stream,
              builder: (BuildContext ctx,
                  AsyncSnapshot snapshot) {
                return SizedBox(
                  width: 300,
                  height: 30,
                  child:RaisedButton(
                  textColor: Theme.of(context)
                      .accentColor,
                  child: Center(
                      child:
                      snapshot.data == 0 ?
                      Text('send code again')
                          : Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(' button will be enable after ${snapshot.hasData ? snapshot.data.toString() : 30} seconds '),
                        ],)
                  ),
                  onPressed: snapshot.data == 0 ? () {
                    // your sending code method

                    _timerStream.sink.add(30);
                    activeCounter();
                  } : null,
                )
                );
              },
            )

您可以使用此link

在dartpad.dev上找到完整的代码。

答案 2 :(得分:0)

您可以尝试

全局声明这样的变量调用

True

然后单击“发送OTP”按钮将调用此方法,而其他诸如发送OTP的操作之后将调用此方法

bool shouldButtonEnabled=true;

并且在像这样重新发送OTP按钮时选中此布尔值

  _disabledButton(){
    shouldButtonEnabled=false;
    Timer(
        Duration(seconds: 30),
            () => shouldButtonEnabled=true);
  }
相关问题