【发布时间】:2019-10-13 03:41:30
【问题描述】:
我有一个 SliverAppBar(浮动:true,固定:false)。
我希望在 SliverAppBar 开始压缩/向上滑动之前,用户必须滚动 200 像素(或其他数量)。
【问题讨论】:
标签: flutter flutter-sliver flutter-appbar
我有一个 SliverAppBar(浮动:true,固定:false)。
我希望在 SliverAppBar 开始压缩/向上滑动之前,用户必须滚动 200 像素(或其他数量)。
【问题讨论】:
标签: flutter flutter-sliver flutter-appbar
这里的问题是 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(),
),
)
],
),
);
}
}
所以我认为您在这里得到的最佳选择是使用常规的 AppBar 和 animate it manually 和 Transform。
【讨论】: