其他分享
首页 > 其他分享> > flutter 效果实现 —— 无AppBar下列表滚动时状态栏透明度变化

flutter 效果实现 —— 无AppBar下列表滚动时状态栏透明度变化

作者:互联网

效果

此效果参考自 twitter 与 gmail

image

实现原理

通过 Stack 组件,在最上层页面的状态栏位置用一个白色的容器占位,在列表滚动时,根据监听到的滚动位置动态调整其透明度。

代码

import 'package:flutter/material.dart';

class StatusBarOpacityPage extends StatefulWidget {
  const StatusBarOpacityPage({Key? key}) : super(key: key);

  @override
  State<StatusBarOpacityPage> createState() => _StatusBarOpacityPageState();
}

class _StatusBarOpacityPageState extends State<StatusBarOpacityPage> with SingleTickerProviderStateMixin{

  final ScrollController _scrollController = ScrollController();

  double statusBarOpacity = 1;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var screenPadding = MediaQuery.of(context).padding;
    return Stack(
      children: [
        NotificationListener(
          onNotification: (ScrollNotification notification) {
            var pixels = notification.metrics.pixels;
            final statusBarHeight= screenPadding.top;
            if (pixels > 0 && pixels <= statusBarHeight) {
              setState(() {
                print(1 - pixels/statusBarHeight *0.5);
                statusBarOpacity = 1 - pixels/statusBarHeight *0.3;
              });
            }
            //若返回值为 true,则不会继续往上冒泡
            return false;
          },
          child: ListView(
            controller: _scrollController,
            children: [
              Container(
                height: 1500,
                color: Colors.yellow,
                child: Text("Hello"),
              )
            ],
          ),
        ),
        Positioned(
          child: Container(
            color: Colors.white.withOpacity(statusBarOpacity),
            height: screenPadding.top,
          ),
          top: 0,
          left: 0,
          right: 0,
        )
      ],
    );
  }
}

标签:滚动,状态栏,final,AppBar,flutter,key,override,pixels,StatusBarOpacityPage
来源: https://www.cnblogs.com/lemos/p/16581865.html