简体   繁体   English

Flutter 中的动画浮动操作按钮

[英]Animating Floating Action Button in Flutter

I'm trying to add in an animated where when the user scrolls down, the fab will animated out and disappear, and when scroll up, the fab reappear again.我正在尝试添加动画,当用户向下滚动时,工厂将动画出来并消失,当向上滚动时,工厂再次出现。

Here is my code:这是我的代码:

return Scaffold(
      floatingActionButton: AnimatedContainer(
        curve: Curves.easeIn,
        duration: const Duration(seconds: 1),
        child: Opacity(
          opacity: _fabVisible ? 1 : 0,
          child: FloatingActionButton(
            onPressed: () {},
            child: Icon(
              Icons.add,
              color: Colors.black,
            ),
            backgroundColor: Colors.white,
          ),
        ),
      ),

What the code is doing now is the fab is hiding properly, but it doesn't animate the opacity, it just pop in and out without animation, please help!代码现在正在做的是工厂正确隐藏,但它不会为不透明度设置动画,它只是在没有 animation 的情况下弹出和弹出,请帮助!

thank you谢谢你

try to use scrollnotification for detecting scroll down (increase of pixels) and scroll up (decrease of pixels), when scroll down, setState _fabVisible = 0, vice versa:尝试使用scrollnotification来检测向下滚动(增加像素)和向上滚动(减少像素),向下滚动时,setState _fabVisible = 0,反之亦然:

Expanded(
            child: NotificationListener<ScrollNotification>(
              onNotification: (scrollNotification) {
                if (scrollNotification is ScrollStartNotification) {
                  _onStartScroll(scrollNotification.metrics);
                } else if (scrollNotification is ScrollUpdateNotification) {
                  _onUpdateScroll(scrollNotification.metrics);
                } else if (scrollNotification is ScrollEndNotification) {
                  _onEndScroll(scrollNotification.metrics);
                }
              },
              child: ListView.builder(
                itemCount: 30,
                itemBuilder: (context, index) {
                  return ListTile(title: Text("Index : $index"));
                },
              ),
            ),
          ),

_onStartScroll(ScrollMetrics metrics) {
      setState(() {
        message = "Scroll Start";
      });
    }
_onUpdateScroll(ScrollMetrics metrics) {
      setState(() {
        message = "Scroll Update";
      });
    }
_onEndScroll(ScrollMetrics metrics) {
      setState(() {
        message = "Scroll End";
      });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM