美文网首页
列表滚动监听、移除ListView的padding、透明导航栏

列表滚动监听、移除ListView的padding、透明导航栏

作者: 天往哪方 | 来源:发表于2020-06-02 13:48 被阅读0次
    import 'package:flutter/material.dart';
    import 'package:flutter_swiper/flutter_swiper.dart';
    
    class HomePage extends StatefulWidget {
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      List _swiperUrlList = [
        "https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1591004658&di=57f566d206833a8645c53f86b550b1b5&src=http://img8.zol.com.cn/bbs/upload/19129/19128997.jpg",
        "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1591014741937&di=5705994c6e0dfc13210e14d4d7a39a12&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fsoftbbs%2F1001%2F14%2Fc0%2F2738706_1263465972689_1024x1024.jpg",
        "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1591014741937&di=0f646d424312e16ff8eadffdbd1e96e9&imgtype=0&src=http%3A%2F%2Fimg8.zol.com.cn%2Fbbs%2Fupload%2F21909%2F21908582.jpg"
      ];
      double _appBarAlpha = 0.0;
    
      @override
      Widget build(BuildContext context) {
        final PageController _controller = PageController(initialPage: 0);
        return Scaffold(
            body: MediaQuery.removePadding(
          removeTop: true,
          context: context,
          child: Stack(
            children: <Widget>[
              NotificationListener(
                  onNotification: (scrollNotification) {
                    if (scrollNotification is ScrollUpdateNotification &&
                        scrollNotification.depth == 0) {
                      _onscroll(scrollNotification.metrics.pixels);
                    }
                  },
                  child: ListView(
                    children: <Widget>[
                      Container(
                        height: 160,
                        child: Swiper(
                          itemCount: _swiperUrlList.length,
                          autoplay: true,
                          itemBuilder: (BuildContext context, int index) {
                            return Image.network(
                              _swiperUrlList[index],
                              fit: BoxFit.fill,
                            );
                          },
                          pagination: SwiperPagination(),
                        ),
                      ),
                      Container(
                        height: 1200,
                        child: ListTile(
                          title: Text("我是内容撑大组件,这里是为了撑大ListView,可以实现上滑ListView查看透明度的变化"),
                        ),
                      )
                    ],
                  )),
              Opacity(
                opacity: this._appBarAlpha,
                child: Container(
                  height: 80,
                  decoration: BoxDecoration(
                    color: Colors.white,
                  ),
                  child: Center(
                    child: Padding(
                      padding: EdgeInsets.only(top: 20),
                      child: Text("首页"),
                    ),
                  ),
                ),
              )
            ],
          ),
        ));
      }
    
      _onscroll(offset) {
        double alpha = offset / 100;
        if (alpha < 0.0){
          alpha = 0.0;
        }
        if (alpha > 1.0){
          alpha = 1.0;
        }
        setState(() {
          this._appBarAlpha = alpha;
        });
      }
    }
    
    

    MediaQuery.removePadding 当ListView作为页面根节点的时候,它与屏幕的最上面有一个状态栏的高度,这时候就需要MediaQuery.removePadding 组件包裹,设置MediaQuery.removePadding 距离最顶端没有距离

    if (scrollNotification is ScrollUpdateNotification && scrollNotification.depth == 0)
    第一个条件是,如果接收到的通知是ScrollView更新的通知
    第二个条件是,如果是最下层的ScrollView(这里的层级与iOS的层级基本一致,最先添加到页面上的控件的层级数为0以后依次递增)

    Stack 组件,可以理解为绝对定位,所有被Stack组件包裹的组件,都是以Stack组件的左上角作为起始位置,所以当Stack组件包裹ListView和Container的时候,两个组件是互相重叠并遮盖的效果

    Opacity组件,这是一个可以调节透明度的组件,凡是被Opacity组件包裹的子组件,它的透明度都是跟随Opacity的透明度来显示的

    相关文章

      网友评论

          本文标题:列表滚动监听、移除ListView的padding、透明导航栏

          本文链接:https://www.haomeiwen.com/subject/zpduzhtx.html