【问题标题】:how to get updates on a 'TextField'?如何获取“TextField”的更新?
【发布时间】:2020-11-08 14:03:36
【问题描述】:

我想将文本表单域中的每 3 个字符串分开并显示在
我已经尝试过regex 我可以在终端中获取它,但我无法在 textformfield 中显示它,使它看起来像终端,enter image description here
在左侧,当我打印它时,我有我想要的东西,但在右侧,我有我的 textformfield

我想完全像终端一样显示它,但在 textformfield 中 这是我的代码
如果您需要更多信息,请告诉我

import 'package:flutter/material.dart';

class AddTransication extends StatefulWidget {
  @override
  _AddTransicationState createState() => _AddTransicationState();
}

class _AddTransicationState extends State<AddTransication> {
  RegExp reg_ex = new RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))');
  Function mathFunc = (Match match) => '${match[1]},';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Test Screen"),
        actions: <Widget>[
          FlatButton(
            textColor: Colors.white,
            onPressed: () {},
            child: Text("Save"),
            shape: CircleBorder(side: BorderSide(color: Colors.transparent)),
          ),
        ],
      ),
      body: SafeArea(
        child: Form(
          child: Column(
            children: [emailField(reg_ex, mathFunc)],
          ),
        ),
      ),
    );
  }

  Widget emailField(reg_ex, mathFunc) {
    return TextFormField(
      onChanged: (str) {
        String result = str.replaceAllMapped(reg_ex, mathFunc);
        print(' $result');
      },
      keyboardType: TextInputType.emailAddress,
      decoration: InputDecoration(
        labelText: 'Email Address',
        hintText: 'you@example.com',
      ),
    );
  }
}

更新!

我还希望它返回一个有效的 int,就像我写 1000 时它应该返回 10,000,我已经用正则表达式做到了,但我想在那里显示它

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您希望显示文本的方式称为“文本屏蔽”。 有一个库可以帮助您制作此类文本。

    库 1:https://pub.dev/packages/flutter_masked_text

    库 2:https://pub.dev/packages/mask_text_input_formatter

    您可以使用这些库中的任何一个来解决您的问题。祝你好运。

    【讨论】:

    • 我想要像当输入为 1000 时只返回 1.000 而当它是 10000 时返回 10,000 之类的东西,该怎么做?最后一个结果是这样,但新结果只分隔数字,这意味着当我写 10000 它返回 100,00 时,包也有一些错误
    • 如果我可以在文本字段中显示正则表达式返回的内容会更好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 2021-01-03
    相关资源
    最近更新 更多