Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何访问textformfield显示的内容_Flutter_Dart - Fatal编程技术网

Flutter 如何访问textformfield显示的内容

Flutter 如何访问textformfield显示的内容,flutter,dart,Flutter,Dart,我有textformfield,我想访问它显示的内容 例如,当我打印10000时,我希望它显示并分隔整数,例如10000,我可以使用regex打印它,但我也希望在文本表单字段中显示它 下面是我在终端(左侧)中得到的内容,以及它在文本字段(右侧)中显示的内容 我想做的就是显示终端到文本字段中的内容\ . 如果您需要更多信息,请告诉我 这是我的密码 import 'package:flutter/material.dart'; class AddTransication extends Stat

我有textformfield,我想访问它显示的内容
例如,当我打印10000时,我希望它显示并分隔整数,例如10000,我可以使用
regex
打印它,但我也希望在文本表单字段中显示它
下面是我在终端(
左侧
)中得到的内容,以及它在文本字段(
右侧
)中显示的内容

我想做的就是显示终端到文本字段中的内容\

.
如果您需要更多信息,请告诉我
这是我的密码

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',
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
类AddTransaction扩展了StatefulWidget{
@凌驾
_AddTransactionState createState()=>\u AddTransactionState();
}
类_addTransactionState扩展状态{
RegExp reg_ex=新的RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d));
函数mathFunc=(匹配)=>“${Match[1]}”;
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“测试屏幕”),
行动:[
扁平按钮(
textColor:Colors.white,
按下:(){},
子项:文本(“保存”),
形状:圆形顺序(边:边框边(颜色:Colors.transparent)),
),
],
),
正文:安全区(
孩子:表格(
子:列(
儿童:[emailField(reg_ex,mathFunc)],
),
),
),
);
}
Widget emailField(注册表项,mathFunc){
返回TextFormField(
一旦更改:(str){
字符串结果=str.replaceAllMapped(reg_ex,mathFunc);
打印(“$result”);
},
键盘类型:TextInputType.emailAddress,
装饰:输入装饰(
labelText:“电子邮件地址”,
hintText:'you@example.com',
),
);
}
}

您必须使用TextEditingController类

可编辑文本字段的控制器

每当用户使用关联的TextEditingController修改文本字段时,文本字段都会更新值,并且控制器会通知其侦听器。然后,侦听器可以读取文本和选择属性,以了解用户键入的内容或选择内容的更新方式

参考:

请检查下面的代码,我已经根据您的问题对其进行了更新

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',
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
最终颜色深蓝色=颜色。来自argb(255,18,32,47);
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
主题:ThemeData.dark().copyWith(脚手架背景颜色:深蓝色),
debugShowCheckedModeBanner:false,
主页:addTransition(),
);
}
}
类AddTransaction扩展了StatefulWidget{
@凌驾
_AddTransactionState createState()=>\u AddTransactionState();
}
类_addTransactionState扩展状态{
最终_controller=TextEditingController();
RegExp regex=newregexp(r'(\d{1,3})(?=(\d{3})+(?!\d));
函数mathFunc=(匹配)=>“${Match[1]}”;
void initState(){
super.initState();
}
无效处置(){
_controller.dispose();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“测试屏幕”),
行动:[
扁平按钮(
textColor:Colors.white,
按下:(){},
子项:文本(“保存”),
形状:圆形顺序(边:边框边(颜色:Colors.transparent)),
),
],
),
正文:安全区(
孩子:表格(
子:列(
儿童:[emailField(regex,mathFunc)],
),
),
),
);
}
Widget emailField(regex,mathFunc){
返回TextFormField(
控制器:_控制器,
一旦更改:(str){
字符串text=str.replaceAll(“,”,“”)。replaceAllMapping(regex,mathFunc);
打印(“$text”);
_controller.value=\u controller.value.copyWith(
文本:文本,
选择:
text选择(baseOffset:text.length,extendtoffset:text.length),
组合:TextRange.empty,
);
},
键盘类型:TextInputType.emailAddress,
装饰:输入装饰(
labelText:“电子邮件地址”,
hintText:'you@example.com',
),
);
}
}

检查上面的包id可能会有所帮助,将ad掩码改为文本输入检查我的答案,我已经更新了我的答案并添加了符合您问题的代码:Please replace===>String text=\u controller.text.replaceAll(“,”,”).replaceAllMapping(regex,mathFunc);只有一个小问题,当我写111111文本字段show 111111,但在控制台我得到了这个111111,我如何处理它?我已经更新了代码,现在它将显示完全如控制台所示。