17-flutter导航栏渐变效果

本文介绍如何在Flutter应用中实现AppBar随着ListView滚动而逐渐变得透明的效果。通过监听ListView滚动通知,动态调整AppBar的透明度,使应用界面更加平滑过渡。

MediaQuery.removePadding 移除顶部的 padding

import 'package:flutter/material.dart';
// 导入swiper 组件
import 'package:flutter_swiper/flutter_swiper.dart';

const APPBAR_SCROLL_OFFSET = 200;
class HomePage extends StatefulWidget
{
    // 重写Create State 方法
    @override
    _HomePageState createState() => _HomePageState();

}


class _HomePageState extends State<HomePage>
{

    //定义一个AppBar 的透明度默认值
    double appBarAlpha = 0;

    List _imgUrls = [
      'http://pic1.nipic.com/2008-12-05/200812584425541_2.jpg',
      'http://pic18.nipic.com/20111129/4155754_234055006000_2.jpg',
      'http://b-ssl.duitang.com/uploads/item/201412/25/20141225204152_aYEc3.jpeg'
    ];

    @override  
    Widget build(BuildContext context) {
      return Scaffold(
        // MediaQuery.removePadding 移除顶部的 padding
        // 使用Stack 的作用就是让AppBar 叠加在上面
        body: Stack(
        children: <Widget>[
          MediaQuery.removePadding(
          removeTop: true,
          context: context,
          // 监听列表的滚动
          child: NotificationListener(
            
            // 滚动的回调
            onNotification: (scrollNotifation){
                // 判断ListView滚动的效果
                if (scrollNotifation is ScrollUpdateNotification && scrollNotifation.depth == 0){
                  // 滚动且是列表滚动的的的时候
                  double offset =  scrollNotifation.metrics.pixels;
                  print(offset);
                  double  alpha =  offset / APPBAR_SCROLL_OFFSET;
                  if(alpha < 0){
                    alpha =  0;
                  }else if (alpha > 0){
                    alpha =  1;
                  }
                  
                  setState(() {
                    appBarAlpha = alpha;
                  });
                  print(alpha);
                   
                }
              
            },  
            child: ListView(
            children: <Widget>[
                Column(
                children: <Widget>[
                  Container(
                    height: 160,
                    // 
                    child:Swiper(
                        itemCount: _imgUrls.length,
                        autoplay: true,
                        itemBuilder: (BuildContext context,int index){
                          // 获取图片
                          return Image.network(
                              _imgUrls[index],
                              // 适配方式
                              fit: BoxFit.fill,
                          );
                        },
                        // 添加一个页码指示器
                        pagination: SwiperPagination(),
                    ),
                  ),

                  Container(
                    height: 800,
                    child: ListTile(title: Text("Nice"),),
                  )

                ],
              ),
              ],

            ),
            ),
          ),
          // 创建AppBar
          Opacity(
            // 当列表滚动的时候 改变其透明度的值
            opacity: appBarAlpha,
            child: Container(
              height: 80,
              decoration: BoxDecoration(
                color: Colors.white
              ),
              child: Center(
                child: Padding(padding: EdgeInsets.only(top: 20),
                child: Text('首页'),),
                
              ),
            ),
          )



          ],
          
        )
      );
    }

}
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值