如果你想从一个页面进入另一个页面,返回时还是保留着跳转前的最后的状态,也许你第一个想的是用Key去实现,但是那么有点麻烦,很多人的都不知道如何下手。今天我查找源码,发现了一个PageStorage这个类,它主要是用于存储状态的,不管你有多少页面,都可以保存状态信息。真是太方便了,而且使用起来也是超级简单。下面详细讲解一下PageStorageKey、PageStorageBucket和PageStorage这几个类的用法和源码解析。
Flutter系列博文链接 ↓:
工具安装:
Flutter基础篇:
Flutter进阶篇:
Dart语法系列博文链接 ↓:
Flutter安装路径:E:/develop/flutter
。
本文所讲到的文件名称为:page_storage.dart
,源码存放在本地的路径为:Flutter安装路径/packages/flutter/lib/src/widgets/page_storage.dart
。
一、PageStorageKey:
PageStorageKey继承自ValueKey,其实就是一个Key,保存状态用的。
PageStorageKey:它是定义PageStorage
的value
将保存在何处的一个ValueKey
。
Scrollable
(实际上是ScrollPosition
)以及它的相关类使用PageStorage
保存滚动偏移量。每次滚动完成时,滚动条的页面存储都会更新。
源码:
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的字符串标签。示例代码如下:
new TabBarView(
children: myTabs.map((Tab tab) {
new MyScrollableTabView(
key: new PageStorageKey<String>(tab.text), // like 'Tab 1'
tab: tab,
),
}),
)
二、PageStorageBucket:
PageStorageBucket:它是与应用程序中的页面关联的存储桶(storage bucket)。用于存储在从一个页面到另一个页面的导航之间持续存在的每个页面的状态。
源码如下:
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()这三个函数。
网友评论