Skip to content

Commit 1f35da5

Browse files
committed
编辑文章,未保存的时候点退出增加提示
1 parent f54239d commit 1f35da5

1 file changed

Lines changed: 38 additions & 5 deletions

File tree

lib/pages/editor_page.dart

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -564,17 +564,50 @@ class _EditorPageState extends State<EditorPage> {
564564
}
565565
}
566566

567+
Future<bool> _confirmDiscard() async {
568+
if (!_dirty) return true;
569+
final s = AppStrings.current;
570+
final result = await showDialog<bool>(
571+
context: context,
572+
builder: (ctx) => AlertDialog(
573+
title: Text(s.unsavedChanges),
574+
content: Text('${s.unsavedChangesDesc}\n\n${AppStrings.isZh ? '离开将丢失这些未保存的改动' : 'Leaving will discard unsaved changes'}'),
575+
actions: [
576+
TextButton(
577+
onPressed: () => Navigator.pop(ctx, false),
578+
child: Text(s.cancel),
579+
),
580+
TextButton(
581+
onPressed: () => Navigator.pop(ctx, true),
582+
child: Text(AppStrings.isZh ? '确认' : 'Confirm'),
583+
),
584+
],
585+
),
586+
);
587+
return result ?? false;
588+
}
589+
567590
@override
568591
Widget build(BuildContext context) {
569592
final wide = Responsive.isWide(context);
570593
final s = AppStrings.current;
571594

572-
return Scaffold(
573-
appBar: AppBar(
574-
title: Text(_editingArticle == null ? s.newArticle : s.editorTitle),
575-
actions: _buildAppBarActions(wide),
595+
return PopScope(
596+
canPop: false,
597+
onPopInvokedWithResult: (didPop, result) async {
598+
if (didPop) return;
599+
final discard = await _confirmDiscard();
600+
if (discard && context.mounted) {
601+
Navigator.pop(context);
602+
}
603+
},
604+
child: Scaffold(
605+
appBar: AppBar(
606+
title: Text(_editingArticle == null ? s.newArticle : s.editorTitle),
607+
actions: _buildAppBarActions(wide),
608+
),
609+
body: SafeArea(child: wide ? _buildWide() : _buildNarrow()),
576610
),
577-
body: SafeArea(child: wide ? _buildWide() : _buildNarrow()),
578611
);
579612
}
580613

0 commit comments

Comments
 (0)