LCOV - code coverage report
Current view: top level - validators - color_validator.dart (source / functions) Hit Total Coverage
Test: Folly Fields Lines: 28 37 75.7 %
Date: 2023-04-01 18:13:35 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:flutter/services.dart';
       2             : import 'package:folly_fields/util/mask_text_input_formatter.dart';
       3             : import 'package:folly_fields/validators/abstract_validator.dart';
       4             : 
       5             : ///
       6             : ///
       7             : ///
       8             : class ColorValidator extends AbstractParserValidator<Color> {
       9             :   ///
      10             :   ///
      11             :   ///
      12           1 :   ColorValidator()
      13           1 :       : super(
      14           1 :           <TextInputFormatter>[
      15           1 :             MaskTextInputFormatter(
      16             :               mask: 'AAAAAAAA',
      17           1 :               filter: <String, RegExp>{
      18           1 :                 'A': RegExp('[0-9,A-F,a-f]'),
      19             :               },
      20             :             ),
      21             :           ],
      22             :         );
      23             : 
      24             :   ///
      25             :   ///
      26             :   ///
      27           0 :   @override
      28           0 :   String format(Color value) => value.value.toRadixString(16).toUpperCase();
      29             : 
      30             :   ///
      31             :   ///
      32             :   ///
      33           0 :   @override
      34             :   String strip(String value) => value;
      35             : 
      36             :   ///
      37             :   ///
      38             :   ///
      39           0 :   @override
      40           0 :   bool isValid(String value) => valid(value) == null;
      41             : 
      42             :   ///
      43             :   ///
      44             :   ///
      45           1 :   @override
      46             :   Color? parse(String? text, [int? defaultColor]) {
      47           1 :     if (text == null || text.isEmpty) {
      48           0 :       return defaultColor == null ? null : Color(defaultColor);
      49             :     } else {
      50             :       try {
      51           1 :         if (!text.startsWith('0x')) {
      52           3 :           text = text.replaceAll('#', '').trim().toUpperCase();
      53             : 
      54           2 :           if (text.length == 3) {
      55          11 :             text = text[0] + text[0] + text[1] + text[1] + text[2] + text[2];
      56             :           }
      57             : 
      58           2 :           if (text.length == 4) {
      59           2 :             text = text[0] +
      60           2 :                 text[0] +
      61           2 :                 text[1] +
      62           2 :                 text[1] +
      63           2 :                 text[2] +
      64           2 :                 text[2] +
      65           2 :                 text[3] +
      66           1 :                 text[3];
      67             :           }
      68             : 
      69           2 :           if (text.length == 6) {
      70           1 :             text = 'FF$text';
      71             :           }
      72             : 
      73           2 :           if (text.length > 8) {
      74           1 :             text = text.substring(0, 8);
      75             :           }
      76             : 
      77           1 :           text = '0x$text';
      78             :         }
      79             : 
      80           2 :         return Color(int.parse(text));
      81           1 :       } on Exception catch (_) {
      82           0 :         return defaultColor == null ? null : Color(defaultColor);
      83             :       }
      84             :     }
      85             :   }
      86             : 
      87             :   ///
      88             :   ///
      89             :   ///
      90           0 :   @override
      91           0 :   String? valid(String? value) => parse(value) == null ? 'Cor inválida.' : null;
      92             : }

Generated by: LCOV version 1.14