diff --git a/examples/stocks/lib/stock_home.dart b/examples/stocks/lib/stock_home.dart index 7b4734ce7e..3692bffed3 100644 --- a/examples/stocks/lib/stock_home.dart +++ b/examples/stocks/lib/stock_home.dart @@ -241,9 +241,6 @@ class StockHomeState extends State { static const List portfolioSymbols = const ["AAPL","FIZZ", "FIVE", "FLAT", "ZINC", "ZNGA"]; - static GlobalKey searchFieldKey = new GlobalKey(); - static GlobalKey companyNameKey = new GlobalKey(); - // TODO(abarth): Should we factor this into a SearchBar in the framework? Widget buildSearchBar() { return new ToolBar( @@ -255,7 +252,6 @@ class StockHomeState extends State { ), center: new Input( value: _searchQuery, - key: searchFieldKey, autofocus: true, hintText: 'Search stocks', onChanged: _handleSearchQueryChanged @@ -272,7 +268,6 @@ class StockHomeState extends State { return new Column( children: [ new Input( - key: companyNameKey, autofocus: true, hintText: 'Company Name' ), diff --git a/packages/flutter/lib/src/material/input.dart b/packages/flutter/lib/src/material/input.dart index 9dada4eee0..7f6621bfb4 100644 --- a/packages/flutter/lib/src/material/input.dart +++ b/packages/flutter/lib/src/material/input.dart @@ -15,7 +15,7 @@ export 'package:sky_services/editing/editing.mojom.dart' show KeyboardType; /// A material design text input field. class Input extends StatefulComponent { Input({ - GlobalKey key, + Key key, this.value: InputValue.empty, this.keyboardType: KeyboardType.text, this.icon, @@ -28,9 +28,7 @@ class Input extends StatefulComponent { this.autofocus: false, this.onChanged, this.onSubmitted - }) : super(key: key) { - assert(key != null); - } + }) : super(key: key); /// The text of the input field. final InputValue value; @@ -77,10 +75,13 @@ const Curve _kTransitionCurve = Curves.ease; class _InputState extends State { GlobalKey _rawInputLineKey = new GlobalKey(); + GlobalKey get focusKey => config.key is GlobalKey ? config.key : _rawInputLineKey; + Widget build(BuildContext context) { assert(debugCheckHasMaterial(context)); ThemeData themeData = Theme.of(context); - bool focused = Focus.at(context, autofocus: config.autofocus); + BuildContext focusContext = focusKey.currentContext; + bool focused = focusContext != null && Focus.at(focusContext, autofocus: config.autofocus); TextStyle textStyle = config.style ?? themeData.text.subhead; Color focusHighlightColor = themeData.accentColor; @@ -156,7 +157,7 @@ class _InputState extends State { child: new RawInputLine( key: _rawInputLineKey, value: config.value, - focusKey: config.key, + focusKey: focusKey, style: textStyle, hideText: config.hideText, cursorColor: cursorColor,