Commit 74575775 authored by Hixie's avatar Hixie

Introduce an explicit Key type.

This fixes some theoretical bugs whereby we were using hashCode to try
to get unique keys for objects, but really we wanted object identity.
It also lays the groundwork for a new GlobalKey concept.

I tried to keep the impact on the code minimal, which is why the "Key"
constructor is actually a factory that returns a StringKey. The code
has this class hierarchy:

```
   KeyBase
    |
   Key--------------+---------------+
    |               |               |
   StringKey    ObjectKey       UniqueKey
```

...where the constructors are Key and Key.stringify (StringKey),
Key.fromObjectIdentity (ObjectKey), and Key.unique (UniqueKey).

We could instead of factory methods use regular constructors with the
following hierarchy:

```
   KeyBase
    |
   LocalKey---------+---------------+
    |               |               |
   Key      ObjectIdentityKey   UniqueKey
```

...with constructors Key, Key.stringify, ObjectIdentityKey, and
UniqueKey, but I felt that that was maybe a more confusing hierarchy.
I don't have a strong opinion on this.
parent bb0c8bb1
...@@ -60,9 +60,9 @@ class SkyDemo { ...@@ -60,9 +60,9 @@ class SkyDemo {
this.description, this.description,
this.textTheme, this.textTheme,
this.decoration this.decoration
}) : name = name, key = name; }) : name = name, key = new Key(name);
final String name; final String name;
final String key; final Key key;
final String href; final String href;
final String bundle; final String bundle;
final String description; final String description;
......
...@@ -27,7 +27,7 @@ import 'fitness_types.dart'; ...@@ -27,7 +27,7 @@ import 'fitness_types.dart';
import 'measurement.dart'; import 'measurement.dart';
class MeasurementList extends Component { class MeasurementList extends Component {
MeasurementList({ String key, this.measurements, this.onDismissed }) : super(key: key); MeasurementList({ Key key, this.measurements, this.onDismissed }) : super(key: key);
final List<Measurement> measurements; final List<Measurement> measurements;
final MeasurementHandler onDismissed; final MeasurementHandler onDismissed;
...@@ -49,7 +49,7 @@ class MeasurementList extends Component { ...@@ -49,7 +49,7 @@ class MeasurementList extends Component {
class MeasurementRow extends Component { class MeasurementRow extends Component {
MeasurementRow({ Measurement measurement, this.onDismissed }) : this.measurement = measurement, super(key: measurement.when.toString()); MeasurementRow({ Measurement measurement, this.onDismissed }) : this.measurement = measurement, super(key: new Key.stringify(measurement.when));
final Measurement measurement; final Measurement measurement;
final MeasurementHandler onDismissed; final MeasurementHandler onDismissed;
...@@ -74,7 +74,7 @@ class MeasurementRow extends Component { ...@@ -74,7 +74,7 @@ class MeasurementRow extends Component {
]; ];
return new Dismissable( return new Dismissable(
key: measurement.when.toString(), key: new Key.stringify(measurement.when),
onDismissed: () => onDismissed(measurement), onDismissed: () => onDismissed(measurement),
child: new Card( child: new Card(
child: new Container( child: new Container(
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -57,6 +56,20 @@ class MineDiggerApp extends App { ...@@ -57,6 +56,20 @@ class MineDiggerApp extends App {
// |uiState| keeps track of the visible player progess. // |uiState| keeps track of the visible player progess.
List<List<CellState>> uiState; List<List<CellState>> uiState;
Game(this.app) {
randomSeed = 22;
// Colors for each mine count:
// 0 - none, 1 - blue, 2-green, 3-red, 4-black, 5-dark red .. etc.
textStyles.add(new TextStyle(color: const Color(0xFF555555), fontWeight: bold));
textStyles.add(new TextStyle(color: const Color(0xFF0094FF), fontWeight: bold));
textStyles.add(new TextStyle(color: const Color(0xFF13A023), fontWeight: bold));
textStyles.add(new TextStyle(color: const Color(0xFFDA1414), fontWeight: bold));
textStyles.add(new TextStyle(color: const Color(0xFF1E2347), fontWeight: bold));
textStyles.add(new TextStyle(color: const Color(0xFF7F0037), fontWeight: bold));
textStyles.add(new TextStyle(color: const Color(0xFFE93BE9), fontWeight: bold));
initialize();
}
void resetGame() { void resetGame() {
alive = true; alive = true;
hasWon = false; hasWon = false;
...@@ -148,13 +161,14 @@ class MineDiggerApp extends App { ...@@ -148,13 +161,14 @@ class MineDiggerApp extends App {
} else if (state == CellState.flagged) { } else if (state == CellState.flagged) {
row.add(new CoveredMineNode( row.add(new CoveredMineNode(
flagged: true, flagged: true,
posX: ix, posY: iy) posX: ix,
); posY: iy
));
} else { } else {
row.add(new ExposedMineNode( row.add(new ExposedMineNode(
state: state, state: state,
count: count) count: count
); ));
} }
} }
flexRows.add( flexRows.add(
...@@ -162,8 +176,9 @@ class MineDiggerApp extends App { ...@@ -162,8 +176,9 @@ class MineDiggerApp extends App {
row, row,
direction: FlexDirection.horizontal, direction: FlexDirection.horizontal,
justifyContent: FlexJustifyContent.center, justifyContent: FlexJustifyContent.center,
key: 'flex_row($iy)' key: new Key.stringify(iy)
)); )
);
} }
if (!hasCoveredCell) { if (!hasCoveredCell) {
......
...@@ -12,7 +12,7 @@ import 'package:sky/widgets/basic.dart'; ...@@ -12,7 +12,7 @@ import 'package:sky/widgets/basic.dart';
class StockArrow extends Component { class StockArrow extends Component {
StockArrow({ String key, this.percentChange }) : super(key: key); StockArrow({ Key key, this.percentChange }) : super(key: key);
final double percentChange; final double percentChange;
......
...@@ -10,7 +10,7 @@ import 'stock_data.dart'; ...@@ -10,7 +10,7 @@ import 'stock_data.dart';
import 'stock_row.dart'; import 'stock_row.dart';
class Stocklist extends Component { class Stocklist extends Component {
Stocklist({ String key, this.stocks }) : super(key: key); Stocklist({ Key key, this.stocks }) : super(key: key);
final List<Stock> stocks; final List<Stock> stocks;
......
...@@ -14,7 +14,7 @@ export 'package:sky/widgets/popup_menu.dart' show PopupMenuStatus; ...@@ -14,7 +14,7 @@ export 'package:sky/widgets/popup_menu.dart' show PopupMenuStatus;
class StockMenu extends Component { class StockMenu extends Component {
StockMenu({ StockMenu({
String key, Key key,
this.showing, this.showing,
this.onStatusChanged, this.onStatusChanged,
this.navigator, this.navigator,
......
...@@ -14,7 +14,7 @@ import 'stock_data.dart'; ...@@ -14,7 +14,7 @@ import 'stock_data.dart';
class StockRow extends Component { class StockRow extends Component {
StockRow({ Stock stock }) : this.stock = stock, super(key: stock.symbol); StockRow({ Stock stock }) : this.stock = stock, super(key: new Key(stock.symbol));
final Stock stock; final Stock stock;
......
...@@ -115,7 +115,7 @@ class BlockViewportApp extends App { ...@@ -115,7 +115,7 @@ class BlockViewportApp extends App {
if (index >= lengths.length) if (index >= lengths.length)
return null; return null;
return new Listener( return new Listener(
key: lengths[index].toString(), key: new Key.stringify(lengths[index]),
child: new Container( child: new Container(
decoration: new BoxDecoration( decoration: new BoxDecoration(
backgroundColor: new Color((0xFF000000 + 0xFFFFFF * lengths[index] / kMaxLength).round()) backgroundColor: new Color((0xFF000000 + 0xFFFFFF * lengths[index] / kMaxLength).round())
......
...@@ -27,13 +27,13 @@ class CardModel { ...@@ -27,13 +27,13 @@ class CardModel {
Color color; Color color;
AnimationPerformance performance; AnimationPerformance performance;
String get label => "Item $value"; String get label => "Item $value";
String get key => value.toString(); Key get key => new Key.fromObjectIdentity(this);
} }
class ShrinkingCard extends AnimatedComponent { class ShrinkingCard extends AnimatedComponent {
ShrinkingCard({ ShrinkingCard({
String key, Key key,
CardModel this.card, CardModel this.card,
Function this.onUpdated, Function this.onUpdated,
Function this.onCompleted Function this.onCompleted
......
...@@ -24,7 +24,7 @@ void addFlexChildSolidColor(RenderFlex parent, sky.Color backgroundColor, { int ...@@ -24,7 +24,7 @@ void addFlexChildSolidColor(RenderFlex parent, sky.Color backgroundColor, { int
// Solid colour, Widget version // Solid colour, Widget version
class Rectangle extends Component { class Rectangle extends Component {
Rectangle(this.color, { String key }) : super(key: key); Rectangle(this.color, { Key key }) : super(key: key);
final Color color; final Color color;
Widget build() { Widget build() {
return new Flexible( return new Flexible(
......
...@@ -52,7 +52,7 @@ HAL: This mission is too important for me to allow you to jeopardize it.'''; ...@@ -52,7 +52,7 @@ HAL: This mission is too important for me to allow you to jeopardize it.''';
Component toStyledText(String name, String text) { Component toStyledText(String name, String text) {
TextStyle lineStyle = (name == "Dave") ? daveStyle : halStyle; TextStyle lineStyle = (name == "Dave") ? daveStyle : halStyle;
return new StyledText( return new StyledText(
key: text, key: new Key(text),
elements: [lineStyle, [boldStyle, [underlineStyle, name], ":"], text] elements: [lineStyle, [boldStyle, [underlineStyle, name], ":"], text]
); );
} }
......
...@@ -18,7 +18,7 @@ const _kCursorWidth = 1.0; ...@@ -18,7 +18,7 @@ const _kCursorWidth = 1.0;
class EditableText extends StatefulComponent { class EditableText extends StatefulComponent {
EditableText({ EditableText({
String key, Key key,
this.value, this.value,
this.focused: false, this.focused: false,
this.style, this.style,
......
...@@ -25,7 +25,7 @@ class Input extends StatefulComponent { ...@@ -25,7 +25,7 @@ class Input extends StatefulComponent {
// Never makes sense to have both a localKey and a globalKey. // Never makes sense to have both a localKey and a globalKey.
// Possibly a class HeroKey who functions as a UUID. // Possibly a class HeroKey who functions as a UUID.
Input({String key, Input({Key key,
this.placeholder, this.placeholder,
this.onChanged, this.onChanged,
this.focused}) this.focused})
...@@ -75,7 +75,7 @@ class Input extends StatefulComponent { ...@@ -75,7 +75,7 @@ class Input extends StatefulComponent {
if (placeholder != null && _value.isEmpty) { if (placeholder != null && _value.isEmpty) {
Widget child = new Opacity( Widget child = new Opacity(
key: "placeholder", key: new Key('placeholder'),
child: new Text(placeholder, style: textStyle), child: new Text(placeholder, style: textStyle),
opacity: themeData.hintOpacity opacity: themeData.hintOpacity
); );
......
...@@ -7,7 +7,7 @@ import 'package:sky/widgets/basic.dart'; ...@@ -7,7 +7,7 @@ import 'package:sky/widgets/basic.dart';
abstract class AnimatedComponent extends StatefulComponent { abstract class AnimatedComponent extends StatefulComponent {
AnimatedComponent({ String key }) : super(key: key); AnimatedComponent({ Key key }) : super(key: key);
void syncFields(AnimatedComponent source) { } void syncFields(AnimatedComponent source) { }
......
...@@ -77,7 +77,7 @@ class ImplicitlyAnimatedValue<T> { ...@@ -77,7 +77,7 @@ class ImplicitlyAnimatedValue<T> {
class AnimatedContainer extends AnimatedComponent { class AnimatedContainer extends AnimatedComponent {
AnimatedContainer({ AnimatedContainer({
String key, Key key,
this.child, this.child,
this.duration, this.duration,
this.constraints, this.constraints,
......
This diff is collapsed.
...@@ -16,7 +16,7 @@ class _Key { ...@@ -16,7 +16,7 @@ class _Key {
const _Key(this.type, this.key); const _Key(this.type, this.key);
factory _Key.fromWidget(Widget widget) => new _Key(widget.runtimeType, widget.key); factory _Key.fromWidget(Widget widget) => new _Key(widget.runtimeType, widget.key);
final Type type; final Type type;
final String key; final Key key;
bool operator ==(other) => other is _Key && other.type == type && other.key == key; bool operator ==(other) => other is _Key && other.type == type && other.key == key;
int get hashCode => 373 * 37 * type.hashCode + key.hashCode; int get hashCode => 373 * 37 * type.hashCode + key.hashCode;
String toString() => "_Key(type: $type, key: $key)"; String toString() => "_Key(type: $type, key: $key)";
...@@ -78,7 +78,7 @@ class BlockViewportLayoutState { ...@@ -78,7 +78,7 @@ class BlockViewportLayoutState {
} }
class BlockViewport extends RenderObjectWrapper { class BlockViewport extends RenderObjectWrapper {
BlockViewport({ this.builder, this.startOffset, this.token, this.layoutState, String key }) BlockViewport({ this.builder, this.startOffset, this.token, this.layoutState, Key key })
: super(key: key) { : super(key: key) {
assert(this.layoutState != null); assert(this.layoutState != null);
} }
......
...@@ -6,7 +6,7 @@ import 'package:sky/widgets/basic.dart'; ...@@ -6,7 +6,7 @@ import 'package:sky/widgets/basic.dart';
abstract class ButtonBase extends StatefulComponent { abstract class ButtonBase extends StatefulComponent {
ButtonBase({ String key, this.highlight: false }) : super(key: key); ButtonBase({ Key key, this.highlight: false }) : super(key: key);
bool highlight; bool highlight;
......
...@@ -11,7 +11,7 @@ const EdgeDims kCardMargins = const EdgeDims.all(4.0); ...@@ -11,7 +11,7 @@ const EdgeDims kCardMargins = const EdgeDims.all(4.0);
/// ///
/// <https://www.google.com/design/spec/components/cards.html> /// <https://www.google.com/design/spec/components/cards.html>
class Card extends Component { class Card extends Component {
Card({ String key, this.child, this.color }) : super(key: key); Card({ Key key, this.child, this.color }) : super(key: key);
final Widget child; final Widget child;
final Color color; final Color color;
......
...@@ -32,7 +32,7 @@ class Checkbox extends Toggleable { ...@@ -32,7 +32,7 @@ class Checkbox extends Toggleable {
/// * `value` determines whether the checkbox is checked. /// * `value` determines whether the checkbox is checked.
/// * `onChanged` is called whenever the state of the checkbox should change. /// * `onChanged` is called whenever the state of the checkbox should change.
Checkbox({ Checkbox({
String key, Key key,
bool value, bool value,
ValueChanged onChanged ValueChanged onChanged
}) : super(key: key, value: value, onChanged: onChanged); }) : super(key: key, value: value, onChanged: onChanged);
......
...@@ -9,7 +9,7 @@ import 'package:sky/widgets/widget.dart'; ...@@ -9,7 +9,7 @@ import 'package:sky/widgets/widget.dart';
class DefaultTextStyle extends Inherited { class DefaultTextStyle extends Inherited {
DefaultTextStyle({ DefaultTextStyle({
String key, Key key,
this.style, this.style,
Widget child Widget child
}) : super(key: key, child: child) { }) : super(key: key, child: child) {
......
...@@ -14,7 +14,7 @@ import 'package:sky/widgets/theme.dart'; ...@@ -14,7 +14,7 @@ import 'package:sky/widgets/theme.dart';
/// <https://www.google.com/design/spec/components/dialogs.html> /// <https://www.google.com/design/spec/components/dialogs.html>
class Dialog extends Component { class Dialog extends Component {
Dialog({ Dialog({
String key, Key key,
this.title, this.title,
this.content, this.content,
this.actions, this.actions,
......
...@@ -22,7 +22,7 @@ typedef void DismissedCallback(); ...@@ -22,7 +22,7 @@ typedef void DismissedCallback();
class Dismissable extends AnimatedComponent { class Dismissable extends AnimatedComponent {
Dismissable({ Dismissable({
String key, Key key,
this.child, this.child,
this.onDismissed this.onDismissed
// TODO(hansmuller): direction // TODO(hansmuller): direction
......
...@@ -46,7 +46,7 @@ typedef void DrawerStatusChangedCallback(DrawerStatus status); ...@@ -46,7 +46,7 @@ typedef void DrawerStatusChangedCallback(DrawerStatus status);
class Drawer extends AnimatedComponent { class Drawer extends AnimatedComponent {
Drawer({ Drawer({
String key, Key key,
this.children, this.children,
this.showing: false, this.showing: false,
this.level: 0, this.level: 0,
......
...@@ -6,7 +6,7 @@ import 'package:sky/widgets/basic.dart'; ...@@ -6,7 +6,7 @@ import 'package:sky/widgets/basic.dart';
import 'package:sky/widgets/theme.dart'; import 'package:sky/widgets/theme.dart';
class DrawerDivider extends Component { class DrawerDivider extends Component {
DrawerDivider({ String key }) : super(key: key); DrawerDivider({ Key key }) : super(key: key);
Widget build() { Widget build() {
return new Container( return new Container(
......
...@@ -12,7 +12,7 @@ import 'package:sky/widgets/theme.dart'; ...@@ -12,7 +12,7 @@ import 'package:sky/widgets/theme.dart';
class DrawerHeader extends Component { class DrawerHeader extends Component {
DrawerHeader({ String key, this.children }) : super(key: key); DrawerHeader({ Key key, this.children }) : super(key: key);
final List<Widget> children; final List<Widget> children;
......
...@@ -15,7 +15,7 @@ import 'package:sky/widgets/theme.dart'; ...@@ -15,7 +15,7 @@ import 'package:sky/widgets/theme.dart';
import 'package:sky/widgets/widget.dart'; import 'package:sky/widgets/widget.dart';
class DrawerItem extends ButtonBase { class DrawerItem extends ButtonBase {
DrawerItem({ String key, this.icon, this.children, this.onPressed, this.selected: false }) DrawerItem({ Key key, this.icon, this.children, this.onPressed, this.selected: false })
: super(key: key); : super(key: key);
String icon; String icon;
......
...@@ -10,7 +10,7 @@ import 'package:sky/widgets/scrollable.dart'; ...@@ -10,7 +10,7 @@ import 'package:sky/widgets/scrollable.dart';
abstract class FixedHeightScrollable extends Scrollable { abstract class FixedHeightScrollable extends Scrollable {
FixedHeightScrollable({ String key, this.itemHeight, this.padding }) FixedHeightScrollable({ Key key, this.itemHeight, this.padding })
: super(key: key) { : super(key: key) {
assert(itemHeight != null); assert(itemHeight != null);
} }
......
...@@ -9,7 +9,7 @@ import 'package:sky/widgets/theme.dart'; ...@@ -9,7 +9,7 @@ import 'package:sky/widgets/theme.dart';
class FlatButton extends MaterialButton { class FlatButton extends MaterialButton {
FlatButton({ FlatButton({
String key, Key key,
Widget child, Widget child,
bool enabled: true, bool enabled: true,
Function onPressed Function onPressed
......
...@@ -16,7 +16,7 @@ const double _kSize = 56.0; ...@@ -16,7 +16,7 @@ const double _kSize = 56.0;
class FloatingActionButton extends ButtonBase { class FloatingActionButton extends ButtonBase {
FloatingActionButton({ FloatingActionButton({
String key, Key key,
this.child, this.child,
this.backgroundColor, this.backgroundColor,
this.onPressed this.onPressed
......
...@@ -19,7 +19,7 @@ class IconThemeData { ...@@ -19,7 +19,7 @@ class IconThemeData {
class IconTheme extends Inherited { class IconTheme extends Inherited {
IconTheme({ IconTheme({
String key, Key key,
this.data, this.data,
Widget child Widget child
}) : super(key: key, child: child) { }) : super(key: key, child: child) {
...@@ -49,7 +49,7 @@ final AssetBundle _iconBundle = _initIconBundle(); ...@@ -49,7 +49,7 @@ final AssetBundle _iconBundle = _initIconBundle();
class Icon extends Component { class Icon extends Component {
Icon({ Icon({
String key, Key key,
this.size, this.size,
this.type: '', this.type: '',
this.color, this.color,
......
...@@ -12,7 +12,7 @@ import 'package:sky/widgets/widget.dart'; ...@@ -12,7 +12,7 @@ import 'package:sky/widgets/widget.dart';
class IconButton extends Component { class IconButton extends Component {
IconButton({ String icon: '', this.onPressed, this.color }) IconButton({ String icon: '', this.onPressed, this.color })
: super(key: icon), icon = icon; : super(key: new Key(icon)), icon = icon;
final String icon; final String icon;
final Function onPressed; final Function onPressed;
......
...@@ -126,7 +126,7 @@ class RenderInkWell extends RenderProxyBox { ...@@ -126,7 +126,7 @@ class RenderInkWell extends RenderProxyBox {
} }
class InkWell extends OneChildRenderObjectWrapper { class InkWell extends OneChildRenderObjectWrapper {
InkWell({ String key, Widget child }) InkWell({ Key key, Widget child })
: super(key: key, child: child); : super(key: key, child: child);
RenderInkWell get root => super.root; RenderInkWell get root => super.root;
......
...@@ -20,7 +20,7 @@ const Map<MaterialType, double> edges = const { ...@@ -20,7 +20,7 @@ const Map<MaterialType, double> edges = const {
class Material extends Component { class Material extends Component {
Material({ Material({
String key, Key key,
this.child, this.child,
this.type: MaterialType.card, this.type: MaterialType.card,
this.level: 0, this.level: 0,
......
...@@ -11,7 +11,7 @@ import 'package:sky/widgets/material.dart'; ...@@ -11,7 +11,7 @@ import 'package:sky/widgets/material.dart';
abstract class MaterialButton extends ButtonBase { abstract class MaterialButton extends ButtonBase {
MaterialButton({ MaterialButton({
String key, Key key,
this.child, this.child,
this.enabled: true, this.enabled: true,
this.onPressed this.onPressed
......
...@@ -7,7 +7,7 @@ import 'package:sky/widgets/widget.dart'; ...@@ -7,7 +7,7 @@ import 'package:sky/widgets/widget.dart';
class ModalOverlay extends Component { class ModalOverlay extends Component {
ModalOverlay({ String key, this.children, this.onDismiss }) : super(key: key); ModalOverlay({ Key key, this.children, this.onDismiss }) : super(key: key);
final List<Widget> children; final List<Widget> children;
final Function onDismiss; final Function onDismiss;
......
...@@ -46,7 +46,7 @@ const Point _kTransitionStartPoint = const Point(0.0, 75.0); ...@@ -46,7 +46,7 @@ const Point _kTransitionStartPoint = const Point(0.0, 75.0);
enum TransitionDirection { forward, reverse } enum TransitionDirection { forward, reverse }
class Transition extends AnimatedComponent { class Transition extends AnimatedComponent {
Transition({ Transition({
String key, Key key,
this.content, this.content,
this.direction, this.direction,
this.onDismissed, this.onDismissed,
...@@ -190,7 +190,7 @@ class NavigationState { ...@@ -190,7 +190,7 @@ class NavigationState {
class Navigator extends StatefulComponent { class Navigator extends StatefulComponent {
Navigator(this.state, { String key }) : super(key: key); Navigator(this.state, { Key key }) : super(key: key);
NavigationState state; NavigationState state;
...@@ -242,7 +242,7 @@ class Navigator extends StatefulComponent { ...@@ -242,7 +242,7 @@ class Navigator extends StatefulComponent {
if (content == null) if (content == null)
continue; continue;
Transition transition = new Transition( Transition transition = new Transition(
key: historyEntry.hashCode.toString(), // TODO(ianh): make it not collide key: new Key.fromObjectIdentity(historyEntry),
content: content, content: content,
direction: (i <= state.historyIndex) ? TransitionDirection.forward : TransitionDirection.reverse, direction: (i <= state.historyIndex) ? TransitionDirection.forward : TransitionDirection.reverse,
interactive: (i == state.historyIndex), interactive: (i == state.historyIndex),
......
...@@ -35,7 +35,7 @@ typedef void PopupMenuStatusChangedCallback(PopupMenuStatus status); ...@@ -35,7 +35,7 @@ typedef void PopupMenuStatusChangedCallback(PopupMenuStatus status);
class PopupMenu extends AnimatedComponent { class PopupMenu extends AnimatedComponent {
PopupMenu({ PopupMenu({
String key, Key key,
this.showing, this.showing,
this.onStatusChanged, this.onStatusChanged,
this.items, this.items,
......
...@@ -13,7 +13,7 @@ const double kBaselineOffsetFromBottom = 20.0; ...@@ -13,7 +13,7 @@ const double kBaselineOffsetFromBottom = 20.0;
class PopupMenuItem extends Component { class PopupMenuItem extends Component {
PopupMenuItem({ PopupMenuItem({
String key, Key key,
this.onPressed, this.onPressed,
this.child this.child
}) : super(key: key); }) : super(key: key);
......
...@@ -17,7 +17,7 @@ typedef void ValueChanged(value); ...@@ -17,7 +17,7 @@ typedef void ValueChanged(value);
class Radio extends ButtonBase { class Radio extends ButtonBase {
Radio({ Radio({
String key, Key key,
this.value, this.value,
this.groupValue, this.groupValue,
this.onChanged this.onChanged
......
...@@ -10,7 +10,7 @@ import 'package:sky/widgets/theme.dart'; ...@@ -10,7 +10,7 @@ import 'package:sky/widgets/theme.dart';
class RaisedButton extends MaterialButton { class RaisedButton extends MaterialButton {
RaisedButton({ RaisedButton({
String key, Key key,
Widget child, Widget child,
bool enabled: true, bool enabled: true,
Function onPressed Function onPressed
......
...@@ -173,7 +173,7 @@ class RenderScaffold extends RenderBox { ...@@ -173,7 +173,7 @@ class RenderScaffold extends RenderBox {
class Scaffold extends RenderObjectWrapper { class Scaffold extends RenderObjectWrapper {
Scaffold({ Scaffold({
String key, Key key,
Widget body, Widget body,
Widget statusBar, Widget statusBar,
Widget toolbar, Widget toolbar,
......
...@@ -27,7 +27,7 @@ enum ScrollDirection { vertical, horizontal } ...@@ -27,7 +27,7 @@ enum ScrollDirection { vertical, horizontal }
abstract class Scrollable extends StatefulComponent { abstract class Scrollable extends StatefulComponent {
Scrollable({ Scrollable({
String key, Key key,
this.direction: ScrollDirection.vertical this.direction: ScrollDirection.vertical
}) : super(key: key); }) : super(key: key);
......
...@@ -11,7 +11,7 @@ typedef Widget ItemBuilder<T>(T item); ...@@ -11,7 +11,7 @@ typedef Widget ItemBuilder<T>(T item);
class ScrollableList<T> extends FixedHeightScrollable { class ScrollableList<T> extends FixedHeightScrollable {
ScrollableList({ ScrollableList({
String key, Key key,
this.items, this.items,
this.itemBuilder, this.itemBuilder,
double itemHeight, double itemHeight,
......
...@@ -8,7 +8,7 @@ import 'package:sky/widgets/scrollable.dart'; ...@@ -8,7 +8,7 @@ import 'package:sky/widgets/scrollable.dart';
class ScrollableViewport extends Scrollable { class ScrollableViewport extends Scrollable {
ScrollableViewport({ String key, this.child }) : super(key: key); ScrollableViewport({ Key key, this.child }) : super(key: key);
Widget child; Widget child;
...@@ -54,7 +54,7 @@ class ScrollableViewport extends Scrollable { ...@@ -54,7 +54,7 @@ class ScrollableViewport extends Scrollable {
class ScrollableBlock extends Component { class ScrollableBlock extends Component {
ScrollableBlock(this.children, { String key }) : super(key: key); ScrollableBlock(this.children, { Key key }) : super(key: key);
final List<Widget> children; final List<Widget> children;
......
...@@ -10,7 +10,7 @@ import 'package:sky/widgets/material.dart'; ...@@ -10,7 +10,7 @@ import 'package:sky/widgets/material.dart';
import 'package:sky/widgets/theme.dart'; import 'package:sky/widgets/theme.dart';
class SnackBarAction extends Component { class SnackBarAction extends Component {
SnackBarAction({String key, this.label, this.onPressed }) : super(key: key) { SnackBarAction({Key key, this.label, this.onPressed }) : super(key: key) {
assert(label != null); assert(label != null);
} }
...@@ -32,7 +32,7 @@ class SnackBarAction extends Component { ...@@ -32,7 +32,7 @@ class SnackBarAction extends Component {
class SnackBar extends Component { class SnackBar extends Component {
SnackBar({ SnackBar({
String key, Key key,
this.content, this.content,
this.actions this.actions
}) : super(key: key) { }) : super(key: key) {
......
...@@ -26,7 +26,7 @@ class Switch extends Toggleable { ...@@ -26,7 +26,7 @@ class Switch extends Toggleable {
// TODO(jackson): Hit-test the switch so that it can respond to both taps and swipe gestures // TODO(jackson): Hit-test the switch so that it can respond to both taps and swipe gestures
Switch({ Switch({
String key, Key key,
bool value, bool value,
ValueChanged onChanged ValueChanged onChanged
}) : super(key: key, value: value, onChanged: onChanged); }) : super(key: key, value: value, onChanged: onChanged);
......
...@@ -256,7 +256,7 @@ class TabBarWrapper extends MultiChildRenderObjectWrapper { ...@@ -256,7 +256,7 @@ class TabBarWrapper extends MultiChildRenderObjectWrapper {
this.textAndIcons, this.textAndIcons,
this.scrollable: false, this.scrollable: false,
this.onLayoutChanged, this.onLayoutChanged,
String key Key key
}) : super(key: key, children: children); }) : super(key: key, children: children);
final int selectedIndex; final int selectedIndex;
...@@ -289,7 +289,7 @@ class TabLabel { ...@@ -289,7 +289,7 @@ class TabLabel {
class Tab extends Component { class Tab extends Component {
Tab({ Tab({
String key, Key key,
this.label, this.label,
this.selected: false this.selected: false
}) : super(key: key) { }) : super(key: key) {
...@@ -347,7 +347,7 @@ class Tab extends Component { ...@@ -347,7 +347,7 @@ class Tab extends Component {
class TabBar extends Scrollable { class TabBar extends Scrollable {
TabBar({ TabBar({
String key, Key key,
this.labels, this.labels,
this.selectedIndex: 0, this.selectedIndex: 0,
this.onChanged, this.onChanged,
...@@ -381,7 +381,7 @@ class TabBar extends Scrollable { ...@@ -381,7 +381,7 @@ class TabBar extends Scrollable {
Tab tab = new Tab( Tab tab = new Tab(
label: label, label: label,
selected: tabIndex == selectedIndex, selected: tabIndex == selectedIndex,
key: label.text == null ? label.icon : label.text key: new Key(label.text == null ? label.icon : label.text)
); );
return new Listener( return new Listener(
child: tab, child: tab,
...@@ -472,7 +472,7 @@ class TabNavigatorView { ...@@ -472,7 +472,7 @@ class TabNavigatorView {
class TabNavigator extends Component { class TabNavigator extends Component {
TabNavigator({ TabNavigator({
String key, Key key,
this.views, this.views,
this.selectedIndex: 0, this.selectedIndex: 0,
this.onChanged, this.onChanged,
......
...@@ -11,7 +11,7 @@ export 'package:sky/theme/theme_data.dart' show ThemeData, ThemeBrightness; ...@@ -11,7 +11,7 @@ export 'package:sky/theme/theme_data.dart' show ThemeData, ThemeBrightness;
class Theme extends Inherited { class Theme extends Inherited {
Theme({ Theme({
String key, Key key,
this.data, this.data,
Widget child Widget child
}) : super(key: key, child: child) { }) : super(key: key, child: child) {
......
...@@ -17,7 +17,7 @@ const Duration _kCheckDuration = const Duration(milliseconds: 200); ...@@ -17,7 +17,7 @@ const Duration _kCheckDuration = const Duration(milliseconds: 200);
abstract class Toggleable extends AnimatedComponent { abstract class Toggleable extends AnimatedComponent {
Toggleable({ Toggleable({
String key, Key key,
this.value, this.value,
this.onChanged this.onChanged
}) : super(key: key); }) : super(key: key);
......
...@@ -16,7 +16,7 @@ import 'package:sky/widgets/icon.dart'; ...@@ -16,7 +16,7 @@ import 'package:sky/widgets/icon.dart';
class ToolBar extends Component { class ToolBar extends Component {
ToolBar({ ToolBar({
String key, Key key,
this.left, this.left,
this.center, this.center,
this.right, this.right,
......
...@@ -12,7 +12,7 @@ export 'package:sky/widgets/block_viewport.dart' show BlockViewportLayoutState; ...@@ -12,7 +12,7 @@ export 'package:sky/widgets/block_viewport.dart' show BlockViewportLayoutState;
class VariableHeightScrollable extends Scrollable { class VariableHeightScrollable extends Scrollable {
VariableHeightScrollable({ VariableHeightScrollable({
String key, Key key,
this.builder, this.builder,
this.token, this.token,
this.layoutState this.layoutState
......
...@@ -21,22 +21,54 @@ final bool _shouldLogRenderDuration = false; ...@@ -21,22 +21,54 @@ final bool _shouldLogRenderDuration = false;
typedef Widget Builder(); typedef Widget Builder();
typedef void WidgetTreeWalker(Widget); typedef void WidgetTreeWalker(Widget);
abstract class KeyBase {
}
abstract class Key extends KeyBase {
Key.constructor(); // so that subclasses can call us, since the Key() factory constructor shadows the implicit constructor
factory Key(String value) => new StringKey(value);
factory Key.stringify(Object value) => new StringKey(value.toString());
factory Key.fromObjectIdentity(Object value) => new ObjectKey(value);
factory Key.unique() => new UniqueKey();
}
class StringKey extends Key {
StringKey(this.value) : super.constructor();
final String value;
String toString() => value;
bool operator==(other) => other is StringKey && other.value == value;
int get hashCode => value.hashCode;
}
class ObjectKey extends Key {
ObjectKey(this.value) : super.constructor();
final Object value;
String toString() => '[Instance of ${value.runtimeType}]';
bool operator==(other) => other is ObjectKey && identical(other.value, value);
int get hashCode => identityHashCode(value);
}
class UniqueKey extends Key {
UniqueKey() : super.constructor();
String toString() => '[$hashCode]';
}
/// A base class for elements of the widget tree /// A base class for elements of the widget tree
abstract class Widget { abstract class Widget {
Widget({ String key }) : _key = key { Widget({ Key key }) : _key = key {
assert(_isConstructedDuringBuild()); assert(_isConstructedDuringBuild());
} }
// TODO(jackson): Remove this workaround for limitation of Dart mixins // TODO(jackson): Remove this workaround for limitation of Dart mixins
Widget._withKey(String key) : _key = key { Widget._withKey(Key key) : _key = key {
assert(_isConstructedDuringBuild()); assert(_isConstructedDuringBuild());
} }
// you should not build the UI tree ahead of time, build it only during build() // you should not build the UI tree ahead of time, build it only during build()
bool _isConstructedDuringBuild() => this is AbstractWidgetRoot || this is App || _inRenderDirtyComponents || _inLayoutCallbackBuilder > 0; bool _isConstructedDuringBuild() => this is AbstractWidgetRoot || this is App || _inRenderDirtyComponents || _inLayoutCallbackBuilder > 0;
String _key; Key _key;
/// A semantic identifer for this widget /// A semantic identifer for this widget
/// ///
...@@ -46,7 +78,7 @@ abstract class Widget { ...@@ -46,7 +78,7 @@ abstract class Widget {
/// Assigning a key to a widget can improve performance by causing the /// Assigning a key to a widget can improve performance by causing the
/// framework to sync widgets that share a lot of common structure and can /// framework to sync widgets that share a lot of common structure and can
/// help match stateful components semantically rather than positionally. /// help match stateful components semantically rather than positionally.
String get key => _key; Key get key => _key;
Widget _parent; Widget _parent;
...@@ -243,11 +275,11 @@ abstract class Widget { ...@@ -243,11 +275,11 @@ abstract class Widget {
// stylistic information, etc. // stylistic information, etc.
abstract class TagNode extends Widget { abstract class TagNode extends Widget {
TagNode(Widget child, { String key }) TagNode(Widget child, { Key key })
: this.child = child, super(key: key); : this.child = child, super(key: key);
// TODO(jackson): Remove this workaround for limitation of Dart mixins // TODO(jackson): Remove this workaround for limitation of Dart mixins
TagNode._withKey(Widget child, String key) TagNode._withKey(Widget child, Key key)
: this.child = child, super._withKey(key); : this.child = child, super._withKey(key);
Widget child; Widget child;
...@@ -284,14 +316,14 @@ abstract class TagNode extends Widget { ...@@ -284,14 +316,14 @@ abstract class TagNode extends Widget {
} }
class ParentDataNode extends TagNode { class ParentDataNode extends TagNode {
ParentDataNode(Widget child, this.parentData, { String key }) ParentDataNode(Widget child, this.parentData, { Key key })
: super(child, key: key); : super(child, key: key);
final ParentData parentData; final ParentData parentData;
} }
abstract class Inherited extends TagNode { abstract class Inherited extends TagNode {
Inherited({ String key, Widget child }) : super._withKey(child, key); Inherited({ Key key, Widget child }) : super._withKey(child, key);
void _sync(Widget old, dynamic slot) { void _sync(Widget old, dynamic slot) {
if (old != null && syncShouldNotify(old)) { if (old != null && syncShouldNotify(old)) {
...@@ -320,7 +352,7 @@ typedef void EventListener(sky.Event e); ...@@ -320,7 +352,7 @@ typedef void EventListener(sky.Event e);
class Listener extends TagNode { class Listener extends TagNode {
Listener({ Listener({
String key, Key key,
Widget child, Widget child,
EventListener onWheel, EventListener onWheel,
GestureEventListener onGestureFlingCancel, GestureEventListener onGestureFlingCancel,
...@@ -407,7 +439,7 @@ class Listener extends TagNode { ...@@ -407,7 +439,7 @@ class Listener extends TagNode {
abstract class Component extends Widget { abstract class Component extends Widget {
Component({ String key }) Component({ Key key })
: _order = _currentOrder + 1, : _order = _currentOrder + 1,
super._withKey(key); super._withKey(key);
...@@ -524,7 +556,7 @@ abstract class Component extends Widget { ...@@ -524,7 +556,7 @@ abstract class Component extends Widget {
abstract class StatefulComponent extends Component { abstract class StatefulComponent extends Component {
StatefulComponent({ String key }) : super(key: key); StatefulComponent({ Key key }) : super(key: key);
bool _disqualifiedFromEverAppearingAgain = false; bool _disqualifiedFromEverAppearingAgain = false;
bool _isStateInitialized = false; bool _isStateInitialized = false;
...@@ -682,7 +714,7 @@ void _scheduleComponentForRender(Component c) { ...@@ -682,7 +714,7 @@ void _scheduleComponentForRender(Component c) {
// become stateful. // become stateful.
abstract class RenderObjectWrapper extends Widget { abstract class RenderObjectWrapper extends Widget {
RenderObjectWrapper({ String key }) : super(key: key); RenderObjectWrapper({ Key key }) : super(key: key);
RenderObject createNode(); RenderObject createNode();
...@@ -766,7 +798,7 @@ abstract class RenderObjectWrapper extends Widget { ...@@ -766,7 +798,7 @@ abstract class RenderObjectWrapper extends Widget {
abstract class LeafRenderObjectWrapper extends RenderObjectWrapper { abstract class LeafRenderObjectWrapper extends RenderObjectWrapper {
LeafRenderObjectWrapper({ String key }) : super(key: key); LeafRenderObjectWrapper({ Key key }) : super(key: key);
void insertChildRoot(RenderObjectWrapper child, dynamic slot) { void insertChildRoot(RenderObjectWrapper child, dynamic slot) {
assert(false); assert(false);
...@@ -780,7 +812,7 @@ abstract class LeafRenderObjectWrapper extends RenderObjectWrapper { ...@@ -780,7 +812,7 @@ abstract class LeafRenderObjectWrapper extends RenderObjectWrapper {
abstract class OneChildRenderObjectWrapper extends RenderObjectWrapper { abstract class OneChildRenderObjectWrapper extends RenderObjectWrapper {
OneChildRenderObjectWrapper({ String key, Widget child }) OneChildRenderObjectWrapper({ Key key, Widget child })
: _child = child, super(key: key); : _child = child, super(key: key);
Widget _child; Widget _child;
...@@ -828,7 +860,7 @@ abstract class MultiChildRenderObjectWrapper extends RenderObjectWrapper { ...@@ -828,7 +860,7 @@ abstract class MultiChildRenderObjectWrapper extends RenderObjectWrapper {
// In MultiChildRenderObjectWrapper subclasses, slots are RenderObject nodes // In MultiChildRenderObjectWrapper subclasses, slots are RenderObject nodes
// to use as the "insert before" sibling in ContainerRenderObjectMixin.add() calls // to use as the "insert before" sibling in ContainerRenderObjectMixin.add() calls
MultiChildRenderObjectWrapper({ String key, List<Widget> children }) MultiChildRenderObjectWrapper({ Key key, List<Widget> children })
: this.children = children == null ? const [] : children, : this.children = children == null ? const [] : children,
super(key: key) { super(key: key) {
assert(!_debugHasDuplicateIds()); assert(!_debugHasDuplicateIds());
...@@ -867,7 +899,7 @@ abstract class MultiChildRenderObjectWrapper extends RenderObjectWrapper { ...@@ -867,7 +899,7 @@ abstract class MultiChildRenderObjectWrapper extends RenderObjectWrapper {
} }
bool _debugHasDuplicateIds() { bool _debugHasDuplicateIds() {
var idSet = new HashSet<String>(); var idSet = new HashSet<Key>();
for (var child in children) { for (var child in children) {
assert(child != null); assert(child != null);
if (child.key == null) if (child.key == null)
...@@ -922,9 +954,9 @@ abstract class MultiChildRenderObjectWrapper extends RenderObjectWrapper { ...@@ -922,9 +954,9 @@ abstract class MultiChildRenderObjectWrapper extends RenderObjectWrapper {
nextSibling = children[endIndex].root; nextSibling = children[endIndex].root;
} }
HashMap<String, Widget> oldNodeIdMap = null; HashMap<Key, Widget> oldNodeIdMap = null;
bool oldNodeReordered(String key) { bool oldNodeReordered(Key key) {
return oldNodeIdMap != null && return oldNodeIdMap != null &&
oldNodeIdMap.containsKey(key) && oldNodeIdMap.containsKey(key) &&
oldNodeIdMap[key] == null; oldNodeIdMap[key] == null;
...@@ -942,7 +974,7 @@ abstract class MultiChildRenderObjectWrapper extends RenderObjectWrapper { ...@@ -942,7 +974,7 @@ abstract class MultiChildRenderObjectWrapper extends RenderObjectWrapper {
if (oldNodeIdMap != null) if (oldNodeIdMap != null)
return; return;
oldNodeIdMap = new HashMap<String, Widget>(); oldNodeIdMap = new HashMap<Key, Widget>();
for (int i = oldStartIndex; i < oldEndIndex; i++) { for (int i = oldStartIndex; i < oldEndIndex; i++) {
var node = oldChildren[i]; var node = oldChildren[i];
if (node.key != null) if (node.key != null)
...@@ -1047,7 +1079,7 @@ class WidgetSkyBinding extends SkyBinding { ...@@ -1047,7 +1079,7 @@ class WidgetSkyBinding extends SkyBinding {
abstract class App extends StatefulComponent { abstract class App extends StatefulComponent {
App({ String key }) : super(key: key); App({ Key key }) : super(key: key);
void _handleEvent(sky.Event event) { void _handleEvent(sky.Event event) {
if (event.type == 'back') if (event.type == 'back')
...@@ -1095,7 +1127,7 @@ abstract class AbstractWidgetRoot extends StatefulComponent { ...@@ -1095,7 +1127,7 @@ abstract class AbstractWidgetRoot extends StatefulComponent {
} }
class RenderViewWrapper extends OneChildRenderObjectWrapper { class RenderViewWrapper extends OneChildRenderObjectWrapper {
RenderViewWrapper({ String key, Widget child }) : super(key: key, child: child); RenderViewWrapper({ Key key, Widget child }) : super(key: key, child: child);
RenderView get root => super.root; RenderView get root => super.root;
RenderView createNode() => SkyBinding.instance.renderView; RenderView createNode() => SkyBinding.instance.renderView;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment