gpt4 book ai didi

flutter - Flutter TextFormField装饰

转载 作者:行者123 更新时间:2023-12-03 03:33:17 25 4
gpt4 key购买 nike

如何在TextFormfield中将数字“123456789”显示为“123 456 789”?

TextFormField(
controller: _numberId,
keyboardType: TextInputType.number,
),

最佳答案

我在String上写以下扩展:

extension StringSeprate on String {
String stringSeparate(
{int count = 3, String separator = ",", bool fromRightToLeft = true}) {
if (this.isEmpty) {
return "";
}

if (count < 1) {
return this;
}

if (count >= this.length) {
return this;
}

var str = this.replaceAll(separator, "");

if (fromRightToLeft) {
str = String.fromCharCodes(str.runes.toList().reversed);
}

var chars = str.runes.toList();
var namOfSeparation =
(chars.length.toDouble() / count.toDouble()).ceil() - 1;
var separatedChars = List(chars.length + namOfSeparation.round());
var j = 0;
for (var i = 0; i < chars.length; i++) {
separatedChars[j] = String.fromCharCode(chars[i]);
if (i > 0 && (i + 1) < chars.length && (i + 1) % count == 0) {
j += 1;
separatedChars[j] = separator;
}

j += 1;
}

return fromRightToLeft
? String.fromCharCodes(separatedChars.join().runes.toList().reversed)
: separatedChars.join();
}
}
最终代码(将以下代码复制到文件中,然后从main调用以进行测试):
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class TestWidget extends StatelessWidget {
TextEditingController _textEditingController = TextEditingController();

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: _textEditingController,
inputFormatters: [
TextInputFormatter.withFunction((oldValue, newValue) =>
TextEditingValue(
text: newValue.text.stringSeparate(
separator: ' ', fromRightToLeft: false)))
],
onChanged: (String value) {
if (value == null) return;
_textEditingController.selection = TextSelection.fromPosition(
TextPosition(
offset: value.length, affinity: TextAffinity.upstream));
},
),
],
),
),
);
}
}

extension StringSeprate on String {
String stringSeparate(
{int count = 3, String separator = ",", bool fromRightToLeft = true}) {
if (this.isEmpty) {
return "";
}

if (count < 1) {
return this;
}

if (count >= this.length) {
return this;
}

var str = this.replaceAll(separator, "");

var chars = str.runes.toList();
var namOfSeparation =
(chars.length.toDouble() / count.toDouble()).ceil() - 1;
var separatedChars = List(chars.length + namOfSeparation.round());
var j = 0;
for (var i = 0; i < chars.length; i++) {
separatedChars[j] = String.fromCharCode(chars[i]);
if (i > 0 && (i + 1) < chars.length && (i + 1) % count == 0) {
j += 1;
separatedChars[j] = separator;
}

j += 1;
}

return fromRightToLeft
? String.fromCharCodes(separatedChars.join().runes.toList().reversed)
: separatedChars.join();
}
}
结果是这样的:

关于flutter - Flutter TextFormField装饰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62517048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com