美文网首页
Flutter Key

Flutter Key

作者: hqdoremi22 | 来源:发表于2019-11-05 15:43 被阅读0次
image.png

LocalKey

应用于拥有相同父 Element 的小部件进行比较的情况

1. UniqueKey

A key that is only equal to itself

2. ValueKey

A key that uses a value of a particular type to identify itself

3. ObjectKey

A key that takes its identity from the object used as its value

/**
 * Check whether two references are to the same object.
 */
external bool identical(Object a, Object b);

4.PageStorageKey

当你有一个滑动列表,你通过某一个 Item 跳转到了一个新的页面,当你返回之前的列表页面时,你发现滑动的距离回到了顶部。这时候,给 Sliver 一个 PageStorageKey!它将能够保持 Sliver 的滚动状态。

/// A [ValueKey] that defines where [PageStorage] values will be saved.
///
/// [Scrollable]s ([ScrollPosition]s really) use [PageStorage] to save their
/// scroll offset. Each time a scroll completes, the scrollable's page
/// storage is updated.
///
/// [PageStorage] is used to save and restore values that can outlive the widget.
/// The values are stored in a per-route [Map] whose keys are defined by the
/// [PageStorageKey]s for the widget and its ancestors. To make it possible
/// for a saved value to be found when a widget is recreated, the key's values
/// must not be objects whose identity will change each time the widget is created.
///
/// For example, to ensure that the scroll offsets for the scrollable within
/// each MyScrollableTabView below are restored when the [TabBarView]
/// is recreated, we've specified [PageStorageKey]s whose values are the
/// tabs' string labels.

TabBarView(
   children: myTabs.map((Tab tab) {
     MyScrollableTabView(
       key: PageStorageKey<String>(tab.text), // like 'Tab 1'
       tab: tab,
     ),
   }),
 )

Demo:

String a = 'ab' + 'c';
  String b = 'abc';
  UniqueKey uKey1 = new UniqueKey();
  UniqueKey uKey2 = new UniqueKey();
  ValueKey vKey1 = new ValueKey('abc');
  ValueKey vKey2 = new ValueKey('ab' + 'c');
  ObjectKey oKey1 = new ObjectKey('abc');
  ObjectKey oKey2 = new ObjectKey('ab' + 'c');

  print(
      '${a == b} ${identical(a, b)} UniqueKey:${uKey1 == uKey2} ValueKey:${vKey1 == vKey2} ObjectKey:${oKey1 == oKey2}');

结果:

true false UniqueKey:false ValueKey:true ObjectKey:false

GlobalKey

能够跨 Widget 访问状态,GlobalKey会在组件Mount阶段把自身放到一个Map里面缓存起来,代价较昂贵,谨慎使用

相关文章

  • Flutter key

    新创建一个Flutter Application的时候,默认生成的代码里面有这么一段 title很好理解,给App...

  • Flutter ---- Key

    Key Key 本身是一个抽象类 LocalKey:用作diff算法的核心所在,用作Element和Widget进...

  • Flutter Key

    什么是key Key 能够帮助开发者在 Widget tree 中保存状态。 Flutter | 深入浅出Key ...

  • Flutter Key

    LocalKey 应用于拥有相同父 Element 的小部件进行比较的情况 1. UniqueKey A key ...

  • The Key of Widget in Flutter

    The Key of Widget in Flutter 当我们刚开始使用Flutter,我们在继承Statele...

  • Flutter 详解 Key

    转载 原文链接:https://juejin.im/post/6863300824660082701作者: fgy...

  • Flutter详解 Key

    Key 是什么 key是用来作为Widget、Element和SemanticsNode的标示,仅仅用来更新wid...

  • flutter GlobalKey、Key

    Key 局部key只会比较树中相同位置的widget,如children中index 为1的element不会复用...

  • flutter的key

    Key本身是一个抽象类,用作diff的核心算法比较Widget。-ValueKey 以一个数据作为Key-Obje...

  • Flutter中Widget之key原理探索

    开始 在Flutter的每个Widget中, 都会有key这个可选属性. 在刚开始学习flutter时, 基本就直...

网友评论

      本文标题:Flutter Key

      本文链接:https://www.haomeiwen.com/subject/bpxebctx.html