在TabBar选项卡之间导航时,我遇到奇怪的行为;我总共有8个标签。
如果我在两个标签之间进行导航(轻击或轻扫):
选项卡1->选项卡2->选项卡3->选项卡4->选项卡5->选项卡6->选项卡7->选项卡,然后按预期工作。
但是,如果在导航过程中我跳过了相邻的标签,例如直接从tab1转到tab4,如下所示:
tab1-> tab4或Tab6-> Tab2,然后会发生奇怪的行为,并改为加载其他选项卡或状态。
我有两个类,ExploreList,其中包含选项卡,并且它调用NewsStream对象。这是代码:
class ExploreList extends StatefulWidget {
@override
createState() => ExploreListState();
}
class ExploreListState extends State<ExploreList> {
@override
void initState() {
setState(() {});
scrollControllerExploreList = ScrollController(initialScrollOffset: 84);
super.initState();
}
@override
void dispose() {
scrollControllerExploreList.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
setState(() {});
return DefaultTabController(
length: 8,
child: Scaffold(
appBar: AppBar(
title: Text('Top Headlines')
bottom: const TabBar(
isScrollable: true,
tabs: [
Tab(text: 'World'),
Tab(text: 'Nation'),
Tab(text: 'Business'),
Tab(text: 'Entertainment'),
Tab(text: 'Health'),
Tab(text: 'Science'),
Tab(text: 'Sports'),
Tab(text: 'Technology'),
],
),
),
body: SafeArea(
bottom: false,
child: TabBarView(
children: [
NewsStream("world"),
NewsStream("nation"),
NewsStream("business"),
NewsStream("entertainment"),
NewsStream("health"),
NewsStream("science"),
NewsStream("sports"),
NewsStream("technology"),
],
),
),
),
);
}
}
这是包含集团的NewsStream类:
ScrollController scrollControllerNewsStream;
class NewsStream extends StatefulWidget {
final String topic;
const NewsStream(this.topic);
@override
createState() => NewsStreamState();
}
class NewsStreamState extends State<NewsStream> {
@override
void initState() {
setState(() {});
scrollControllerNewsStream = ScrollController(initialScrollOffset: 84);
super.initState();
}
@override
void dispose() {
scrollControllerNewsStream.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
bloc.fetchExploreNews(widget.topic);
setState(() {});
return SafeArea(
child: CustomScrollView(
controller: scrollControllerNewsStream,
slivers: <Widget>[
SliverToBoxAdapter(
),
streamBuild(bloc.exploreNews),
],
),
);
}
}