From eb1b0333e91422b9d007a973612c8fb3b109de7b Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 20 Aug 2015 09:29:58 -0700 Subject: [PATCH] Key should be const For great constness. Fixes #693 --- examples/widgets/tabs.dart | 10 +++++----- packages/flutter/lib/editing/input.dart | 3 ++- packages/flutter/lib/widgets/framework.dart | 12 ++++++------ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/examples/widgets/tabs.dart b/examples/widgets/tabs.dart index a83d878bf0..f434fc4ada 100644 --- a/examples/widgets/tabs.dart +++ b/examples/widgets/tabs.dart @@ -35,7 +35,7 @@ class TabbedNavigatorApp extends App { builder: () => _buildContent(text) ); }); - return _buildTabNavigator(n, views.toList(), new Key('textLabelsTabNavigator')); + return _buildTabNavigator(n, views.toList(), const StringKey('textLabelsTabNavigator')); } TabNavigator _buildIconLabelsTabNavigator(int n) { @@ -46,7 +46,7 @@ class TabbedNavigatorApp extends App { builder: () => _buildContent(icon_name) ); }); - return _buildTabNavigator(n, views.toList(), new Key('iconLabelsTabNavigator')); + return _buildTabNavigator(n, views.toList(), const StringKey('iconLabelsTabNavigator')); } TabNavigator _buildTextAndIconLabelsTabNavigator(int n) { @@ -64,7 +64,7 @@ class TabbedNavigatorApp extends App { builder: () => _buildContent("Summary") ) ]; - return _buildTabNavigator(n, views, new Key('textAndIconLabelsTabNavigator')); + return _buildTabNavigator(n, views, const StringKey('textAndIconLabelsTabNavigator')); } TabNavigator _buildScrollableTabNavigator(int n) { @@ -86,7 +86,7 @@ class TabbedNavigatorApp extends App { builder: () => _buildContent(text) ); }); - return _buildTabNavigator(n, views.toList(), new Key('scrollableTabNavigator'), isScrollable: true); + return _buildTabNavigator(n, views.toList(), const StringKey('scrollableTabNavigator'), isScrollable: true); } @@ -118,7 +118,7 @@ class TabbedNavigatorApp extends App { ) ]; - TabNavigator tabNavigator = _buildTabNavigator(4, views, new Key('tabs')); + TabNavigator tabNavigator = _buildTabNavigator(4, views, const StringKey('tabs')); assert(selectedIndices.length == 5); ToolBar toolbar = new ToolBar( diff --git a/packages/flutter/lib/editing/input.dart b/packages/flutter/lib/editing/input.dart index 643ab3985e..91fe65a6f0 100644 --- a/packages/flutter/lib/editing/input.dart +++ b/packages/flutter/lib/editing/input.dart @@ -8,6 +8,7 @@ import 'package:sky/mojo/keyboard.dart'; import 'package:sky/painting/text_style.dart'; import 'package:sky/widgets/basic.dart'; import 'package:sky/widgets/focus.dart'; +import 'package:sky/widgets/framework.dart'; import 'package:sky/widgets/theme.dart'; export 'package:sky/mojo/keyboard.dart' show KeyboardType_TEXT, KeyboardType_NUMBER, KeyboardType_PHONE, KeyboardType_DATETIME; @@ -74,7 +75,7 @@ class Input extends StatefulComponent { if (placeholder != null && _value.isEmpty) { Widget child = new Opacity( - key: new Key('placeholder'), + key: const StringKey('placeholder'), child: new Text(placeholder, style: textStyle), opacity: themeData.hintOpacity ); diff --git a/packages/flutter/lib/widgets/framework.dart b/packages/flutter/lib/widgets/framework.dart index 23fd959acb..1fbbc2736f 100644 --- a/packages/flutter/lib/widgets/framework.dart +++ b/packages/flutter/lib/widgets/framework.dart @@ -25,14 +25,14 @@ typedef Widget Builder(); typedef void WidgetTreeWalker(Widget); abstract class Key { - 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.stringify(Object value) => new StringKey(value.toString()); factory Key.fromObjectIdentity(Object value) => new ObjectKey(value); } class StringKey extends Key { - StringKey(this.value) : super.constructor(); + const StringKey(this.value) : super.constructor(); final String value; String toString() => '[\'${value}\']'; bool operator==(other) => other is StringKey && other.value == value; @@ -40,7 +40,7 @@ class StringKey extends Key { } class ObjectKey extends Key { - ObjectKey(this.value) : super.constructor(); + const ObjectKey(this.value) : super.constructor(); final Object value; String toString() => '[${value.runtimeType}(${value.hashCode})]'; bool operator==(other) => other is ObjectKey && identical(other.value, value); @@ -50,7 +50,7 @@ class ObjectKey extends Key { typedef void GlobalKeyRemovalListener(GlobalKey key); abstract class GlobalKey extends Key { - 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.fromObjectIdentity(Object value) => new GlobalObjectKey(value); @@ -136,13 +136,13 @@ abstract class GlobalKey extends Key { class LabeledGlobalKey extends GlobalKey { // the label is purely for documentary purposes and does not affect the key - LabeledGlobalKey(this._label) : super.constructor(); + const LabeledGlobalKey(this._label) : super.constructor(); final String _label; String toString() => '[GlobalKey ${_label != null ? _label : hashCode}]'; } class GlobalObjectKey extends GlobalKey { - GlobalObjectKey(this.value) : super.constructor(); + const GlobalObjectKey(this.value) : super.constructor(); final Object value; String toString() => '[GlobalKey ${value.runtimeType}(${value.hashCode})]'; bool operator==(other) => other is GlobalObjectKey && identical(other.value, value);