【问题标题】:How to activate / deactivate floating button with timer in flutter?如何在颤动中激活/停用带有计时器的浮动按钮?
【发布时间】:2021-04-30 11:33:55
【问题描述】:

我需要为用户显示浮动按钮,并且我希望他们每小时只使用一次。那么我该怎么做呢?谁能指导我为浮动按钮添加倒计时或计时器方法?

这是我的浮动按钮代码

Widget _floating(BuildContext context,bool isVerified){
    if (isVerified)
    return FloatingActionButton(
      //TODO: customise according to your needs
      onPressed:() async{
      },
      tooltip: 'Increment',
      child: Icon(Icons.add),
    );
    else
      return Container();
  }

}

【问题讨论】:

    标签: flutter firebase-realtime-database floating-action-button


    【解决方案1】:

    试试这个,

    设置默认值,

      bool _buttonVisibility = false;
      DateTime lastClicked;
    

    FAB,

      if (lastClicked == null) {
        setState(() => _buttonVisibility = true);
      }
      floatingActionButton: Visibility(
        visible: _buttonVisibility,
        child: FloatingActionButton(
          child: Icon(Icons.plus_one),
          onPressed: () {
            setState(() {
              lastClicked = DateTime.now();
              _buttonVisibility = false;
              // change this seconds with `hours:1`
              new Timer(Duration(seconds: 5),
                  () => setState(() => _buttonVisibility = true));
            });
          },
        ),
      ),
    

    【讨论】:

    • 我会尝试并尽快告诉你
    【解决方案2】:

    您可以尝试使用带有 Timer() 的小部件 AnimatedPositioned 小部件来显示和隐藏按钮,例如:

    导入

    import 'dart:async';
    

    然后在StatefulWidget中:

    // Inside your StatefulWidget
    [...]
    
    Timer t;
    double position = -300.0; // or any of your value
    
    @override
    void initState() {
      super.initState();
      t = Timer(const Duration(hours: 1), () {
        _position = 0;
      }
    }
    
    [...]
    
    Widget myAnimatedFloatingActionButton(){
      AnimatedPositioned(
        duration: const Duration(milliseconds: 300),
        curve: Curves.ease,
        left: _position,
        child: FloatingActionButton(),
      );
    }
    [...]
    

    并在您的 onPressed() 中重置计时器。 当然,根据您的需要调整 _position 和 Timer 初始值

    【讨论】:

    • 好的,兄弟我试试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2020-09-22
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    相关资源
    最近更新 更多