问题

我用TabControllerTabBarView里渲染了两个ListViews.

如何将状态(因为没有更好的词)保持在ListView之间,以便:小部件不会重新生成,2.)标签之间会记住ListView的位置.

相关代码

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

推荐答案

长话短说,将PageStorageKey()用于ListView或其祖先之一,即您的容器小部件:

child: Container(
    key: PageStorageKey(page.country),
    child: Newsfeed(country: page.country),
),

请在此处查看详细信息:

Flutter相关问答推荐

如果文档确实存在,则FiRestore将";False";返回到";if-Document-Existes";...还是我说错了?

video_player在Android上返回401 Unauthorized with Bunny CDN

从外部类动态更改IconButton的图标

如何管理枚举类型的阻塞事件?

应为标识符,并应为查找)错误

Flutter卡内容未在水平列表视图中居中

Flutter飞溅屏幕即使在修改后仍显示默认徽标和背景色

处理 flutter/dart 模型中的空值

没有为类型 'Widget' 定义运算符 '[]' .try 定义运算符[]

断言失败:std::move(hal_2_1_verifier).Run(). 初始化,LE音频客户端至少需要Bluetooth音频HAL V2.1

FutureProvider 不返回数据给 UI

如何在Flutter中显示ListView Builder区域外的列表数据?

是否可以在 flutter 应用程序中使用 jetpack compose?

切换 Firebase 项目后 Flutter FirebaseAuthException 'configuration-not-found'

如何在 Flutter 中的 BackdropFilter.blur 中制作一个 100x100 的清晰孔

如何在 Flutter 中将 ScaffoldMessenger 中的 Snackbar 显示为 Widget?

http响应在Flutter 中返回307

Flutter Firebase:如何判断返回值是否是特定对象

Firebase Firestore 查询

在 Flutter 中更新对象实例属性的最佳实践是什么?该实例嵌套在提供程序类的映射中,如下所示