JJ_MKVMaker/lib/ui/progress_dialog.dart

42 lines
1.6 KiB
Dart

import 'package:flutter/material.dart';
import '../l10n/tr.dart';
/// 바꾸는 · 여는 동안의 진행 창 ([취소] 있음). 관리자 지시 (10/20, 28 지적): Android ← · [취소] 로 창이 먼저 닫힐 수 있어,
/// 일이 끝난 뒤에는 그 창이 아직 열려 있을 때만 닫는다 (예전에는 끝난 뒤의 pop() 이 다른 화면을 닫을 수 있었다).
/// - [ProgressDialog.cancelled]: 사용자가 [취소] · ← 로 그만두었는지 (그러면 결과를 보이지 않는다)
/// - [ProgressDialog.close]: 일이 끝나 창을 닫는다 (이미 닫혔으면 아무것도 하지 않는다)
class ProgressDialog {
final Future<void> done;
final bool Function() cancelled;
final void Function() close;
const ProgressDialog._(this.done, this.cancelled, this.close);
}
ProgressDialog showProgressDialog(BuildContext context, String text) {
BuildContext? dctx;
var open = true;
var byUser = true;
final done = showDialog<void>(
context: context,
barrierDismissible: false,
builder: (ctx) {
dctx = ctx;
return AlertDialog(
content: Row(children: [
const CircularProgressIndicator(),
const SizedBox(width: 16),
Expanded(child: Text(text)),
]),
actions: [TextButton(onPressed: () => Navigator.of(ctx).pop(), child: Text(tr('취소')))],
);
},
).whenComplete(() => open = false);
return ProgressDialog._(done, () => !open && byUser, () {
if (!open) return;
byUser = false;
final x = dctx;
if (x != null && x.mounted) Navigator.of(x).pop();
});
}