screenshot

我在正文和底部导航栏中有一个列表.我想在帖子列表向下滚动时用一个向下滑动的动画隐藏底部导航栏,在向上滚动时用一个向上滑动的动画隐藏底部导航栏.怎么做?

推荐答案

屏幕截图(空安全+优化)

enter image description here


代码:

class MyPage extends StatefulWidget {
  @override
  State<MyPage> createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  late final ScrollListener _model;
  late final ScrollController _controller;
  final double _bottomNavBarHeight = 56;

  @override
  void initState() {
    super.initState();
    _controller = ScrollController();
    _model = ScrollListener.initialise(_controller);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnimatedBuilder(
        animation: _model,
        builder: (context, child) {
          return Stack(
            children: [
              ListView.builder(
                controller: _controller,
                itemCount: 20,
                itemBuilder: (_, i) => ListTile(title: Text('Item $i')),
              ),
              Positioned(
                left: 0,
                right: 0,
                bottom: _model.bottom,
                child: _bottomNavBar,
              ),
            ],
          );
        },
      ),
    );
  }

  Widget get _bottomNavBar {
    return SizedBox(
      height: _bottomNavBarHeight,
      child: BottomNavigationBar(
        backgroundColor: Colors.amber,
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.call), label: 'Call'),
          BottomNavigationBarItem(icon: Icon(Icons.message), label: 'Message'),
        ],
      ),
    );
  }
}

class ScrollListener extends ChangeNotifier {
  double bottom = 0;
  double _last = 0;

  ScrollListener.initialise(ScrollController controller, [double height = 56]) {
    controller.addListener(() {
      final current = controller.offset;
      bottom += _last - current;
      if (bottom <= -height) bottom = -height;
      if (bottom >= 0) bottom = 0;
      _last = current;
      if (bottom <= 0 && bottom >= -height) notifyListeners();
    });
  }
}

Dart相关问答推荐

单元测试广播流类

我不明白在DART的`if()`语句中使用`case

捕获一个区域中的所有期货

构造函数:将预处理的参数存储在传递给最终字段的辅助变量中

Flutter Web:无法用鼠标向下滚动(drag)(Flutter 2.5+)

MappedListIterable 不是子类型

无法在Flutter中为 TabBarView 设置动态高度

dart:js 和 js 包有什么区别?

Flutter Dismissible 坚持必须从树中删除列表项

自定义声音推送通知不起作用

如何在 Flutter CustomPainter 中使用Bezier Curves绘制形状

为什么这个文件会自动创建自己 generate_plugin_registrant.dart

如何在Flutter中正确显示Snackbar?

如何从Flatter应用程序中导航到网页?(OAuth)

Flutter 在 main 中读取共享首选项,然后决定哪个启动页面?

Firebase 手机身份验证Flutter crash崩溃

Dart MD5 字符串

在 Dart 中打开类类型

如何判断一个对象是否是一个类的实例

Dart 语言中的 Console.log