保留标签视图页面之间的状态 [英] Preserving state between tab view pages
问题描述
我在 TabBarView $内有两个
ListViews
渲染c $ c>使用 TabController
。
I have two ListViews
rendering inside of a TabBarView
using a TabController
.
如何在两次之间保留状态(缺少更好的词)每个 ListView
,以便:1.)窗口小部件不重建,以及2.)在两次之间记住 ListView
位置
How do I preserve state (for lack of a better word) between each ListView
so that: 1.) the Widgets don't rebuild and 2.) the ListView
position is remembered between tabs.
class AppState extends State<App> with SingleTickerProviderStateMixin {
TabController _tabController;
@override
void initState() {
super.initState();
_tabController = new TabController(
vsync: this,
length: _allPages.length,
);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
Widget _buildScaffold(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('headlines'),
bottom: new TabBar(
controller: _tabController,
isScrollable: true,
tabs: _allPages
.map((_Page page) => new Tab(text: page.country))
.toList()),
),
body: new TabBarView(
controller: _tabController,
children: _allPages.map((_Page page) {
return new SafeArea(
top: false,
bottom: false,
child: new Container(
key: new ObjectKey(page.country),
child: new Newsfeed(country: page.country),
),
);
}).toList()),
);
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'news app',
home: _buildScaffold(context),
);
}
}
插图gif
https://media.giphy.com/media/2ysWhzqHVqL1xcBlBE/giphy .gif
推荐答案
长话短说,对您的ListView或其祖先之一使用PageStorageKey()您所用的容器小部件:
Long story short, use a PageStorageKey() for your ListView or one of it's ancestors, the Container widget in your case:
child: new Container(
key: new PageStorageKey(page.country),
child: new Newsfeed(country: page.country),
),
在此处查看详细信息:
https://docs.flutter.io/flutter/widgets/PageStorage-class。 html
https://docs.flutter.io/flutter/widgets/ ScrollView / controller.html
这篇关于保留标签视图页面之间的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!