|
1 | 1 | // global_settings_page.dart |
| 2 | +import 'dart:async'; |
2 | 3 | import 'dart:io'; |
3 | 4 |
|
4 | 5 | import 'package:file_picker_ohos/file_picker_ohos.dart'; |
@@ -55,131 +56,156 @@ class _GlobalSettingsPageState extends State<GlobalSettingsPage> { |
55 | 56 | bool passwordObscure = true; |
56 | 57 | bool confirmPasswordObscure = true; |
57 | 58 |
|
| 59 | + // 保存主页面的 context |
| 60 | + final BuildContext pageContext = context; |
| 61 | + |
58 | 62 | showDialog( |
59 | | - context: context, |
60 | | - builder: (dialogContext) => StatefulBuilder( |
61 | | - builder: (context, setState) { |
62 | | - return AlertDialog( |
63 | | - title: const Text('备份数据'), |
64 | | - content: SingleChildScrollView( |
65 | | - child: Column( |
66 | | - mainAxisSize: MainAxisSize.min, |
67 | | - children: [ |
68 | | - const SizedBox(height: 16), |
69 | | - TextField( |
70 | | - controller: passwordController, |
71 | | - obscureText: passwordObscure, |
72 | | - decoration: InputDecoration( |
73 | | - labelText: '请设置密码', |
74 | | - border: OutlineInputBorder( |
75 | | - borderRadius: BorderRadius.circular(12.0), |
76 | | - ), |
77 | | - suffixIcon: IconButton( |
78 | | - icon: Icon(passwordObscure |
79 | | - ? Icons.visibility |
80 | | - : Icons.visibility_off), |
81 | | - onPressed: () => |
82 | | - setState(() => passwordObscure = !passwordObscure), |
| 63 | + context: pageContext, |
| 64 | + builder: (dialogContext) { |
| 65 | + return StatefulBuilder( |
| 66 | + builder: (context, setState) { |
| 67 | + return AlertDialog( |
| 68 | + title: const Text('备份数据'), |
| 69 | + content: SingleChildScrollView( |
| 70 | + child: Column( |
| 71 | + mainAxisSize: MainAxisSize.min, |
| 72 | + children: [ |
| 73 | + const SizedBox(height: 16), |
| 74 | + TextField( |
| 75 | + controller: passwordController, |
| 76 | + obscureText: passwordObscure, |
| 77 | + decoration: InputDecoration( |
| 78 | + labelText: '请设置密码', |
| 79 | + border: OutlineInputBorder( |
| 80 | + borderRadius: BorderRadius.circular(12.0), |
| 81 | + ), |
| 82 | + suffixIcon: IconButton( |
| 83 | + icon: Icon(passwordObscure |
| 84 | + ? Icons.visibility |
| 85 | + : Icons.visibility_off), |
| 86 | + onPressed: () => setState( |
| 87 | + () => passwordObscure = !passwordObscure), |
| 88 | + ), |
83 | 89 | ), |
84 | 90 | ), |
85 | | - ), |
86 | | - const SizedBox(height: 12), |
87 | | - TextField( |
88 | | - controller: confirmPasswordController, |
89 | | - obscureText: confirmPasswordObscure, |
90 | | - decoration: InputDecoration( |
91 | | - labelText: '确认密码', |
92 | | - border: OutlineInputBorder( |
93 | | - borderRadius: BorderRadius.circular(12.0), |
94 | | - ), |
95 | | - suffixIcon: IconButton( |
96 | | - icon: Icon(confirmPasswordObscure |
97 | | - ? Icons.visibility |
98 | | - : Icons.visibility_off), |
99 | | - onPressed: () => setState(() => |
100 | | - confirmPasswordObscure = !confirmPasswordObscure), |
| 91 | + const SizedBox(height: 12), |
| 92 | + TextField( |
| 93 | + controller: confirmPasswordController, |
| 94 | + obscureText: confirmPasswordObscure, |
| 95 | + decoration: InputDecoration( |
| 96 | + labelText: '确认密码', |
| 97 | + border: OutlineInputBorder( |
| 98 | + borderRadius: BorderRadius.circular(12.0), |
| 99 | + ), |
| 100 | + suffixIcon: IconButton( |
| 101 | + icon: Icon(confirmPasswordObscure |
| 102 | + ? Icons.visibility |
| 103 | + : Icons.visibility_off), |
| 104 | + onPressed: () => setState(() => |
| 105 | + confirmPasswordObscure = !confirmPasswordObscure), |
| 106 | + ), |
101 | 107 | ), |
102 | 108 | ), |
103 | | - ), |
104 | | - const SizedBox(height: 8), |
105 | | - if (Platform.isAndroid) |
106 | | - const Text( |
107 | | - '备份文件将保存在应用私有目录中,您可以通过文件管理器访问。', |
108 | | - style: TextStyle(fontSize: 12), |
109 | | - ), |
110 | | - ], |
111 | | - ), |
112 | | - ), |
113 | | - actions: [ |
114 | | - OutlinedButton( |
115 | | - onPressed: () => Navigator.of(context).pop(), |
116 | | - child: const Text('取消'), |
| 109 | + const SizedBox(height: 8), |
| 110 | + if (Platform.isAndroid) |
| 111 | + const Text( |
| 112 | + '备份文件将保存在应用私有目录中,您可以通过文件管理器访问。', |
| 113 | + style: TextStyle(fontSize: 12), |
| 114 | + ), |
| 115 | + ], |
| 116 | + ), |
117 | 117 | ), |
118 | | - OutlinedButton( |
119 | | - onPressed: () async { |
120 | | - if (passwordController.text.isEmpty) { |
121 | | - ScaffoldMessenger.of(context) |
122 | | - .showSnackBar(const SnackBar(content: Text('请输入密码'))); |
123 | | - return; |
124 | | - } |
125 | | - if (passwordController.text != |
126 | | - confirmPasswordController.text) { |
127 | | - ScaffoldMessenger.of(context).showSnackBar( |
128 | | - const SnackBar(content: Text('两次输入的密码不一致'))); |
129 | | - return; |
130 | | - } |
131 | | - |
132 | | - final NavigatorState nav = Navigator.of(context); |
133 | | - nav.pop(); |
134 | | - |
135 | | - // 显示加载对话框 |
136 | | - showDialog( |
137 | | - context: context, |
138 | | - barrierDismissible: false, |
139 | | - builder: (context) => const AlertDialog( |
140 | | - backgroundColor: Colors.transparent, |
141 | | - content: Column( |
142 | | - mainAxisSize: MainAxisSize.min, |
143 | | - children: [ |
144 | | - CircularProgressIndicator(), |
145 | | - SizedBox(height: 16), |
146 | | - Text('正在备份数据...'), |
147 | | - ], |
| 118 | + actions: [ |
| 119 | + OutlinedButton( |
| 120 | + onPressed: () => Navigator.of(context).pop(), |
| 121 | + child: const Text('取消'), |
| 122 | + ), |
| 123 | + OutlinedButton( |
| 124 | + onPressed: () async { |
| 125 | + if (passwordController.text.isEmpty) { |
| 126 | + ScaffoldMessenger.of(pageContext) |
| 127 | + .showSnackBar(const SnackBar(content: Text('请输入密码'))); |
| 128 | + return; |
| 129 | + } |
| 130 | + if (passwordController.text != |
| 131 | + confirmPasswordController.text) { |
| 132 | + ScaffoldMessenger.of(pageContext).showSnackBar( |
| 133 | + const SnackBar(content: Text('两次输入的密码不一致'))); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + Navigator.of(context).pop(); |
| 138 | + |
| 139 | + final completer = Completer<void>(); |
| 140 | + |
| 141 | + final loadingDialogFuture = showDialog( |
| 142 | + context: pageContext, |
| 143 | + barrierDismissible: false, |
| 144 | + builder: (context) => const AlertDialog( |
| 145 | + backgroundColor: Colors.transparent, |
| 146 | + content: Column( |
| 147 | + mainAxisSize: MainAxisSize.min, |
| 148 | + children: [ |
| 149 | + CircularProgressIndicator(), |
| 150 | + SizedBox(height: 16), |
| 151 | + Text('正在备份数据...'), |
| 152 | + ], |
| 153 | + ), |
148 | 154 | ), |
149 | | - ), |
150 | | - ); |
| 155 | + ); |
| 156 | + |
| 157 | + Future<void> backupOperation() async { |
| 158 | + try { |
| 159 | + final filePath = await _backupService |
| 160 | + .backupData(passwordController.text); |
151 | 161 |
|
152 | | - try { |
153 | | - // 执行备份 |
154 | | - final filePath = await _backupService |
155 | | - .backupData(passwordController.text); |
| 162 | + if (!mounted) return; |
156 | 163 |
|
157 | | - // 关闭加载对话框 |
158 | | - if (Navigator.of(context, rootNavigator: true).canPop()) { |
159 | | - Navigator.of(context, rootNavigator: true).pop(); |
160 | | - } |
| 164 | + completer.complete(); |
161 | 165 |
|
162 | | - // 显示结果 |
163 | | - _showResultDialog('备份成功', '备份文件已保存到:\n$filePath'); |
164 | | - } catch (e) { |
165 | | - // 关闭加载对话框 |
166 | | - if (Navigator.of(context, rootNavigator: true).canPop()) { |
167 | | - Navigator.of(context, rootNavigator: true).pop(); |
| 166 | + await Future.delayed(const Duration(milliseconds: 100)); |
| 167 | + |
| 168 | + if (Navigator.of(pageContext, rootNavigator: true) |
| 169 | + .canPop()) { |
| 170 | + Navigator.of(pageContext, rootNavigator: true).pop(); |
| 171 | + } |
| 172 | + |
| 173 | + await loadingDialogFuture; |
| 174 | + |
| 175 | + _showResultDialog( |
| 176 | + pageContext, '备份成功', '备份文件已保存到:\n$filePath'); |
| 177 | + } catch (e) { |
| 178 | + if (!mounted) return; |
| 179 | + |
| 180 | + completer.completeError(e); |
| 181 | + |
| 182 | + await Future.delayed(const Duration(milliseconds: 100)); |
| 183 | + |
| 184 | + if (Navigator.of(pageContext, rootNavigator: true) |
| 185 | + .canPop()) { |
| 186 | + Navigator.of(pageContext, rootNavigator: true).pop(); |
| 187 | + } |
| 188 | + |
| 189 | + await loadingDialogFuture; |
| 190 | + |
| 191 | + _showResultDialog( |
| 192 | + pageContext, '备份失败', '错误: $e\n请检查存储空间'); |
| 193 | + } |
168 | 194 | } |
169 | 195 |
|
170 | | - _showResultDialog('备份失败', '错误: $e\n请检查存储空间'); |
171 | | - } |
172 | | - }, |
173 | | - child: const Text('备份'), |
174 | | - ), |
175 | | - ], |
176 | | - ); |
177 | | - }, |
178 | | - ), |
| 196 | + backupOperation(); |
| 197 | + }, |
| 198 | + child: const Text('备份'), |
| 199 | + ), |
| 200 | + ], |
| 201 | + ); |
| 202 | + }, |
| 203 | + ); |
| 204 | + }, |
179 | 205 | ); |
180 | 206 | } |
181 | 207 |
|
182 | | - void _showResultDialog(String title, String message) { |
| 208 | + void _showResultDialog(BuildContext context, String title, String message) { |
183 | 209 | showDialog( |
184 | 210 | context: context, |
185 | 211 | builder: (context) => AlertDialog( |
|
0 commit comments