From 780563ce05e4bdc8b73c8ecb9da4ccc575a2d5ac Mon Sep 17 00:00:00 2001 From: Shogo Hida Date: Wed, 18 Jan 2023 09:55:33 +0900 Subject: [PATCH] Add const constructor to TextInputFormatter (#116654) * Add const constructor to TextInputFormatter * Fix test * Add abstract * Add noSuchMethod Signed-off-by: Shogo Hida * Add @override and void Signed-off-by: Shogo Hida * Fix text and position of test Signed-off-by: Shogo Hida Signed-off-by: Shogo Hida --- .../flutter/lib/src/services/text_formatter.dart | 3 +++ .../test/services/text_formatter_test.dart | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/packages/flutter/lib/src/services/text_formatter.dart b/packages/flutter/lib/src/services/text_formatter.dart index d565cc9aaba8..8ba5d0d778b1 100644 --- a/packages/flutter/lib/src/services/text_formatter.dart +++ b/packages/flutter/lib/src/services/text_formatter.dart @@ -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 diff --git a/packages/flutter/test/services/text_formatter_test.dart b/packages/flutter/test/services/text_formatter_test.dart index 088fc281e904..0202e9507c29 100644 --- a/packages/flutter/test/services/text_formatter_test.dart +++ b/packages/flutter/test/services/text_formatter_test.dart @@ -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;