我在我的main.dart中有一个tabbarview,每个标签页都有一个类来显示内容(它是listview对象),当我在标签页之间切换时,列表视图页面每次都会刷新,这对于tab栏视图来说是正常的吗?我不希望每次我在标签间切换时,它都会刷新。
是我的课出了问题吗?如何解决这个问题?代码是这样的。
class ListWidget extends StatefulWidget {
final catID;
ListWidget(this.catID);
_ListWidgetState createState() => new _ListWidgetState(catID);
}
class _ListWidgetState extends State {
var catID;
void initState() {
super.initState();
_fetchListData();
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(.......
}发布于 2018-07-13 16:07:00
如果我很好地理解您,您会抱怨刷新,因为您需要视图在选项卡之间移动后保存其状态。有一个未解决的问题在这个主题上,有一种方法可以绕过评论中提到的这个问题。
更新:
有一个解决方法可以解决此问题,方法是使用AutomaticKeepAliveClientMixin中您可以了解到更多信息。文章。
发布于 2018-10-17 20:36:38
MahMoos是对的,但是在这里有一个例子是很好的……
使用AutomaticKeepAliveClientMixin
重写wantKeepAlive属性并返回true
`
class ListWidget extends StatefulWidget {
@override
_ListWidgetState createState() => _ListWidgetState();
}
class _ListWidgetState extends State with
AutomaticKeepAliveClientMixin{ // ** here
@override
Widget build(BuildContext context) {
super.build(context)
return Container();
}
@override
bool get wantKeepAlive => true; // ** and here
}发布于 2019-03-20 14:27:14
如果希望在更改选项卡时不刷新选项卡视图数据,则应使用
AutomaticKeepAliveClientMixin
class BaseScreen extends StatefulWidget {
BaseScreen(this.title, this.listener, {Key key}) : super(key: key);
}
class BaseScreenState extends State with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
screenWidth = MediaQuery.of(context).size.width;
screenHeight = MediaQuery.of(context).size.height;
primaryColor = Theme.of(context).primaryColor;
textTheme = Theme.of(context).textTheme;
return Scaffold(
key: scaffoldKey,
appBar: getAppBar(),
body: Container(),
);
}
@override
bool get wantKeepAlive => true;
}我也面临着同样的问题本教程帮了我。编码愉快。
https://stackoverflow.com/questions/51319804
复制相似问题