如何在 Flutter 中显示 2 个连续的对话框
How to show 2 consecutive Dialogs in Flutter
我有一个包含 Dismissible 小部件的列表。当用户交换 startToEnd 时,我想在用户可以添加注释时播放一个对话框。一旦用户在该对话框中单击确定,我希望弹出另一个对话框以确认添加了注释,然后在 2 秒后消失。当用户从右向左切换时,我想显示一个包含 2 秒消息的对话框
这是我的代码
List<Container> _buildBasketList() {
return appData.basketList.map((bList) {
var container = Container(
child: Builder(
builder: (context) => Dismissible(
key: Key(UniqueKey().toString()),
background: Container(
margin: EdgeInsets.all(8.0),
color: kColorAccent,
child: Align(
alignment: Alignment(-0.90, 0.00),
child: Icon(Icons.add_comment)),
),
onDismissed: (direction) async {
print('ondismiss started');
if (direction == DismissDirection.startToEnd) {
print('adding a note');
await showDialog(
context: context,
builder: (context) => AddDetailsInDialog(
dialogText: 'Enter your note',
hintText: 'eg. xxx',
)).then((value) async {
if (value != null) {
print('got value');
await showDialog(
context: context,
builder: (context) => ShowAlertAndAutoDismiss(
text: 'Note added',
));
}
});
} else {
print('deleting record in basketList');
await showDialog(
context: context,
builder: (context) => ShowAlertAndAutoDismiss(
text:
"Item removed from your list",
));
}
},
这是我的 类
class ShowAlertAndAutoDismiss extends StatelessWidget {
final String text;
ShowAlertAndAutoDismiss({this.text});
@override
Widget build(BuildContext context) {
print('building ShowAlertAndAutoDismiss');
Future.delayed(Duration(milliseconds: 1000)).then((_) {
print('ziiiip');
});
return AlertDialog(
title: Center(child: Text(text)),
);
}
}
class AddDetailsInDialog extends StatelessWidget {
final String dialogText;
final String hintText;
final myController = TextEditingController();
AddDetailsInDialog({
this.dialogText,
this.hintText,
});
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(dialogText),
content: new Row(
children: <Widget>[
new Expanded(
child: new TextField(
controller: myController,
autofocus: true,
decoration: new InputDecoration(hintText: hintText),
))
],
),
actions: <Widget>[
FlatButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop(myController.toString());
},
),
],
);
}
@override
void dispose() {
myController.dispose();
}
}
现在由于某些奇怪的原因,AddDetailsInDialog 中的 ShowAlertAndAutoDismiss 被调用了两次......嗯?
这是我从右向左交换(删除)然后从左向右滑动(添加注释)时得到的日志。删除工作但 addNote 被调用 2 次。为什么???有谁明白为什么?
删除时记录
I/flutter ( 7941): ondismiss started
I/flutter ( 7941): deleting record in basketList
I/flutter ( 7941): building ShowAlertAndAutoDismiss
I/flutter ( 7941): ziiiip
添加备注时记录
I/flutter ( 7941): ondismiss started
I/flutter ( 7941): adding a note
I/flutter ( 7941): got value
I/flutter ( 7941): building ShowAlertAndAutoDismiss
I/flutter ( 7941): building ShowAlertAndAutoDismiss
I/flutter ( 7941): ziiiip
I/flutter ( 7941): ziiiip
所以我通过在函数中移动 ShowAlertAndAutoDismiss 来解决
void alertToDismiss() {
showDialog(
context: context,
builder: (context) => ShowAlertAndAutoDismiss(
text: 'Note added',
));
}
...
await showDialog(
context: context,
builder: (context) => AddDetailsInDialog(
dialogText: 'Enter your note',
hintText: 'eg. xxx',
)).then((value) async {
if (value != null) {
print('got value');
alertToDismiss():
}
看起来这个小部件的行为类似于 FutureBuilder,并且首先使用 null 立即调用构建器,然后在解决 future 之后
我有一个包含 Dismissible 小部件的列表。当用户交换 startToEnd 时,我想在用户可以添加注释时播放一个对话框。一旦用户在该对话框中单击确定,我希望弹出另一个对话框以确认添加了注释,然后在 2 秒后消失。当用户从右向左切换时,我想显示一个包含 2 秒消息的对话框
这是我的代码
List<Container> _buildBasketList() {
return appData.basketList.map((bList) {
var container = Container(
child: Builder(
builder: (context) => Dismissible(
key: Key(UniqueKey().toString()),
background: Container(
margin: EdgeInsets.all(8.0),
color: kColorAccent,
child: Align(
alignment: Alignment(-0.90, 0.00),
child: Icon(Icons.add_comment)),
),
onDismissed: (direction) async {
print('ondismiss started');
if (direction == DismissDirection.startToEnd) {
print('adding a note');
await showDialog(
context: context,
builder: (context) => AddDetailsInDialog(
dialogText: 'Enter your note',
hintText: 'eg. xxx',
)).then((value) async {
if (value != null) {
print('got value');
await showDialog(
context: context,
builder: (context) => ShowAlertAndAutoDismiss(
text: 'Note added',
));
}
});
} else {
print('deleting record in basketList');
await showDialog(
context: context,
builder: (context) => ShowAlertAndAutoDismiss(
text:
"Item removed from your list",
));
}
},
这是我的 类
class ShowAlertAndAutoDismiss extends StatelessWidget {
final String text;
ShowAlertAndAutoDismiss({this.text});
@override
Widget build(BuildContext context) {
print('building ShowAlertAndAutoDismiss');
Future.delayed(Duration(milliseconds: 1000)).then((_) {
print('ziiiip');
});
return AlertDialog(
title: Center(child: Text(text)),
);
}
}
class AddDetailsInDialog extends StatelessWidget {
final String dialogText;
final String hintText;
final myController = TextEditingController();
AddDetailsInDialog({
this.dialogText,
this.hintText,
});
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(dialogText),
content: new Row(
children: <Widget>[
new Expanded(
child: new TextField(
controller: myController,
autofocus: true,
decoration: new InputDecoration(hintText: hintText),
))
],
),
actions: <Widget>[
FlatButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop(myController.toString());
},
),
],
);
}
@override
void dispose() {
myController.dispose();
}
}
现在由于某些奇怪的原因,AddDetailsInDialog 中的 ShowAlertAndAutoDismiss 被调用了两次......嗯?
这是我从右向左交换(删除)然后从左向右滑动(添加注释)时得到的日志。删除工作但 addNote 被调用 2 次。为什么???有谁明白为什么?
删除时记录
I/flutter ( 7941): ondismiss started
I/flutter ( 7941): deleting record in basketList
I/flutter ( 7941): building ShowAlertAndAutoDismiss
I/flutter ( 7941): ziiiip
添加备注时记录
I/flutter ( 7941): ondismiss started
I/flutter ( 7941): adding a note
I/flutter ( 7941): got value
I/flutter ( 7941): building ShowAlertAndAutoDismiss
I/flutter ( 7941): building ShowAlertAndAutoDismiss
I/flutter ( 7941): ziiiip
I/flutter ( 7941): ziiiip
所以我通过在函数中移动 ShowAlertAndAutoDismiss 来解决
void alertToDismiss() {
showDialog(
context: context,
builder: (context) => ShowAlertAndAutoDismiss(
text: 'Note added',
));
}
...
await showDialog(
context: context,
builder: (context) => AddDetailsInDialog(
dialogText: 'Enter your note',
hintText: 'eg. xxx',
)).then((value) async {
if (value != null) {
print('got value');
alertToDismiss():
}
看起来这个小部件的行为类似于 FutureBuilder,并且首先使用 null 立即调用构建器,然后在解决 future 之后