我有一个包含多个“TabView”小部件的“NestedScrollView”,每个选项卡都有一个列表构建器。问题是,当我滚动浏览特定选项卡中的一个列表时,滚动位置会影响其他选项卡中的所有其他列表。
即使我确实将“ScrollController”添加到每个列表视图(在选项卡中),选项卡滚动内的 listBuilder 也会与“NestedScrollView”分开,这是一个示例代码:
import 'package:flutter/material.dart';
void main() => runApp(
MaterialApp(
home: MyApp()
,
)
);
class MyApp extends StatefulWidget{
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
TabController tabController;
Widget _tabBarView;
@override
void initState() {
super.initState();
tabController = TabController(length: 2, vsync: this,);
_tabBarView = TabBarView(
children: [
DemoTab(),
DemoTab(),
]);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: NestedScrollView(
controller: ScrollController(keepScrollOffset: true),
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverList(
delegate: SliverChildListDelegate(
[
Container(height: 300, color: Colors.blue)
]
),
),
];
},
body: DefaultTabController(
length: 2,
child: Column(
children: <Widget>[
Expanded(
child: Container(
child: _tabBarView
),
),
],
),
)
),
);
}
}
class DemoTab extends StatefulWidget{
DemoTabState createState() => DemoTabState();
}
class DemoTabState extends State<DemoTab> with AutomaticKeepAliveClientMixin<DemoTab>{
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
return ListView.builder(
key: UniqueKey(),
itemBuilder: (b, i) {
return Container(
height: 50,
color: Colors.green,
margin: EdgeInsets.only(bottom: 3),
child: Text(i.toString(),),
);
}, itemCount: 30,) ;
}
}