Blame view

lib/utils/text_input_formatter.dart 490 Bytes
c9df43c8   Key   feat: 修改个人信息、接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  import 'package:flutter/services.dart';
  
  class RegexFormatter extends TextInputFormatter {
    RegexFormatter({required this.regex});
  
    /// 需要匹配的正则表达
    final String regex;
  
    @override
    TextEditingValue formatEditUpdate(
      TextEditingValue oldValue,
      TextEditingValue newValue,
    ) {
      if (newValue.text.isEmpty) {
        return TextEditingValue.empty;
      }
  
      if (!RegExp(regex).hasMatch(newValue.text)) {
        return oldValue;
      }
      return newValue;
    }
  }