前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flutter进阶篇(6)-- PageStorageKey、PageStorageBucket和PageStorage使用详解

Flutter进阶篇(6)-- PageStorageKey、PageStorageBucket和PageStorage使用详解

作者头像
AWeiLoveAndroid
发布2019-07-11 10:36:27
4.2K0
发布2019-07-11 10:36:27
举报


Flutter安装路径:E:/develop/flutter

本文所讲到的文件名称为:page_storage.dart,源码存放在本地的路径为:Flutter安装路径/packages/flutter/lib/src/widgets/page_storage.dart

一、PageStorageKey:

PageStorageKey继承自ValueKey,其实就是一个Key,保存状态用的。

PageStorageKey:它是定义PageStoragevalue将保存在何处的一个ValueKeyScrollable(实际上是ScrollPosition)以及它的相关类使用PageStorage保存滚动偏移量。每次滚动完成时,滚动条的页面存储都会更新。

源码:

代码语言:javascript
复制
class PageStorageKey<T> extends ValueKey<T> {
  /// Creates a [ValueKey] that defines where [PageStorage] values will be saved.
  const PageStorageKey(T value) : super(value);
}

例如,为了确保在重新创建TabbarView时恢复下面每个MyScrollableTabView中scrollable 的滚动偏移量(scroll offsets),我们指定了pageStorageKey,其值是Tabs的字符串标签。示例代码如下:

代码语言:javascript
复制
new TabBarView(
   children: myTabs.map((Tab tab) {
    new MyScrollableTabView(
      key: new PageStorageKey<String>(tab.text), // like 'Tab 1'
       tab: tab,
     ),
   }),
 )

二、PageStorageBucket:

PageStorageBucket:它是与应用程序中的页面关联的存储桶(storage bucket)。用于存储在从一个页面到另一个页面的导航之间持续存在的每个页面的状态。

源码如下:

代码语言:javascript
复制
class PageStorageBucket {
  static bool _maybeAddKey(BuildContext context, List<PageStorageKey<dynamic>> keys) {
    final Widget widget = context.widget;
    final Key key = widget.key;
    if (key is PageStorageKey)
      keys.add(key);
    return widget is! PageStorage;
  }

  List<PageStorageKey<dynamic>> _allKeys(BuildContext context) {
    final List<PageStorageKey<dynamic>> keys = <PageStorageKey<dynamic>>[];
    if (_maybeAddKey(context, keys)) {
      // 访问存储元素
      context.visitAncestorElements((Element element) {
        return _maybeAddKey(element, keys);
      });
    }
    return keys;
  }

  // 存储实体标识符
  _StorageEntryIdentifier _computeIdentifier(BuildContext context) {
    return _StorageEntryIdentifier(_allKeys(context));
  }

  Map<Object, dynamic> _storage;

  /// Write the given data into this page storage bucket using the
  /// specified identifier or an identifier computed from the given context.
  /// The computed identifier is based on the [PageStorageKey]s
  /// found in the path from context to the [PageStorage] widget that
  /// owns this page storage bucket.
  ///
  /// If an explicit identifier is not provided and no [PageStorageKey]s
  /// are found, then the `data` is not saved.
  void writeState(BuildContext context, dynamic data, { Object identifier }) {
    _storage ??= <Object, dynamic>{};
    if (identifier != null) {
      _storage[identifier] = data;
    } else {
      final _StorageEntryIdentifier contextIdentifier = _computeIdentifier(context);
      if (contextIdentifier.isNotEmpty)
        _storage[contextIdentifier] = data;
    }
  }

  /// Read given data from into this page storage bucket using the specified
  /// identifier or an identifier computed from the given context.
  /// The computed identifier is based on the [PageStorageKey]s
  /// found in the path from context to the [PageStorage] widget that
  /// owns this page storage bucket.
  ///
  /// If an explicit identifier is not provided and no [PageStorageKey]s
  /// are found, then null is returned.
  dynamic readState(BuildContext context, { Object identifier }) {
    if (_storage == null)
      return null;
    if (identifier != null)
      return _storage[identifier];
    final _StorageEntryIdentifier contextIdentifier = _computeIdentifier(context);
    return contextIdentifier.isNotEmpty ? _storage[contextIdentifier] : null;
  }
}

这里面用到了一个私有的类:_StorageEntryIdentifier。这个类比较简单,构造里面传入一个List<PageStorageKey<dynamic>>的集合,里面存放的是PageStorageKey<dynamic>,isNotEmpty函数判断传入的List集合是否为空,后面有三个函数就没什么好说的了,基本都是模板写法,差不多的,分别是:重写操作符==,hashcode,toString()这三个函数。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.07.10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、PageStorageKey:
  • 二、PageStorageBucket:
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档