gpt4 book ai didi

Flutter 文档非常困惑,SliverOverlapAbsorber 和 SliverOverlapInjector 有什么用?

转载 作者:行者123 更新时间:2023-12-03 02:40:33 35 4
gpt4 key购买 nike

我正在使用 this example来自 flutter 官方文档。这是最小的代码:

List<String> _tabs = ["Tab1", "Tab2"];

@override
Widget build(BuildContext context) {
return DefaultTabController(
length: _tabs.length,
child: Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
// 1/2 remove this widget and only use SliverAppBar
SliverOverlapAbsorber(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
child: SliverAppBar(
title: const Text('Books'),
pinned: true,
expandedHeight: 150.0,
bottom: TabBar(tabs: _tabs.map((name) => Tab(text: name)).toList()),
),
),
];
},
body: TabBarView(
children: _tabs.map((String name) {
return Builder(
builder: (BuildContext context) {
return CustomScrollView(
key: PageStorageKey<String>(name),
slivers: <Widget>[
// 2/2 remove this widget
SliverOverlapInjector(handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context)),
SliverFixedExtentList(
itemExtent: 48.0,
delegate: SliverChildBuilderDelegate(
(_, i) => ListTile(title: Text('Item $i')),
childCount: 30,
),
)
],
);
},
);
}).toList(),
),
),
),
);
}

问题:

如您所见,我是否删除了 SliverOverlapAbsorber连同 SliverOverlapInjector在上面的代码中,我没有看到输出有任何变化,拥有它有什么用?文档说

SliverOverlapInjector: This widget takes the overlapping behavior of the SliverAppBar, and redirects it to the SliverOverlapInjector below. If it is missing, then it is possible for the nested "inner" scroll view below to end up under the SliverAppBar even when the inner scroll view thinks it has not been scrolled. This is not necessary if the "headerSliverBuilder" only builds widgets that do not overlap the next sliver.



谁能解释一下这是什么意思以及拥有 SliverOverlapInjector 有什么用?和 SliverOverlapInjector

最佳答案

我在 NestedScrollView 使用它

NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) => [
SliverOverlapAbsorber(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
sliver: SliverAppBar()
],
body: ...
)
没有它,你 body 里的一切都会在 appbar 下。
退房 https://api.flutter.dev/flutter/widgets/NestedScrollView-class.html
据说在那里
SliverOverlapAbsorber(
// This widget takes the overlapping behavior of the SliverAppBar,
// and redirects it to the SliverOverlapInjector below. If it is
// missing, then it is possible for the nested "inner" scroll view
// below to end up under the SliverAppBar even when the inner
// scroll view thinks it has not been scrolled.
// This is not necessary if the "headerSliverBuilder" only builds
// widgets that do not overlap the next sliver.
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),

关于Flutter 文档非常困惑,SliverOverlapAbsorber 和 SliverOverlapInjector 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58084057/

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