【发布时间】:2018-12-30 04:58:44
【问题描述】:
问题:
tabView 的滚动位置未在其中一个 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'),
);
},
),
),
],
),
),
)
);
}
}
【问题讨论】:
-
这个功能是由 Flutter 开发团队添加的,所以现在它默认工作了
标签: flutter flutter-layout nestedscrollview scroll-position flutter-sliver