Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Add const constructor to TextInputFormatter (#116654)
Browse files Browse the repository at this point in the history
* Add const constructor to TextInputFormatter

* Fix test

* Add abstract

* Add noSuchMethod

Signed-off-by: Shogo Hida <shogo.hida@gmail.com>

* Add @OverRide and void

Signed-off-by: Shogo Hida <shogo.hida@gmail.com>

* Fix text and position of test

Signed-off-by: Shogo Hida <shogo.hida@gmail.com>

Signed-off-by: Shogo Hida <shogo.hida@gmail.com>
  • Loading branch information
shogohida committed Jan 18, 2023
1 parent 0eaa83a commit 780563c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/flutter/lib/src/services/text_formatter.dart
Expand Up @@ -88,6 +88,9 @@ enum MaxLengthEnforcement {
/// * [FilteringTextInputFormatter], a provided formatter for filtering
/// characters.
abstract class TextInputFormatter {
/// This constructor enables subclasses to provide const constructors so that they can be used in const expressions.
const TextInputFormatter();

/// 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
Expand Down
16 changes: 16 additions & 0 deletions packages/flutter/test/services/text_formatter_test.dart
Expand Up @@ -6,10 +6,26 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

class TestTextInputFormatter extends TextInputFormatter {
const TestTextInputFormatter();

@override
void noSuchMethod(Invocation invocation) {
super.noSuchMethod(invocation);
}
}

void main() {
TextEditingValue testOldValue = TextEditingValue.empty;
TextEditingValue testNewValue = TextEditingValue.empty;

test('test const constructor', () {
const TestTextInputFormatter testValue1 = TestTextInputFormatter();
const TestTextInputFormatter testValue2 = TestTextInputFormatter();

expect(testValue1, same(testValue2));
});

test('withFunction wraps formatting function', () {
testOldValue = TextEditingValue.empty;
testNewValue = TextEditingValue.empty;
Expand Down

0 comments on commit 780563c

Please sign in to comment.