如何在一个按钮上插入两个动作 OnPress - 删除小部件并通过新建创建它?
How to insert two action at one button OnPress - delete widget and create it by new?
我尝试显示的图片:
1-1 - 看起来错误。
2-2 - 我是怎么解决的。
但它只能通过同时按下两个按钮(首先删除然后随机)才能工作
最好只做一个动作,我还没做
并且我尝试插入代码以删除小部件并通过一键操作创建新小部件。但它不起作用。
我不明白为什么以及如何让它发挥作用?
代码: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();
});
},
我尝试显示的图片:
1-1 - 看起来错误。
2-2 - 我是怎么解决的。
但它只能通过同时按下两个按钮(首先删除然后随机)才能工作
最好只做一个动作,我还没做
并且我尝试插入代码以删除小部件并通过一键操作创建新小部件。但它不起作用。 我不明白为什么以及如何让它发挥作用?
代码: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();
});
},