Flutter tabview 刷新问题

Nic*_*olo 7 tabs tabview dart flutter

我的 main.dart 中有一个 TabBarView,每个选项卡都有一个类来显示内容(它是 listview 对象),当我在选项卡之间切换时,listview 页面每次都会刷新,tabbarview 是否正常?我不希望每次在标签之间切换时它都会刷新。

是我班级的问题吗?如何解决这个问题?代码是这样的。

    class ListWidget extends StatefulWidget {
  final catID;

  ListWidget(this.catID);


  _ListWidgetState createState() => new _ListWidgetState(catID);
}

class _ListWidgetState extends State<ListWidget> {

  var catID;

  void initState() {
    super.initState();
    _fetchListData();
  }

  @override

  Widget build(BuildContext context) {
    // TODO: implement build

    return new Scaffold(.......
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*yan 18

MahMoos 是对的,但是这里有一个例子很好......

  1. 使用 AutomaticKeepAliveClientMixin
  2. 覆盖 wantKeepAlive 属性并返回 true

`

class ListWidget extends StatefulWidget {

  @override
  _ListWidgetState createState() => _ListWidgetState();

}

class _ListWidgetState extends State<ListWidget> with 
                  AutomaticKeepAliveClientMixin<ListWidget>{ // ** here

  @override
  Widget build(BuildContext context) {
    super.build(context)
    return Container();
  }

  @override
  bool get wantKeepAlive => true; // ** and here
}
Run Code Online (Sandbox Code Playgroud)

  • 这些解决方案都不适合我 (2认同)

Mah*_*oos 6

如果我理解你的话,你会抱怨刷新,因为你需要视图在选项卡之间移动后保存它们的状态。有一个开放的问题,关于这个问题,有大约在评论中提到了这个问题的一种方式。

更新:

使用此问题有一种解决方法AutomaticKeepAliveClientMixin,您可以在本文中了解更多信息。