-
Notifications
You must be signed in to change notification settings - Fork 4
/
settings_page.dart
197 lines (186 loc) · 5.59 KB
/
settings_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import 'package:flutter/material.dart';
import 'package:hotkey_manager/hotkey_manager.dart';
import 'package:easy_pasta/model/settings_model.dart';
import 'package:easy_pasta/core/settings_service.dart';
import 'package:easy_pasta/widget/settting_page_widgets.dart';
import 'package:easy_pasta/page/confirm_dialog_view.dart';
import 'package:easy_pasta/model/settings_constants.dart';
import 'package:easy_pasta/providers/theme_provider.dart';
import 'package:provider/provider.dart';
class SettingsPage extends StatefulWidget {
const SettingsPage({Key? key}) : super(key: key);
@override
State<SettingsPage> createState() => _SettingsPageState();
}
class _SettingsPageState extends State<SettingsPage> {
final _settingsService = SettingsService();
bool _autoLaunch = false;
HotKey? _hotKey;
final List<SettingItem> _basicSettings = [
const SettingItem(
type: SettingType.hotkey,
title: SettingsConstants.hotkeyTitle,
subtitle: SettingsConstants.hotkeySubtitle,
icon: Icons.keyboard,
),
const SettingItem(
type: SettingType.theme,
title: SettingsConstants.themeTitle,
subtitle: SettingsConstants.themeSubtitle,
icon: Icons.palette,
),
const SettingItem(
type: SettingType.autoLaunch,
title: SettingsConstants.autoLaunchTitle,
subtitle: SettingsConstants.autoLaunchSubtitle,
icon: Icons.launch,
),
const SettingItem(
type: SettingType.maxStorage,
title: SettingsConstants.maxStorageTitle,
subtitle: SettingsConstants.maxStorageSubtitle,
icon: Icons.storage,
),
const SettingItem(
type: SettingType.clearData,
title: SettingsConstants.clearDataTitle,
subtitle: SettingsConstants.clearDataSubtitle,
icon: Icons.delete_outline,
iconColor: Colors.red,
textColor: Colors.red,
),
const SettingItem(
type: SettingType.exitApp,
title: SettingsConstants.exitAppTitle,
subtitle: SettingsConstants.exitAppSubtitle,
icon: Icons.exit_to_app,
iconColor: Colors.red,
textColor: Colors.red,
),
];
@override
void initState() {
super.initState();
_loadSettings();
}
Future<void> _loadSettings() async {
_hotKey = await _settingsService.getHotKey();
_autoLaunch = await _settingsService.getAutoLaunch();
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('设置'),
centerTitle: true,
elevation: 0,
),
body: ListView(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
children: [
_buildSection(
title: SettingsConstants.basicSettingsTitle,
items: _basicSettings,
),
const SizedBox(height: 32),
_buildSection(
title: SettingsConstants.aboutTitle,
items: [
const SettingItem(
type: SettingType.about,
title: SettingsConstants.versionInfoTitle,
subtitle: SettingsConstants.versionInfoSubtitle,
icon: Icons.info_outline,
),
],
),
],
),
);
}
Widget _buildSection({
required String title,
required List<SettingItem> items,
}) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(16),
child: Text(
title,
),
),
Card(
margin: const EdgeInsets.symmetric(horizontal: 8),
child: Column(
children: items.map((item) => _buildSettingTile(item)).toList(),
),
),
],
);
}
Widget _buildSettingTile(SettingItem item) {
switch (item.type) {
case SettingType.hotkey:
return HotkeyTile(
item: item,
hotKey: _hotKey,
onHotKeyChanged: (newHotKey) async {
await _settingsService.setHotKey(newHotKey);
setState(() => _hotKey = newHotKey);
},
);
case SettingType.theme:
return Consumer<ThemeProvider>(
builder: (context, themeProvider, _) {
return ThemeTile(
item: item,
currentThemeMode: themeProvider.themeMode,
onThemeModeChanged: (ThemeMode mode) {
themeProvider.setThemeMode(mode);
},
);
},
);
case SettingType.autoLaunch:
return AutoLaunchTile(
item: item,
value: _autoLaunch,
onChanged: (value) async {
await _settingsService.setAutoLaunch(value);
setState(() => _autoLaunch = value);
},
);
case SettingType.maxStorage:
return MaxStorageTile(item: item);
case SettingType.clearData:
return ClearDataTile(
item: item,
onClear: () => _showClearConfirmDialog(),
);
case SettingType.exitApp:
return ExitAppTile(item: item);
case SettingType.about:
return AboutTile(item: item);
}
}
Future<void> _showClearConfirmDialog() async {
final result = await showDialog<bool>(
context: context,
builder: (context) => const ConfirmDialog(
title: '确认清除',
content: '是否清除所有剪贴板记录?此操作不可恢复。',
confirmText: '确定',
cancelText: '取消',
),
);
if (result == true && mounted) {
await _settingsService.clearAllData(context);
if (mounted) {
Navigator.pop(context);
}
}
}
}