簡體   English   中英

Flutter - 重啟倒數計時器

[英]Flutter - Restart countdown timer

當我點擊重新發送代碼時,我試圖重新啟動倒數計時器。 我使用的 package 是timer_count_down

我試過添加:

 onTap: () {
     _controller.restart();
     _submit();
 },

但這似乎行不通。 這是我的代碼:

final CountdownController _controller =
  new CountdownController(autoStart: true);

@override
  Widget build(BuildContext context) {
    return Countdown(
            seconds: 40,
            build: (_, double time) =>
                Column(
                  children: [
                    Text(time.toString(),
                      style: TextStyle(
                        color: Theme.of(context).primaryColor,
                      ),
                    ),
                    SizedBox(height: 30,),
                    Visibility(
                      visible: time == 0,
                      child: Column(
                        children: [
                          Text('Haven\'t recieved the code?',
                            style: TextStyle(
                              color: Colors.grey
                            ),
                          ),
                          SizedBox(height: 10,),
                          InkWell(
                            onTap: () {
                              _controller.restart();
                              _submit();
                            },
                            child: Text('Resend code',
                            style: TextStyle(
                              color: Theme.of(context).primaryColor,
                              fontWeight: FontWeight.bold,
                              decoration: TextDecoration.underline,
                            ),),
                          ),
                        ],
                      ))
                  ],
                ),
          ),

有什么解決辦法嗎?

通過使用您提供的代碼和 package(由項目提供) github.com/DizoftTeam/simple_count_down/blob/master/example/lib/main.dart的示例代碼,我已經能夠重現您的問題。

在這兩種情況下,這個問題都是由於沒有將 CountdownController 分配給 Countdown 小部件引起的。

您應該將以下代碼行添加到倒計時小部件: controller: _controller,

它應該是這樣的:

return Countdown(
        controller: _controller,
        seconds: 40,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM