Commit 72931955 authored by Adam Barth's avatar Adam Barth

Merge pull request #1332 from abarth/no_autofocus

Don't autofocus Input widgets by default
parents 740b6122 f176ed27
...@@ -89,6 +89,7 @@ class MealFragmentState extends State<MealFragment> { ...@@ -89,6 +89,7 @@ class MealFragmentState extends State<MealFragment> {
new Text(meal.displayDate), new Text(meal.displayDate),
new Input( new Input(
key: descriptionKey, key: descriptionKey,
autofocus: true,
placeholder: 'Describe meal', placeholder: 'Describe meal',
onChanged: _handleDescriptionChanged onChanged: _handleDescriptionChanged
), ),
......
...@@ -141,6 +141,7 @@ class MeasurementFragmentState extends State<MeasurementFragment> { ...@@ -141,6 +141,7 @@ class MeasurementFragmentState extends State<MeasurementFragment> {
), ),
new Input( new Input(
key: weightKey, key: weightKey,
autofocus: true,
placeholder: 'Enter weight', placeholder: 'Enter weight',
keyboardType: KeyboardType.number, keyboardType: KeyboardType.number,
onChanged: _handleWeightChanged onChanged: _handleWeightChanged
......
...@@ -62,6 +62,7 @@ class SettingsFragmentState extends State<SettingsFragment> { ...@@ -62,6 +62,7 @@ class SettingsFragmentState extends State<SettingsFragment> {
title: new Text("Goal Weight"), title: new Text("Goal Weight"),
content: new Input( content: new Input(
key: weightGoalKey, key: weightGoalKey,
autofocus: true,
placeholder: 'Goal weight in lbs', placeholder: 'Goal weight in lbs',
keyboardType: KeyboardType.number, keyboardType: KeyboardType.number,
onChanged: _handleGoalWeightChanged onChanged: _handleGoalWeightChanged
......
...@@ -236,6 +236,7 @@ class StockHomeState extends State<StockHome> { ...@@ -236,6 +236,7 @@ class StockHomeState extends State<StockHome> {
), ),
center: new Input( center: new Input(
key: searchFieldKey, key: searchFieldKey,
autofocus: true,
placeholder: 'Search stocks', placeholder: 'Search stocks',
onChanged: _handleSearchQueryChanged onChanged: _handleSearchQueryChanged
), ),
...@@ -252,6 +253,7 @@ class StockHomeState extends State<StockHome> { ...@@ -252,6 +253,7 @@ class StockHomeState extends State<StockHome> {
children: <Widget>[ children: <Widget>[
new Input( new Input(
key: companyNameKey, key: companyNameKey,
autofocus: true,
placeholder: 'Company Name' placeholder: 'Company Name'
), ),
] ]
......
...@@ -12,6 +12,7 @@ import 'theme.dart'; ...@@ -12,6 +12,7 @@ import 'theme.dart';
export 'package:flutter/rendering.dart' show ValueChanged; export 'package:flutter/rendering.dart' show ValueChanged;
export 'package:flutter/services.dart' show KeyboardType; export 'package:flutter/services.dart' show KeyboardType;
/// A material design text input widget.
class Input extends Scrollable { class Input extends Scrollable {
Input({ Input({
GlobalKey key, GlobalKey key,
...@@ -19,6 +20,7 @@ class Input extends Scrollable { ...@@ -19,6 +20,7 @@ class Input extends Scrollable {
this.placeholder, this.placeholder,
this.hideText: false, this.hideText: false,
this.isDense: false, this.isDense: false,
this.autofocus: false,
this.onChanged, this.onChanged,
this.keyboardType: KeyboardType.text, this.keyboardType: KeyboardType.text,
this.onSubmitted this.onSubmitted
...@@ -30,12 +32,28 @@ class Input extends Scrollable { ...@@ -30,12 +32,28 @@ class Input extends Scrollable {
assert(key != null); assert(key != null);
} }
/// Initial editable text for the widget.
final String initialValue; final String initialValue;
/// The type of keyboard to use for editing the text.
final KeyboardType keyboardType; final KeyboardType keyboardType;
/// Hint text to show when the widget doesn't contain editable text.
final String placeholder; final String placeholder;
/// Whether to hide the text being edited (e.g., for passwords).
final bool hideText; final bool hideText;
/// Whether the input widget is part of a dense form (i.e., uses less vertical space).
final bool isDense; final bool isDense;
/// Whether this input widget should focus itself is nothing else is already focused.
final bool autofocus;
/// Called when the text being edited changes.
final ValueChanged<String> onChanged; final ValueChanged<String> onChanged;
/// Called when the user indicates that they are done editing the text in the widget.
final ValueChanged<String> onSubmitted; final ValueChanged<String> onSubmitted;
InputState createState() => new InputState(); InputState createState() => new InputState();
...@@ -79,7 +97,7 @@ class InputState extends ScrollableState<Input> { ...@@ -79,7 +97,7 @@ class InputState extends ScrollableState<Input> {
Widget buildContent(BuildContext context) { Widget buildContent(BuildContext context) {
assert(debugCheckHasMaterial(context)); assert(debugCheckHasMaterial(context));
ThemeData themeData = Theme.of(context); ThemeData themeData = Theme.of(context);
bool focused = Focus.at(context); bool focused = Focus.at(context, autofocus: config.autofocus);
if (focused && !_keyboardHandle.attached) { if (focused && !_keyboardHandle.attached) {
_keyboardHandle = keyboard.show(_editableValue.stub, config.keyboardType); _keyboardHandle = keyboard.show(_editableValue.stub, config.keyboardType);
......
...@@ -84,7 +84,7 @@ class Focus extends StatefulComponent { ...@@ -84,7 +84,7 @@ class Focus extends StatefulComponent {
static GlobalKey debugOnlyFocusedKey; static GlobalKey debugOnlyFocusedKey;
static bool at(BuildContext context, { bool autofocus: true }) { static bool at(BuildContext context, { bool autofocus: false }) {
assert(context != null); assert(context != null);
assert(context.widget != null); assert(context.widget != null);
assert(context.widget.key != null); assert(context.widget.key != null);
...@@ -109,7 +109,7 @@ class Focus extends StatefulComponent { ...@@ -109,7 +109,7 @@ class Focus extends StatefulComponent {
return true; return true;
} }
static bool _atScope(BuildContext context, { bool autofocus: true }) { static bool _atScope(BuildContext context, { bool autofocus: false }) {
assert(context != null); assert(context != null);
assert(context.widget != null); assert(context.widget != null);
assert(context.widget is Focus); assert(context.widget is Focus);
...@@ -126,7 +126,7 @@ class Focus extends StatefulComponent { ...@@ -126,7 +126,7 @@ class Focus extends StatefulComponent {
/// Focuses a particular widget, identified by its GlobalKey. /// Focuses a particular widget, identified by its GlobalKey.
/// The widget must be in the widget tree. /// The widget must be in the widget tree.
/// ///
/// Don't call moveTo() from your build() functions, it's intended to be /// Don't call moveTo() from your build() functions, it's intended to be
/// called from event listeners, e.g. in response to a finger tap or tab key. /// called from event listeners, e.g. in response to a finger tap or tab key.
static void moveTo(GlobalKey key) { static void moveTo(GlobalKey key) {
...@@ -138,7 +138,7 @@ class Focus extends StatefulComponent { ...@@ -138,7 +138,7 @@ class Focus extends StatefulComponent {
/// Focuses a particular focus scope, identified by its GlobalKey. The widget /// Focuses a particular focus scope, identified by its GlobalKey. The widget
/// must be in the widget tree. /// must be in the widget tree.
/// ///
/// Don't call moveScopeTo() from your build() functions, it's intended to be /// Don't call moveScopeTo() from your build() functions, it's intended to be
/// called from event listeners, e.g. in response to a finger tap or tab key. /// called from event listeners, e.g. in response to a finger tap or tab key.
static void moveScopeTo(GlobalKey key) { static void moveScopeTo(GlobalKey key) {
......
...@@ -11,7 +11,7 @@ class TestFocusable extends StatelessComponent { ...@@ -11,7 +11,7 @@ class TestFocusable extends StatelessComponent {
final String no; final String no;
final String yes; final String yes;
Widget build(BuildContext context) { Widget build(BuildContext context) {
bool focused = Focus.at(context); bool focused = Focus.at(context, autofocus: true);
return new GestureDetector( return new GestureDetector(
onTap: () { Focus.moveTo(key); }, onTap: () { Focus.moveTo(key); },
child: new Text(focused ? yes : no) child: new Text(focused ? yes : no)
......
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