在按按钮上更新TextFormField
我有一个初学者问题。 我不明白为什么在此示例中未更新TextFormfield时,请按floatingActionButton。 _ Counter字段在SetState内部增加。文本小部件已按预期更新。
我看到此区别:文本无状态和TextFormfield Statefull小部件。
我该怎么做才能在TextFormfield中显示正确的值?
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Your number:',
),
TextFormField(
keyboardType: const TextInputType.numberWithOptions(
signed: false,
decimal: false,
),
initialValue: (_counter.toString()),
onChanged: (val) {
setState(() {
_counter = int.parse(val);
});
},
),
Text(
'$_counter',
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
谢谢你
QJETA
I have a beginner question.
I don't understand why TextFormField is not updated in this example on pressing the FloatingActionButton. _counter field is incremented inside of setState. Text widget is updated as expected.
I see this difference: Text is stateless and TextFormField statefull widget.
What should I do to show the correct value also inside TextFormField?
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Your number:',
),
TextFormField(
keyboardType: const TextInputType.numberWithOptions(
signed: false,
decimal: false,
),
initialValue: (_counter.toString()),
onChanged: (val) {
setState(() {
_counter = int.parse(val);
});
},
),
Text(
'$_counter',
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
Thank you
Qjeta
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
欢迎使用 Flutter
当 widget 构建时,initialValue 设置值,您需要使用其他东西来更新文本字段。这是按您想要的方式工作的完整代码。
您必须使用 TextEditingController 并删除初始值
Welcome To Flutter
initialValue set value on time when the widget is build, you need to use something else to update the textfield. here is the complete code that works as you want.
You have to use the TextEditingController and remove the initialValue
问题:
无法更改“初始值”参数的值
^^
Problem :
the value of the "initialValue" argument can not be changed
Solution ^^