Commit 9047830c authored by Hixie's avatar Hixie

Rationalise the Key API.

Add a way of having keys based on numeric types or DateTimes by having a ValueKey<T> class.
Remove the redundant ways of declaring things, except for leaving one shorthand -- you can say `new Key(s)` instead of `new ValueKey<String>(s)`.
parent 12e3897f
...@@ -30,7 +30,7 @@ abstract class FitnessItemRow extends Component { ...@@ -30,7 +30,7 @@ abstract class FitnessItemRow extends Component {
FitnessItemRow({ FitnessItem item, this.onDismissed }) FitnessItemRow({ FitnessItem item, this.onDismissed })
: this.item = item, : this.item = item,
super(key: new Key(item.when.toString())) { super(key: new ValueKey<DateTime>(item.when)) {
assert(onDismissed != null); assert(onDismissed != null);
} }
......
...@@ -142,7 +142,7 @@ class MineDiggerApp extends App { ...@@ -142,7 +142,7 @@ class MineDiggerApp extends App {
new Row( new Row(
row, row,
justifyContent: FlexJustifyContent.center, justifyContent: FlexJustifyContent.center,
key: new Key.stringify(iy) key: new ValueKey<int>(iy)
) )
); );
} }
......
...@@ -13,7 +13,7 @@ class CardModel { ...@@ -13,7 +13,7 @@ class CardModel {
double height; double height;
Color color; Color color;
String get label => "Item $value"; String get label => "Item $value";
Key get key => new Key.fromObjectIdentity(this); Key get key => new ObjectKey(this);
} }
class CardCollectionApp extends App { class CardCollectionApp extends App {
......
...@@ -12,7 +12,7 @@ class CardModel { ...@@ -12,7 +12,7 @@ class CardModel {
double height; double height;
Color color; Color color;
String get label => "Card $value"; String get label => "Card $value";
Key get key => new Key.fromObjectIdentity(this); Key get key => new ObjectKey(this);
} }
class EnsureVisibleApp extends App { class EnsureVisibleApp extends App {
......
...@@ -15,7 +15,7 @@ class CardModel { ...@@ -15,7 +15,7 @@ class CardModel {
double height; double height;
Color color; Color color;
String get label => "Card $value"; String get label => "Card $value";
Key get key => new Key.fromObjectIdentity(this); Key get key => new ObjectKey(this);
} }
enum MarkerType { topLeft, bottomRight, touch } enum MarkerType { topLeft, bottomRight, touch }
......
...@@ -12,7 +12,7 @@ class CardModel { ...@@ -12,7 +12,7 @@ class CardModel {
Size size; Size size;
Color color; Color color;
String get label => "Card $value"; String get label => "Card $value";
Key get key => new Key.fromObjectIdentity(this); Key get key => new ObjectKey(this);
} }
class PageableListApp extends App { class PageableListApp extends App {
......
...@@ -35,7 +35,7 @@ class TabbedNavigatorApp extends App { ...@@ -35,7 +35,7 @@ class TabbedNavigatorApp extends App {
builder: () => _buildContent(text) builder: () => _buildContent(text)
); );
}); });
return _buildTabNavigator(n, views.toList(), const StringKey('textLabelsTabNavigator')); return _buildTabNavigator(n, views.toList(), const ValueKey<String>('textLabelsTabNavigator'));
} }
TabNavigator _buildIconLabelsTabNavigator(int n) { TabNavigator _buildIconLabelsTabNavigator(int n) {
...@@ -46,7 +46,7 @@ class TabbedNavigatorApp extends App { ...@@ -46,7 +46,7 @@ class TabbedNavigatorApp extends App {
builder: () => _buildContent(icon_name) builder: () => _buildContent(icon_name)
); );
}); });
return _buildTabNavigator(n, views.toList(), const StringKey('iconLabelsTabNavigator')); return _buildTabNavigator(n, views.toList(), const ValueKey<String>('iconLabelsTabNavigator'));
} }
TabNavigator _buildTextAndIconLabelsTabNavigator(int n) { TabNavigator _buildTextAndIconLabelsTabNavigator(int n) {
...@@ -64,7 +64,7 @@ class TabbedNavigatorApp extends App { ...@@ -64,7 +64,7 @@ class TabbedNavigatorApp extends App {
builder: () => _buildContent("Summary") builder: () => _buildContent("Summary")
) )
]; ];
return _buildTabNavigator(n, views, const StringKey('textAndIconLabelsTabNavigator')); return _buildTabNavigator(n, views, const ValueKey<String>('textAndIconLabelsTabNavigator'));
} }
TabNavigator _buildScrollableTabNavigator(int n) { TabNavigator _buildScrollableTabNavigator(int n) {
...@@ -86,7 +86,7 @@ class TabbedNavigatorApp extends App { ...@@ -86,7 +86,7 @@ class TabbedNavigatorApp extends App {
builder: () => _buildContent(text) builder: () => _buildContent(text)
); );
}); });
return _buildTabNavigator(n, views.toList(), const StringKey('scrollableTabNavigator'), isScrollable: true); return _buildTabNavigator(n, views.toList(), const ValueKey<String>('scrollableTabNavigator'), isScrollable: true);
} }
...@@ -118,7 +118,7 @@ class TabbedNavigatorApp extends App { ...@@ -118,7 +118,7 @@ class TabbedNavigatorApp extends App {
) )
]; ];
TabNavigator tabNavigator = _buildTabNavigator(4, views, const StringKey('tabs')); TabNavigator tabNavigator = _buildTabNavigator(4, views, const ValueKey<String>('tabs'));
assert(selectedIndices.length == 5); assert(selectedIndices.length == 5);
ToolBar toolbar = new ToolBar( ToolBar toolbar = new ToolBar(
......
...@@ -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: const StringKey('placeholder'), key: const ValueKey<String>('placeholder'),
child: new Text(placeholder, style: textStyle), child: new Text(placeholder, style: textStyle),
opacity: themeData.hintOpacity opacity: themeData.hintOpacity
); );
......
...@@ -768,7 +768,7 @@ class AssetImage extends Component { ...@@ -768,7 +768,7 @@ class AssetImage extends Component {
class WidgetToRenderBoxAdapter extends LeafRenderObjectWrapper { class WidgetToRenderBoxAdapter extends LeafRenderObjectWrapper {
WidgetToRenderBoxAdapter(RenderBox renderBox) WidgetToRenderBoxAdapter(RenderBox renderBox)
: renderBox = renderBox, : renderBox = renderBox,
super(key: new Key.fromObjectIdentity(renderBox)); super(key: new ObjectKey(renderBox));
final RenderBox renderBox; final RenderBox renderBox;
......
...@@ -178,7 +178,7 @@ Future showDialog(Navigator navigator, DialogBuilder builder) { ...@@ -178,7 +178,7 @@ Future showDialog(Navigator navigator, DialogBuilder builder) {
completer: completer, completer: completer,
builder: (navigator, route) { builder: (navigator, route) {
return new Focus( return new Focus(
key: new GlobalKey.fromObjectIdentity(route), key: new GlobalObjectKey(route),
autofocus: true, autofocus: true,
child: builder(navigator) child: builder(navigator)
); );
......
...@@ -25,16 +25,14 @@ typedef void WidgetTreeWalker(Widget); ...@@ -25,16 +25,14 @@ typedef void WidgetTreeWalker(Widget);
abstract class Key { abstract class Key {
const Key.constructor(); // so that subclasses can call us, since the Key() factory constructor shadows the implicit constructor const 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(String value) => new ValueKey<String>(value);
factory Key.stringify(Object value) => new StringKey(value.toString());
factory Key.fromObjectIdentity(Object value) => new ObjectKey(value);
} }
class StringKey extends Key { class ValueKey<T> extends Key {
const StringKey(this.value) : super.constructor(); const ValueKey(this.value) : super.constructor();
final String value; final T value;
String toString() => '[\'${value}\']'; String toString() => '[\'${value}\']';
bool operator==(other) => other is StringKey && other.value == value; bool operator==(other) => other is ValueKey<T> && other.value == value;
int get hashCode => value.hashCode; int get hashCode => value.hashCode;
} }
...@@ -51,8 +49,7 @@ typedef void GlobalKeyRemoveListener(GlobalKey key); ...@@ -51,8 +49,7 @@ typedef void GlobalKeyRemoveListener(GlobalKey key);
abstract class GlobalKey extends Key { abstract class GlobalKey extends Key {
const GlobalKey.constructor() : super.constructor(); // so that subclasses can call us, since the Key() factory constructor shadows the implicit constructor const GlobalKey.constructor() : super.constructor(); // so that subclasses can call us, since the Key() factory constructor shadows the implicit constructor
factory GlobalKey({ String label }) => new LabeledGlobalKey(label); factory GlobalKey({ String label }) => new LabeledGlobalKey(label); // the label is purely for debugging purposes and is otherwise ignored
factory GlobalKey.fromObjectIdentity(Object value) => new GlobalObjectKey(value);
static final Map<GlobalKey, Widget> _registry = new Map<GlobalKey, Widget>(); static final Map<GlobalKey, Widget> _registry = new Map<GlobalKey, Widget>();
static final Map<GlobalKey, int> _debugDuplicates = new Map<GlobalKey, int>(); static final Map<GlobalKey, int> _debugDuplicates = new Map<GlobalKey, int>();
......
...@@ -186,7 +186,7 @@ class Navigator extends StatefulComponent { ...@@ -186,7 +186,7 @@ class Navigator extends StatefulComponent {
} }
if (child == null) if (child == null)
continue; continue;
TransitionBase transition = historyEntry.route.buildTransition(key: new Key.fromObjectIdentity(historyEntry)) TransitionBase transition = historyEntry.route.buildTransition(key: new ObjectKey(historyEntry))
..child = child ..child = child
..direction = (i <= state.historyIndex) ? Direction.forward : Direction.reverse ..direction = (i <= state.historyIndex) ? Direction.forward : Direction.reverse
..onDismissed = () { ..onDismissed = () {
......
...@@ -13,7 +13,7 @@ void main() { ...@@ -13,7 +13,7 @@ void main() {
new Mimicable( new Mimicable(
key: globalKey, key: globalKey,
child: new Container( child: new Container(
key: new Key.stringify('inner'), key: new Key('inner'),
height: 10.0, height: 10.0,
width: 10.0 width: 10.0
) )
...@@ -50,13 +50,13 @@ void main() { ...@@ -50,13 +50,13 @@ void main() {
tester.pumpFrame(() { tester.pumpFrame(() {
return new Flex([ return new Flex([
new Container( new Container(
key: new Key.stringify('outer'), key: new Key('outer'),
height: 10.0, height: 10.0,
width: 10.0, width: 10.0,
child: new Mimicable( child: new Mimicable(
key: globalKey, key: globalKey,
child: new Container( child: new Container(
key: new Key.stringify('inner'), key: new Key('inner'),
height: 10.0, height: 10.0,
width: 10.0 width: 10.0
) )
......
...@@ -14,7 +14,7 @@ void main() { ...@@ -14,7 +14,7 @@ void main() {
Widget buildPage(int page) { Widget buildPage(int page) {
return new Container( return new Container(
key: new StringKey(page.toString()), key: new ValueKey<int>(page),
width: pageSize.width, width: pageSize.width,
height: pageSize.height, height: pageSize.height,
child: new Text(page.toString()) child: new Text(page.toString())
......
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