Flutter SliverAppBar 吸顶效果

吸顶是常见的布局,主要使用的是CustomScrollView 和SliverApp组件实现的

页面布局

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      controller: controller.scrollController!,
      physics: const BouncingScrollPhysics(),
      slivers: [
        SliverAppBar(
          backgroundColor: Colors.blue,
          iconTheme: const IconThemeData(color: Colors.white),
          expandedHeight: controller.kExpandedHeight,
          floating: false,
          pinned: true,
          stretch: false,
          snap: false,
          stretchTriggerOffset: ScreenHelper.height(100),
          onStretchTrigger: () async {
            return;
          },
          flexibleSpace: FlexibleSpaceBar(
            centerTitle: true,
            title: Obx(() => Text(
              controller.isAppBArPinned.value ? "体重检测" : "",
              style: TextStyle(
                  color: Colors.white, fontSize: ScreenHelper.sp(16)),
            )),
            collapseMode: CollapseMode.parallax,
            stretchModes: [
              StretchMode.zoomBackground,
              StretchMode.fadeTitle,
              StretchMode.blurBackground,
            ],
            background: Container(
              color: Colors.blue,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  SizedBox(height: ScreenHelper.topSafeHeight + 18),
                  Text(
                    "2021年12月30日 13:00",
                    style: TextStyle(color: Colors.white.withOpacity(.8)),
                  ),
                  SizedBox(height: ScreenHelper.height(30)),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      Text(
                        "52.0",
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenHelper.sp(40),
                            fontWeight: FontWeight.bold),
                      ),
                      Text(
                        "kg",
                        style: TextStyle(
                            color: Colors.white, fontSize: ScreenHelper.sp(13)),
                      )
                    ],
                  ),
                  SizedBox(
                    height: ScreenHelper.height(5),
                  ),
                  Text(
                    "BMI 20.7 标准",
                    style: TextStyle(
                        color: Colors.white.withOpacity(.8),
                        fontSize: ScreenHelper.sp(14)),
                  ),
                  SizedBox(
                    height: ScreenHelper.height(15),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 6, vertical: 4),
                    decoration: BoxDecoration(
                        color: Colors.yellow,
                        borderRadius: BorderRadius.circular(50)),
                    child: Text(
                      "记录体重",
                      style: TextStyle(
                          color: Colors.white, fontSize: ScreenHelper.sp(14)),
                    ),
                  )
                ],
              ),
            ),
          ),
        ),
        SliverFixedExtentList(
          delegate: SliverChildBuilderDelegate(
              (context, index) => ListTile(
                    title: Text(
                      "测试",
                      style: TextStyle(color: AppTheme.dartTextColor),
                    ),
                  ),
              childCount: 30),
          itemExtent: 50.0,
        ),
      ],
    );
  }

因为使用的getx状态管理,所以数据的操作都在getx里

GetxController 

 ScrollController? scrollController;
  final isAppBArPinned = false.obs; //判断APPbar是否吸顶
  double kExpandedHeight = ScreenHelper.width(240);

  void onInit() {
    // TODO: implement onInit
    scrollController = ScrollController()..addListener(_onScroll);

    super.onInit();
  }


  void _onScroll() {
   
    if (scrollController!.hasClients &&
        scrollController!.offset > kExpandedHeight - kToolbarHeight) {
      isAppBArPinned.value = true;
    } else {
      isAppBArPinned.value = false;
    }
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    scrollController!.dispose();
  }

最后呈现的效果

吸顶

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值