gpt4 book ai didi

flutter - SliverAppBar 仅在滚动后向上推

转载 作者:IT王子 更新时间:2023-10-29 06:56:51 27 4
gpt4 key购买 nike

我有一个 SliverAppBar( float :true,固定:false)。

我想实现的是,在 SliverAppBar 开始压缩/向上滑动之前,用户必须滚动 200 像素(或其他数量)。

最佳答案

这里的问题是 pinned 值不应更改。如果您在滚动 200 像素后尝试更改它,SliverAppBar 会突然缩小。

您可以通过运行以下代码来检查这一点:

class Buster extends StatefulWidget {
@override
_BusterState createState() => _BusterState();
}

class _BusterState extends State<Buster> {
ScrollController controller;
bool isAppBarPinned;

@override
void initState() {
super.initState();
controller = ScrollController()..addListener(onScroll);
isAppBarPinned = true;
}

void onScroll() {
if (controller.position.pixels > 200) {
if (isAppBarPinned) {
setState(() => isAppBarPinned = false);
}
} else {
if (!isAppBarPinned) {
setState(() => isAppBarPinned = true);
}
}
}

@override
void dispose() {
controller.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
controller: controller,
slivers: [
SliverAppBar(
title: Text('Buster'),
floating: true,
pinned: isAppBarPinned,
),
SliverFixedExtentList(
itemExtent: 150,
delegate: SliverChildListDelegate(
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
.map((int index) => Center(child: Text('Item #$index')))
.toList(),
),
)
],
),
);
}
}

所以我认为您在这里获得的最佳选择是使用常规 AppBaranimate it manuallyTransform .

关于flutter - SliverAppBar 仅在滚动后向上推,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56328173/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com