在我试图显示的图片中:
1-1 - 看起来如何错误。
2-2 - 我是如何解决的。
但它只能通过按下两个按钮来工作(首先是 DELETE,然后是 RANDOM)
只做一个动作会更好,但我没有做过。
我试图插入代码来删除小部件并通过一键操作新建它。但它不起作用。我不明白为什么以及如何使它工作?
代码:https : //github.com/develop86229/editTextControl
FlatButton(
child: Text("RANDOM"),
onPressed: () {
setState(() {
textWidget = Container();
textWidget = Form(
key: _textKey,
child: TextFormField(
controller: myTextController,
));
myTextController.text = rnd.nextInt(1000000000).toString();
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
textWidget,
FlatButton(
child: Text("RANDOM"),
onPressed: () {
setState(() {
textWidget = Container();
textWidget = Form(
key: _textKey,
child: TextFormField(
controller: myTextController,
));
});
myTextController.text = rnd.nextInt(1000000000).toString();
}),
FlatButton(
child: Text("DELETE"),
onPressed: () {
setState(() {
textWidget = Container();
});
}),
],
),
),
);
发生这种情况是因为您在状态中设置了一个新值,然后在其中设置了另一个值,然后才会发生更新。这按预期工作。
但是,如果您想先删除该项目,然后再创建一个新项目,您可以像这样延迟第二个操作:
onPressed: () {
setState(() {
textWidget = Container();
});
Future.delayed(const Duration(milliseconds: 500), () {
setState(() {
textWidget = Form(
key: _textKey,
child: TextFormField(
controller: myTextController,
));
});
myTextController.text = rnd.nextInt(1000000000).toString();
});
},
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句