2022-10-04 13:12:54 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Yubico.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-07-06 16:22:15 +03:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
2023-02-28 13:34:29 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2022-07-06 16:22:15 +03:00
|
|
|
|
|
|
|
/// Get the number of bytes used by a String when encoded to UTF-8.
|
|
|
|
int byteLength(String value) => utf8.encode(value).length;
|
|
|
|
|
|
|
|
/// Builds a counter widget showing number of bytes used/available.
|
|
|
|
///
|
|
|
|
/// Set this as a [TextField.buildCounter] callback to show the number of bytes
|
|
|
|
/// used rather than number of characters. [currentValue] should always match
|
|
|
|
/// the input text value to measure.
|
|
|
|
InputCounterWidgetBuilder buildByteCounterFor(String currentValue) =>
|
2022-11-22 12:24:11 +03:00
|
|
|
(context, {required currentLength, required isFocused, maxLength}) {
|
|
|
|
final theme = Theme.of(context);
|
2023-01-26 13:52:01 +03:00
|
|
|
final caption = theme.textTheme.bodySmall;
|
2022-11-22 12:24:11 +03:00
|
|
|
final style = (byteLength(currentValue) <= (maxLength ?? 0))
|
|
|
|
? caption
|
2023-01-26 13:52:01 +03:00
|
|
|
: caption?.copyWith(color: theme.colorScheme.error);
|
2022-11-22 12:24:11 +03:00
|
|
|
return Text(
|
|
|
|
maxLength != null ? '${byteLength(currentValue)}/$maxLength' : '',
|
|
|
|
style: style,
|
2023-02-28 17:02:12 +03:00
|
|
|
semanticsLabel: AppLocalizations.of(context)!.l_character_count,
|
2022-11-22 12:24:11 +03:00
|
|
|
);
|
|
|
|
};
|
2022-07-06 16:22:15 +03:00
|
|
|
|
|
|
|
/// Limits the input in length based on the byte length when encoded.
|
|
|
|
/// This is generally used together with [buildByteCounterFor].
|
|
|
|
TextInputFormatter limitBytesLength(int maxByteLength) =>
|
|
|
|
TextInputFormatter.withFunction((oldValue, newValue) {
|
|
|
|
final newLength = byteLength(newValue.text);
|
|
|
|
if (newLength <= maxByteLength) {
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
return oldValue;
|
|
|
|
});
|