どうも、ちょげ(@chogetarou)です。
AlertDialogを閉じた時にイベントを起こす法を紹介します。
方法

AlertDialogを閉じた時に処理をする方法は、2つあります。
WillPopScope
1つは、「WillPopScope」を使う方法です。
まず、showDialogで表示するAlertDialogを、「WillPopScope」の「child」に指定します。
そして、WillPopScopの引数「onWillPop」に閉じた時のイベントを設定します。
showDialog(
context: context,
builder: (context) {
return WillPopScope(
child: AlertDialog(
title: Text('Alert'),
),
onWillPop: () async {
//閉じた時の処理
setState(() {
_count++;
});
return true;
},
);
},
);
then
もう1つは、「then」を使う方法です。
showDialog関数の後に、「.then((value) {})」を付け足します。
そして、「then()」内の関数で、閉じた時のイベントを指定します。
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Alert'),
);
},
).then((value) {
//閉じた時の処理
setState(() {
_count++;
});
});
まとめ
「AlertDialog」を閉じる時の処理をする方法は、次の2つです。
- WillPopScopを使う方法
- thenを使う方法
コメント