Skip to content

Commit 780563c

Browse files
authored
Add const constructor to TextInputFormatter (#116654)
* Add const constructor to TextInputFormatter * Fix test * Add abstract * Add noSuchMethod Signed-off-by: Shogo Hida <[email protected]> * Add @OverRide and void Signed-off-by: Shogo Hida <[email protected]> * Fix text and position of test Signed-off-by: Shogo Hida <[email protected]> Signed-off-by: Shogo Hida <[email protected]>
1 parent 0eaa83a commit 780563c

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

packages/flutter/lib/src/services/text_formatter.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ enum MaxLengthEnforcement {
8888
/// * [FilteringTextInputFormatter], a provided formatter for filtering
8989
/// characters.
9090
abstract class TextInputFormatter {
91+
/// This constructor enables subclasses to provide const constructors so that they can be used in const expressions.
92+
const TextInputFormatter();
93+
9194
/// Called when text is being typed or cut/copy/pasted in the [EditableText].
9295
///
9396
/// You can override the resulting text based on the previous text value and

packages/flutter/test/services/text_formatter_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,26 @@ import 'package:flutter/foundation.dart';
66
import 'package:flutter/services.dart';
77
import 'package:flutter_test/flutter_test.dart';
88

9+
class TestTextInputFormatter extends TextInputFormatter {
10+
const TestTextInputFormatter();
11+
12+
@override
13+
void noSuchMethod(Invocation invocation) {
14+
super.noSuchMethod(invocation);
15+
}
16+
}
17+
918
void main() {
1019
TextEditingValue testOldValue = TextEditingValue.empty;
1120
TextEditingValue testNewValue = TextEditingValue.empty;
1221

22+
test('test const constructor', () {
23+
const TestTextInputFormatter testValue1 = TestTextInputFormatter();
24+
const TestTextInputFormatter testValue2 = TestTextInputFormatter();
25+
26+
expect(testValue1, same(testValue2));
27+
});
28+
1329
test('withFunction wraps formatting function', () {
1430
testOldValue = TextEditingValue.empty;
1531
testNewValue = TextEditingValue.empty;

0 commit comments

Comments
 (0)