【Flutter】头部导航条变换

描述:滑动列表时顶部导航栏由高边窄颜色变换

布局:列表顶部是张图片,下面是列表,滚动列表时头部图片移动上去,上面的导航条慢慢出来,导航栏上的图标颜色变换

CustomScrollView(
              slivers: <Widget>[
                SliverPersistentHeader(
                  delegate: SliverHeaderDelegate(
                    minHeight: MediaQuery.of(context).padding.top + 44,
                    maxHeight: 211,
                    onBind: (context, shrinkOffset, overlapsContent) {
                      final minExtent = MediaQuery.of(context).padding.top + 44;
                      final maxExtent = 211.0;
                      final toolbarOpacity = 1 - ((maxExtent - shrinkOffset - minExtent) / minExtent).clamp(0.0, 1.0);
                      final opacity = Interval(0.25, 1.0, curve: Curves.fastOutSlowIn).transform(toolbarOpacity);
                      final iconColor = ColorTween(begin: Colors.black, end: Colors.white).transform(1 - opacity);
                      return FlexibleSpaceBar.createSettings(
                        minExtent: minExtent,
                        maxExtent: maxExtent,
                        currentExtent: max(minExtent, maxExtent - shrinkOffset),
                        toolbarOpacity: toolbarOpacity,
                        child: GestureDetector(
                          onTap: (){
                            print('视频播放界面');
                          },
                          child: Stack(
                            children: <Widget>[
                              ImageWidget(
                                width: MediaQuery.of(context).size.width,
                                height: 211,
                                imageStr: state.data.coverImage,
                              ),
                              Positioned.fromRect(
                                child: Image.asset('assets/ic_play.png'),
                                rect: Rect.fromLTWH(0, 0, MediaQuery.of(context).size.width, 211),
                              ),

                              Container(
                                decoration: BoxDecoration(
                                  color: Colors.white.withOpacity(opacity),
                                  boxShadow: [
                                    BoxShadow(
                                      color: Color(0x33999999).withOpacity(opacity.truncate().toDouble()),
                                      blurRadius: 5,
                                    )
                                  ],
                                ),
                                child: SafeArea(
                                  bottom: false,
                                  child: Container(
                                    height: 44,
                                    child: Row(
                                      children: <Widget>[
                                        Expanded(
                                          child: Container(
                                            alignment: Alignment.centerLeft,
                                            child: IconButton(
                                              icon: const Icon(Icons.arrow_back_ios, size: 20),
                                              color: iconColor,
                                              onPressed: () {
                                                Navigator.maybePop(context);
                                              },
                                            ),
                                          )),
                                        Expanded(
                                          child: Container(
                                            alignment: Alignment.center,
                                            child: Text(
                                              '直播详情',
                                              style: TextStyle(
                                                fontSize: 18,
                                                color: res.Colors.majorTextColor.withOpacity(opacity),
                                              ),
                                            ),
                                          )),
                                        Expanded(
                                          child: Container(),
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ),

                            ],
                          ),
                        )
                      );
                    },
                  ),
                  floating: false,
                  pinned: true,
                ),
                SliverList(
                  delegate: SliverChildListDelegate([
                    _buildTimeWidget(state),
                    _buildliveDataWidget(state),
                    SizedBox(height: 10,),
                    _buildSalesDataWidget(state),
                  ]),
                )
              ],
            )

class SliverHeaderDelegate extends SliverPersistentHeaderDelegate {
  SliverHeaderDelegate({
    @required this.minHeight,
    @required this.maxHeight,
    @required this.onBind,
  });

  final double minHeight;
  final double maxHeight;
  final BuildCallback onBind;
  Widget _child;

  @override
  double get minExtent => minHeight;

  @override
  double get maxExtent => max(maxHeight, minHeight);

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    _child = onBind(context, shrinkOffset, overlapsContent);
    return SizedBox.expand(child: _child);
  }

  @override
  bool shouldRebuild(SliverHeaderDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight || minHeight != oldDelegate.minHeight || _child != oldDelegate._child;
  }
}

typedef BuildCallback = Widget Function(BuildContext context, double shrinkOffset, bool overlapsContent);

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值