Commit 48f2770e authored by Adam Barth's avatar Adam Barth Committed by GitHub

Add missing docs (#9696)

parent 2ab631b7
...@@ -4,23 +4,23 @@ ...@@ -4,23 +4,23 @@
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
/// A [TextInputFormatter] can be optionally injected into an [EditableText] /// A [TextInputFormatter] can be optionally injected into an [EditableText]
/// to provide as-you-type validation and formatting of the text being edited. /// to provide as-you-type validation and formatting of the text being edited.
/// ///
/// Text modification should only be applied when text is being committed by the /// Text modification should only be applied when text is being committed by the
/// IME and not on text under composition (i.e. when /// IME and not on text under composition (i.e., only when
/// [TextEditingValue.composing] is collapsed). /// [TextEditingValue.composing] is collapsed).
/// ///
/// Concrete implementations [BlacklistingTextInputFormatter], which removes /// Concrete implementations [BlacklistingTextInputFormatter], which removes
/// blacklisted characters upon edit commit, and /// blacklisted characters upon edit commit, and
/// [WhitelistingTextInputFormatter], which only allows entries of whitelisted /// [WhitelistingTextInputFormatter], which only allows entries of whitelisted
/// characters, are provided. /// characters, are provided.
/// ///
/// To create custom formatters, extend the [TextInputFormatter] class and /// To create custom formatters, extend the [TextInputFormatter] class and
/// implement the [formatEditUpdate] method. /// implement the [formatEditUpdate] method.
/// ///
/// See also: /// See also:
/// ///
/// * [EditableText] on which the formatting apply. /// * [EditableText] on which the formatting apply.
/// * [BlacklistingTextInputFormatter], a provided formatter for blacklisting /// * [BlacklistingTextInputFormatter], a provided formatter for blacklisting
/// characters. /// characters.
...@@ -28,14 +28,14 @@ import 'package:flutter/services.dart'; ...@@ -28,14 +28,14 @@ import 'package:flutter/services.dart';
/// characters. /// characters.
abstract class TextInputFormatter { abstract class TextInputFormatter {
/// Called when text is being typed or cut/copy/pasted in the [EditableText]. /// Called when text is being typed or cut/copy/pasted in the [EditableText].
/// ///
/// You can override the resulting text based on the previous text value and /// You can override the resulting text based on the previous text value and
/// the incoming new text value. /// the incoming new text value.
/// ///
/// When formatters are chained, `oldValue` reflects the initial value of /// When formatters are chained, `oldValue` reflects the initial value of
/// [TextEditingValue] at the beginning of the chain. /// [TextEditingValue] at the beginning of the chain.
TextEditingValue formatEditUpdate( TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue oldValue,
TextEditingValue newValue TextEditingValue newValue
); );
...@@ -48,50 +48,52 @@ abstract class TextInputFormatter { ...@@ -48,50 +48,52 @@ abstract class TextInputFormatter {
} }
} }
/// Function signature expected for creating custom [TextInputFormatter] /// Function signature expected for creating custom [TextInputFormatter]
/// shorthands via [TextInputFormatter.withFunction]; /// shorthands via [TextInputFormatter.withFunction];
typedef TextEditingValue TextInputFormatFunction( typedef TextEditingValue TextInputFormatFunction(
TextEditingValue oldValue, TextEditingValue oldValue,
TextEditingValue newValue, TextEditingValue newValue,
); );
/// Wiring for [TextInputFormatter.withFunction]. /// Wiring for [TextInputFormatter.withFunction].
class _SimpleTextInputFormatter extends TextInputFormatter { class _SimpleTextInputFormatter extends TextInputFormatter {
_SimpleTextInputFormatter(this.formatFunction) : _SimpleTextInputFormatter(this.formatFunction) :
assert(formatFunction != null); assert(formatFunction != null);
final TextInputFormatFunction formatFunction; final TextInputFormatFunction formatFunction;
@override @override
TextEditingValue formatEditUpdate( TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue oldValue,
TextEditingValue newValue TextEditingValue newValue
) { ) {
return formatFunction(oldValue, newValue); return formatFunction(oldValue, newValue);
} }
} }
/// A [TextInputFormatter] that prevents the insertion of blacklisted /// A [TextInputFormatter] that prevents the insertion of blacklisted
/// characters patterns. /// characters patterns.
/// ///
/// Instances of blacklisted characters found in the new [TextEditingValue]s /// Instances of blacklisted characters found in the new [TextEditingValue]s
/// will be replaced with the [replacementString] which defaults to ``. /// will be replaced with the [replacementString] which defaults to the empty
/// /// string.
///
/// Since this formatter only removes characters from the text, it attempts to /// Since this formatter only removes characters from the text, it attempts to
/// preserve the existing [TextEditingValue.selection] to values it would now /// preserve the existing [TextEditingValue.selection] to values it would now
/// fall at with the removed characters. /// fall at with the removed characters.
/// ///
/// See also: /// See also:
/// ///
/// * [TextInputFormatter]. /// * [WhitelistingTextInputFormatter], which uses a whitelist instead of a
/// * [WhitelistingTextInputFormatter]. /// blacklist.
class BlacklistingTextInputFormatter extends TextInputFormatter { class BlacklistingTextInputFormatter extends TextInputFormatter {
/// Creates a formatter that prevents the insertion of blacklisted characters patterns.
///
/// The [blacklistedPattern] must not be null.
BlacklistingTextInputFormatter( BlacklistingTextInputFormatter(
this.blacklistedPattern, this.blacklistedPattern, {
{ this.replacementString: '',
this.replacementString: '', }) : assert(blacklistedPattern != null);
}
) : assert(blacklistedPattern != null);
/// A [Pattern] to match and replace incoming [TextEditingValue]s. /// A [Pattern] to match and replace incoming [TextEditingValue]s.
final Pattern blacklistedPattern; final Pattern blacklistedPattern;
...@@ -105,7 +107,7 @@ class BlacklistingTextInputFormatter extends TextInputFormatter { ...@@ -105,7 +107,7 @@ class BlacklistingTextInputFormatter extends TextInputFormatter {
TextEditingValue newValue, TextEditingValue newValue,
) { ) {
return _selectionAwareTextManipulation( return _selectionAwareTextManipulation(
newValue, newValue,
(String substring) { (String substring) {
return substring.replaceAll(blacklistedPattern, replacementString); return substring.replaceAll(blacklistedPattern, replacementString);
}, },
...@@ -117,23 +119,26 @@ class BlacklistingTextInputFormatter extends TextInputFormatter { ...@@ -117,23 +119,26 @@ class BlacklistingTextInputFormatter extends TextInputFormatter {
= new BlacklistingTextInputFormatter(new RegExp(r'\n')); = new BlacklistingTextInputFormatter(new RegExp(r'\n'));
} }
/// A [TextInputFormatter] that allows only the insertion of whitelisted /// A [TextInputFormatter] that allows only the insertion of whitelisted
/// characters patterns. /// characters patterns.
/// ///
/// Since this formatter only removes characters from the text, it attempts to /// Since this formatter only removes characters from the text, it attempts to
/// preserve the existing [TextEditingValue.selection] to values it would now /// preserve the existing [TextEditingValue.selection] to values it would now
/// fall at with the removed characters. /// fall at with the removed characters.
/// ///
/// See also: /// See also:
/// ///
/// * [TextInputFormatter]. /// * [BlacklistingTextInputFormatter], which uses a blacklist instead of a
/// * [BlacklistingTextInputFormatter]. /// whitelist.
class WhitelistingTextInputFormatter extends TextInputFormatter { class WhitelistingTextInputFormatter extends TextInputFormatter {
WhitelistingTextInputFormatter(this.whitelistedPattern) : /// Creates a formatter that allows only the insertion of whitelisted characters patterns.
///
/// The [blacklistedPattern] must not be null.
WhitelistingTextInputFormatter(this.whitelistedPattern) :
assert(whitelistedPattern != null); assert(whitelistedPattern != null);
/// A [Pattern] to extract all instances of allowed characters. /// A [Pattern] to extract all instances of allowed characters.
/// ///
/// [RegExp] with multiple groups is not supported. /// [RegExp] with multiple groups is not supported.
final Pattern whitelistedPattern; final Pattern whitelistedPattern;
...@@ -143,7 +148,7 @@ class WhitelistingTextInputFormatter extends TextInputFormatter { ...@@ -143,7 +148,7 @@ class WhitelistingTextInputFormatter extends TextInputFormatter {
TextEditingValue newValue, TextEditingValue newValue,
) { ) {
return _selectionAwareTextManipulation( return _selectionAwareTextManipulation(
newValue, newValue,
(String substring) { (String substring) {
return whitelistedPattern return whitelistedPattern
.allMatches(substring) .allMatches(substring)
...@@ -186,8 +191,8 @@ TextEditingValue _selectionAwareTextManipulation( ...@@ -186,8 +191,8 @@ TextEditingValue _selectionAwareTextManipulation(
} }
return new TextEditingValue( return new TextEditingValue(
text: manipulatedText, text: manipulatedText,
selection: manipulatedSelection ?? const TextSelection.collapsed(offset: -1), selection: manipulatedSelection ?? const TextSelection.collapsed(offset: -1),
composing: manipulatedText == value.text composing: manipulatedText == value.text
? value.composing ? value.composing
: TextRange.empty, : TextRange.empty,
); );
......
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