Skip to content

Commit 6e1b143

Browse files
authored
Text field scroll physics (flutter#31088)
TextField and CupertinoTextField can now specify scrollPhysics. Cupertino defaults to bouncing now.
1 parent 0545c63 commit 6e1b143

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

packages/flutter/lib/src/cupertino/text_field.dart

+6
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ class CupertinoTextField extends StatefulWidget {
186186
this.keyboardAppearance,
187187
this.scrollPadding = const EdgeInsets.all(20.0),
188188
this.dragStartBehavior = DragStartBehavior.start,
189+
this.scrollPhysics,
189190
}) : assert(textAlign != null),
190191
assert(autofocus != null),
191192
assert(obscureText != null),
@@ -425,6 +426,9 @@ class CupertinoTextField extends StatefulWidget {
425426
/// {@macro flutter.widgets.scrollable.dragStartBehavior}
426427
final DragStartBehavior dragStartBehavior;
427428

429+
/// {@macro flutter.widgets.edtiableText.scrollPhysics}
430+
final ScrollPhysics scrollPhysics;
431+
428432
@override
429433
_CupertinoTextFieldState createState() => _CupertinoTextFieldState();
430434

@@ -452,6 +456,7 @@ class CupertinoTextField extends StatefulWidget {
452456
properties.add(IntProperty('maxLength', maxLength, defaultValue: null));
453457
properties.add(FlagProperty('maxLengthEnforced', value: maxLengthEnforced, ifTrue: 'max length enforced'));
454458
properties.add(DiagnosticsProperty<Color>('cursorColor', cursorColor, defaultValue: null));
459+
properties.add(DiagnosticsProperty<ScrollPhysics>('scrollPhysics', scrollPhysics, defaultValue: null));
455460
}
456461
}
457462

@@ -751,6 +756,7 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with AutomaticK
751756
scrollPadding: widget.scrollPadding,
752757
keyboardAppearance: keyboardAppearance,
753758
dragStartBehavior: widget.dragStartBehavior,
759+
scrollPhysics: widget.scrollPhysics,
754760
),
755761
),
756762
);

packages/flutter/lib/src/material/text_field.dart

+6
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class TextField extends StatefulWidget {
165165
this.enableInteractiveSelection,
166166
this.onTap,
167167
this.buildCounter,
168+
this.scrollPhysics,
168169
}) : assert(textAlign != null),
169170
assert(autofocus != null),
170171
assert(obscureText != null),
@@ -459,6 +460,9 @@ class TextField extends StatefulWidget {
459460
/// {@end-tool}
460461
final InputCounterWidgetBuilder buildCounter;
461462

463+
/// {@macro flutter.widgets.edtiableText.scrollPhysics}
464+
final ScrollPhysics scrollPhysics;
465+
462466
@override
463467
_TextFieldState createState() => _TextFieldState();
464468

@@ -489,6 +493,7 @@ class TextField extends StatefulWidget {
489493
properties.add(DiagnosticsProperty<Brightness>('keyboardAppearance', keyboardAppearance, defaultValue: null));
490494
properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('scrollPadding', scrollPadding, defaultValue: const EdgeInsets.all(20.0)));
491495
properties.add(FlagProperty('selectionEnabled', value: selectionEnabled, defaultValue: true, ifFalse: 'selection disabled'));
496+
properties.add(DiagnosticsProperty<ScrollPhysics>('scrollPhysics', scrollPhysics, defaultValue: null));
492497
}
493498
}
494499

@@ -892,6 +897,7 @@ class _TextFieldState extends State<TextField> with AutomaticKeepAliveClientMixi
892897
keyboardAppearance: keyboardAppearance,
893898
enableInteractiveSelection: widget.enableInteractiveSelection,
894899
dragStartBehavior: widget.dragStartBehavior,
900+
scrollPhysics: widget.scrollPhysics,
895901
),
896902
);
897903

packages/flutter/lib/src/widgets/editable_text.dart

+12-1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ class EditableText extends StatefulWidget {
301301
this.keyboardAppearance = Brightness.light,
302302
this.dragStartBehavior = DragStartBehavior.start,
303303
this.enableInteractiveSelection,
304+
this.scrollPhysics,
304305
}) : assert(controller != null),
305306
assert(focusNode != null),
306307
assert(obscureText != null),
@@ -733,6 +734,15 @@ class EditableText extends StatefulWidget {
733734
/// {@macro flutter.widgets.scrollable.dragStartBehavior}
734735
final DragStartBehavior dragStartBehavior;
735736

737+
/// {@template flutter.widgets.editableText.scrollPhysics}
738+
/// The [ScrollPhysics] to use when vertically scrolling the input.
739+
///
740+
/// If not specified, it will behave according to the current platform.
741+
///
742+
/// See [Scrollable.physics].
743+
/// {@endtemplate}
744+
final ScrollPhysics scrollPhysics;
745+
736746
/// {@macro flutter.rendering.editable.selectionEnabled}
737747
bool get selectionEnabled {
738748
return enableInteractiveSelection ?? !obscureText;
@@ -758,6 +768,7 @@ class EditableText extends StatefulWidget {
758768
properties.add(DiagnosticsProperty<bool>('expands', expands, defaultValue: false));
759769
properties.add(DiagnosticsProperty<bool>('autofocus', autofocus, defaultValue: false));
760770
properties.add(DiagnosticsProperty<TextInputType>('keyboardType', keyboardType, defaultValue: null));
771+
properties.add(DiagnosticsProperty<ScrollPhysics>('scrollPhysics', scrollPhysics, defaultValue: null));
761772
}
762773
}
763774

@@ -1397,7 +1408,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
13971408
excludeFromSemantics: true,
13981409
axisDirection: _isMultiline ? AxisDirection.down : AxisDirection.right,
13991410
controller: _scrollController,
1400-
physics: const ClampingScrollPhysics(),
1411+
physics: widget.scrollPhysics,
14011412
dragStartBehavior: widget.dragStartBehavior,
14021413
viewportBuilder: (BuildContext context, ViewportOffset offset) {
14031414
return CompositedTransformTarget(

packages/flutter/test/material/text_field_test.dart

+2
Original file line numberDiff line numberDiff line change
@@ -5348,6 +5348,7 @@ void main() {
53485348
cursorColor: Color(0xff00ff00),
53495349
keyboardAppearance: Brightness.dark,
53505350
scrollPadding: EdgeInsets.zero,
5351+
scrollPhysics: ClampingScrollPhysics(),
53515352
enableInteractiveSelection: false,
53525353
).debugFillProperties(builder);
53535354

@@ -5374,6 +5375,7 @@ void main() {
53745375
'keyboardAppearance: Brightness.dark',
53755376
'scrollPadding: EdgeInsets.zero',
53765377
'selection disabled',
5378+
'scrollPhysics: ClampingScrollPhysics',
53775379
]);
53785380
});
53795381

0 commit comments

Comments
 (0)