我有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/