Skip to content

Instantly share code, notes, and snippets.

@iapicca
Last active September 8, 2022 20:49
Show Gist options
  • Select an option

  • Save iapicca/b0bc58fa91bb6a51ca9b11026b220d4c to your computer and use it in GitHub Desktop.

Select an option

Save iapicca/b0bc58fa91bb6a51ca9b11026b220d4c to your computer and use it in GitHub Desktop.
emoji
// import 'dart:convert';
// import 'dart:math';
// import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
const emoji = '💙';
void main() => runApp(const MaterialApp(home: MyHomePage()));
class MyHomePage extends StatefulWidget {
final int maxLength;
const MyHomePage({
super.key,
this.maxLength = 10,
});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late final TextEditingController _controller;
@override
void initState() {
_controller = TextEditingController();
super.initState();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void addEmoji() {
final newText = _controller.text + emoji;
// if (newText.bytesLength > widget.maxLength) {
// return;
// }
_controller.text = newText;
}
@override
Widget build(context) => Scaffold(
body: Center(
child: TextField(
controller: _controller,
maxLength: widget.maxLength,
//inputFormatters: [Utf8LengthInputFormatter(widget.maxLength)],
//buildCounter: CounterBuilderDelegate(controller: _controller),
),
),
floatingActionButton: FloatingActionButton(
onPressed: addEmoji,
child: const Icon(Icons.add),
),
);
}
// extension StringByteLenghtX on String {
// int get bytesLength => const Utf8Encoder().convert(this).length;
// }
// extension IterableStringByteLenghtX on Iterable<String> {
// int get bytesLength => join().bytesLength;
// }
// extension TextEditingValueTruncateX on TextEditingValue {
// TextEditingValue truncate(int maxLenght) {
// if (maxLenght < 1) {
// return const TextEditingValue();
// }
// if (text.bytesLength <= maxLenght) {
// return this;
// }
// final chars = <String>[];
// for (final char in text.characters) {
// final lenght = chars.bytesLength + char.bytesLength;
// if (lenght <= maxLenght) {
// chars.add(char);
// }
// }
// final truncated = chars.join();
// return TextEditingValue(
// text: truncated,
// selection: selection.copyWith(
// baseOffset: min(selection.start, truncated.length),
// extentOffset: min(selection.end, truncated.length),
// ),
// composing: TextRange.empty,
// );
// }
// }
// class Utf8LengthInputFormatter extends TextInputFormatter {
// final int maxLength;
// final bool keepOldValue;
// Utf8LengthInputFormatter(
// this.maxLength, {
// this.keepOldValue = false,
// });
// @override
// TextEditingValue formatEditUpdate(
// TextEditingValue oldValue,
// TextEditingValue newValue,
// ) {
// if (newValue.text.bytesLength <= maxLength) {
// return newValue;
// }
// if (keepOldValue && oldValue.text.bytesLength <= maxLength) {
// return oldValue;
// }
// return newValue.truncate(maxLength);
// }
// }
// class CounterBuilderDelegate {
// final TextEditingController controller;
// const CounterBuilderDelegate({
// required this.controller,
// });
// Widget call(
// BuildContext context, {
// required int currentLength,
// required bool isFocused,
// required int? maxLength,
// }) =>
// Text(
// '${controller.text.bytesLength}/$maxLength',
// style: Theme.of(context).textTheme.caption,
// );
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment