【问题标题】:how to get access to what textformfield shows如何访问 textformfield 显示的内容
【发布时间】:2020-11-08 15:36:24
【问题描述】:

我有 textformfield 我想访问它显示的内容
就像当我打印 10000 我希望它显示和分隔 int 一样 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',
      ),
    );
  }
}

【问题讨论】:

标签: flutter dart


【解决方案1】:

你必须使用 TextEditingController 类。

可编辑文本字段的控制器。

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

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

【讨论】:

  • 请替换 ===> String text = _controller.text .replaceAll(",", "") .replaceAllMapped(regex, mathFunc);
  • 只是一个小问题,当我写 111111 文本字段时显示 111,111 但在控制台中我得到了这个 11,1111 我该如何处理?
  • 我已经更新了代码,现在它将完全按照控制台显示。
猜你喜欢
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多