RaS*_*Sha 2 scrollview flutter flutter-sliver flutter-layout
SliverList我正在尝试自动滚动到.a 内的 a的末尾CustomScrollView。SliverList本身没有控制器属性,所以我必须将 a 传递ScrollController给CustomScrollView.
问题是,当我将控制器传递给CustomScrollView其行为更改时,它不再滚动外部列表,也不会导致折叠的SliverAppBar 小部件。如何自动滚动并保持以前SliverList的行为?CustomScrollView
这是我的代码:
class _MyHomePageState extends State<MyHomePage> {
ScrollController _scrollController = ScrollController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
curve: Curves.easeOut,
duration: const Duration(seconds: 1),
);
},
),
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
expandedHeight: 230.0,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text('SliverAppBar Expand'),
),
)
];
},
body: CustomScrollView(
//When controller is passed to CustomScrollView, its behavior changes
// controller: _scrollController,
slivers: [
//Some widgets are here
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return Container(
height: 80,
color: Colors.primaries[index % Colors.primaries.length],
alignment: Alignment.center,
child: Text(
'Item : $index',
),
);
},
childCount: 20,
),
),
],
),
) ,
);
}
}
Run Code Online (Sandbox Code Playgroud)
我想您可能会注意到这里示例的解释:NestedScrollView class
// The "controller" and "primary" members should be left
// unset, so that the NestedScrollView can control this
// inner scroll view.
// If the "controller" property is set, then this scroll
// view will not be associated with the NestedScrollView.
// The PageStorageKey should be unique to this ScrollView;
// it allows the list to remember its scroll position when
// the tab view is not on the screen.
Run Code Online (Sandbox Code Playgroud)
和
// This Builder is needed to provide a BuildContext that is
// "inside" the NestedScrollView, so that
// sliverOverlapAbsorberHandleFor() can find the
// NestedScrollView.
Run Code Online (Sandbox Code Playgroud)
正确的方法是获取CustomScrollView使用的控制器,而不是向其中添加新的控制器。
可以通过在CustomScrollViewBuilder上方添加) 并将控制器分配给_scrollController来完成。
...
body: NestedScrollView(
...
body: Builder(
builder: (context){
_scrollController = PrimaryScrollController.of(context);
return CustomScrollView(
...
},
),
),
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2646 次 |
| 最近记录: |