Skip to content

Commit

Permalink
Added TextField.onTap() (flutter#24536)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Muller authored Nov 20, 2018
1 parent 9e0ce09 commit 94dab8f
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 6 deletions.
62 changes: 61 additions & 1 deletion packages/flutter/lib/src/material/text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class TextField extends StatefulWidget {
this.keyboardAppearance,
this.scrollPadding = const EdgeInsets.all(20.0),
this.enableInteractiveSelection = true,
this.onTap,
}) : assert(textAlign != null),
assert(autofocus != null),
assert(obscureText != null),
Expand All @@ -144,9 +145,44 @@ class TextField extends StatefulWidget {
/// If null, this widget will create its own [TextEditingController].
final TextEditingController controller;

/// Controls whether this widget has keyboard focus.
/// Defines the keyboard focus for this widget.
///
/// The [focusNode] is a long-lived object that's typically managed by a
/// [StatefulWidget] parent. See [FocusNode] for more information.
///
/// To give the keyboard focus to this widget, provide a [focusNode] and then
/// use the current [FocusScope] to request the focus:
///
/// ```dart
/// FocusScope.of(context).requestFocus(myFocusNode);
/// ```
///
/// This happens automatically when the widget is tapped.
///
/// To be notified when the widget gains or loses the focus, add a listener
/// to the [focusNode]:
///
/// ```dart
/// focusNode.addListener(() { print(myFocusNode.hasFocus); });
/// ```
///
/// If null, this widget will create its own [FocusNode].
///
/// ## Keyboard
///
/// Requesting the focus will typically cause the the keyboard to be shown
/// if it's not showing already.
///
/// On Android, the user can hide the keyboard - withouth changing the focus -
/// with the system back button. They can restore the keyboard's visibility
/// by tapping on a text field. The user might hide the keyboard and
/// switch to a physical keyboard, or they might just need to get it
/// out of the way for a moment, to expose something it's
/// obscuring. In this case requesting the focus again will not
/// cause the focus to change, and will not make the keyboard visible.
///
/// This widget builds an [EditableText] and will ensure that the keyboard is
/// showing when it is tapped by calling [EditableTextState.requestKeyboard()].
final FocusNode focusNode;

/// The decoration to show around the text field.
Expand Down Expand Up @@ -298,6 +334,26 @@ class TextField extends StatefulWidget {
/// {@macro flutter.widgets.editableText.enableInteractiveSelection}
final bool enableInteractiveSelection;

/// Called when the user taps on this textfield.
///
/// The textfield builds a [GestureDetector] to handle input events like tap,
/// to trigger focus requests, to move the caret, adjust the selection, etc.
/// Handling some of those events by wrapping the textfield with a competing
/// GestureDetector is problematic.
///
/// To unconditionally handle taps, without interfering with the textfield's
/// internal gesture detector, provide this callback.
///
/// If the textfield is created with [enabled] false, taps will not be
/// recognized.
///
/// To be notified when the textfield gains or loses the focus, provide a
/// [focusNode] and add a listener to that.
///
/// To listen to arbitrary pointer events without competing with the
/// textfield's internal gesture detector, use a [Listener].
final GestureTapCallback onTap;

@override
_TextFieldState createState() => _TextFieldState();

Expand All @@ -306,6 +362,7 @@ class TextField extends StatefulWidget {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<TextEditingController>('controller', controller, defaultValue: null));
properties.add(DiagnosticsProperty<FocusNode>('focusNode', focusNode, defaultValue: null));
properties.add(DiagnosticsProperty<bool>('enabled', enabled, defaultValue: null));
properties.add(DiagnosticsProperty<InputDecoration>('decoration', decoration));
properties.add(DiagnosticsProperty<TextInputType>('keyboardType', keyboardType, defaultValue: TextInputType.text));
properties.add(DiagnosticsProperty<TextStyle>('style', style, defaultValue: null));
Expand All @@ -315,6 +372,7 @@ class TextField extends StatefulWidget {
properties.add(IntProperty('maxLines', maxLines, defaultValue: 1));
properties.add(IntProperty('maxLength', maxLength, defaultValue: null));
properties.add(FlagProperty('maxLengthEnforced', value: maxLengthEnforced, ifTrue: 'max length enforced'));
properties.add(DiagnosticsProperty<GestureTapCallback>('onTap', onTap, defaultValue: false));
}
}

Expand Down Expand Up @@ -453,6 +511,8 @@ class _TextFieldState extends State<TextField> with AutomaticKeepAliveClientMixi
_renderEditable.handleTap();
_requestKeyboard();
_confirmCurrentSplash();
if (widget.onTap != null)
widget.onTap();
}

void _handleTapCancel() {
Expand Down
51 changes: 46 additions & 5 deletions packages/flutter/test/material/text_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ Widget overlay({ Widget child }) {
child: Overlay(
initialEntries: <OverlayEntry>[
OverlayEntry(
builder: (BuildContext context) => Center(
child: Material(
child: child,
),
),
builder: (BuildContext context) {
return Center(
child: Material(
child: child,
),
);
},
),
],
),
Expand Down Expand Up @@ -3341,4 +3343,43 @@ void main() {
expect(minOffset, 0.0);
expect(maxOffset, 50.0);
});

testWidgets('onTap is called upon tap', (WidgetTester tester) async {
int tapCount = 0;
await tester.pumpWidget(
overlay(
child: TextField(
onTap: () {
tapCount += 1;
},
),
),
);

expect(tapCount, 0);
await tester.tap(find.byType(TextField));
await tester.tap(find.byType(TextField));
await tester.tap(find.byType(TextField));
expect(tapCount, 3);
});

testWidgets('onTap is not called, field is disabled', (WidgetTester tester) async {
int tapCount = 0;
await tester.pumpWidget(
overlay(
child: TextField(
enabled: false,
onTap: () {
tapCount += 1;
},
),
),
);

expect(tapCount, 0);
await tester.tap(find.byType(TextField));
await tester.tap(find.byType(TextField));
await tester.tap(find.byType(TextField));
expect(tapCount, 0);
});
}

0 comments on commit 94dab8f

Please sign in to comment.