flutter - 如何访问textformfield显示的内容

标签 flutter dart

我有textformfield我想访问它显示的内容
就像我打印10000时一样,我希望它像10,000一样显示并分离整数,我可以使用regex打印它,但我也想在文本表单字段中显示它
这是我们在终端中得到的内容(left)及其在文本字段中显示的内容(right)
enter image description here
我想做的就是显示终端到文本字段的内容\

如果您需要更多信息,请让我知道
这是我的代码

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',
      ),
    );
  }
}

最佳答案

您必须使用TextEditingController类。

A controller for an editable text field.


Whenever the user modifies a text field with an associated TextEditingController, the text field updates value and the controller notifies its listeners. Listeners can then read the text and selection properties to learn what the user has typed or how the selection has been updated.


引用:https://api.flutter.dev/flutter/widgets/TextEditingController-class.html
请检查下面的代码,我已将其更新为根据您的问题工作的代码。
import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: AddTransication(),
    );
  }
}

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

class _AddTransicationState extends State<AddTransication> {
  final _controller = TextEditingController();
  RegExp regex = new RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))');
  Function mathFunc = (Match match) => '${match[1]},';
  void initState() {
    super.initState();
  }

  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @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(regex, mathFunc)],
          ),
        ),
      ),
    );
  }

  Widget emailField(regex, mathFunc) {
    return TextFormField(
      controller: _controller,
      onChanged: (str) {
        String text = str.replaceAll(",", "").replaceAllMapped(regex, mathFunc);
        print(' $text');
        _controller.value = _controller.value.copyWith(
          text: text,
          selection:
              TextSelection(baseOffset: text.length, extentOffset: text.length),
          composing: TextRange.empty,
        );
      },
      keyboardType: TextInputType.emailAddress,
      decoration: InputDecoration(
        labelText: 'Email Address',
        hintText: 'you@example.com',
      ),
    );
  }
}

关于flutter - 如何访问textformfield显示的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64739892/

相关文章:

firebase - Flutter Firebase 存储 : no auth token for request

flutter - 我如何在 flutter 的 child 体内使用if/else语句?

flutter - 在小部件的构建方法中执行导航? - flutter

dart - 剪切后 Material 高度不起作用

flutter - 在 flutter 中从父颜色生成自定义颜色阴影

intellij-idea - 设置VM标志失败

flutter - 如何在 flutter 中为表格添加圆形边框

dart - 如何在 Dart 中覆盖来自不同库的私有(private)方法?

android - 带自定义按钮的抽屉导航

android - 访问被拒绝在 Android 上查找属性 "net.dns1"