vir*_*hjx 5 scroll-position nestedscrollview flutter flutter-sliver flutter-layout
当其中一个tabView滚动到顶部时(显示sliverAppBar),tabView的滚动位置无法正确还原。另一个tabView也将滚动到顶部(失去其先前的滚动位置)。
使用可折叠的应用程序栏(sliverAppBar)时如何保留tabView的滚动位置?
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new DefaultTabController(
length: 2,
child: Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
title: Text('Example'),
pinned: true,
floating: true,
forceElevated: innerBoxIsScrolled,
bottom: TabBar(
tabs: <Widget>[
Tab(text: 'One',),
Tab(text: 'Two'),
],
),
),
];
},
body: TabBarView(
children: <Widget>[
Center(
key: PageStorageKey<String>('one'),
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
return new ListTile(
title: new Text('One Item $index'),
);
},
),
),
Center(
key: PageStorageKey<String>('two'),
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
return new ListTile(
title: new Text('Two Item $index'),
);
},
),
),
],
),
),
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
这实际上是一个已知问题并且仍然悬而未决。尽管有此线程中提到的解决方法:
这是使用包的解决方法
extended_nested_scroll_view。至此,我还没有遇到任何问题(至少现在是这样)。因此,在 flutter 解决这个问题之前,这种解决方法似乎就足够了。仅在 Android 上测试。其基本要点是:
NestedScrollView从包装中使用extended_nested_scroll_view- 将每个选项卡视图包裹在一个
StatefulWidgetwithAutomaticKeepAliveClientMixin和中wantKeepAlive => true- 在其中
StatefulWidget,将可滚动内容包装在NestedScrollViewInnerScrollPositionKeyWidgetinnerScrollPositionKeyBuilder和中使用的键值NestedScrollViewInnerScrollPositionKeyWidget必须匹配。
| 归档时间: |
|
| 查看次数: |
396 次 |
| 最近记录: |