-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
270 lines (251 loc) · 12.4 KB
/
main.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import 'package:animated_flip_counter/animated_flip_counter.dart';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:update_center/update_center.dart';
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(
// You don't have to do this to test Material dialogs
DynamicColorBuilder(
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: darkDynamic,
),
home: const MyApp());
}),
);
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late UpdateCenter updateCenter;
static const String _apiUrl = 'https://example.com/UpdateCenter/update_center.json';
_launchURL(String sourceUrl) async {
final Uri url = Uri.parse(sourceUrl);
if (!await launchUrl(url)) {
throw Exception('Could not launch $sourceUrl');
}
}
@override
void initState() {
initializeUpdateCenter();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Update Center v1.0.0-beta.2+2'),
),
body: Center(
child: OutlinedButton(
onPressed: () =>
checkUpdate(), // You can check for updates using the button
child: const Text('Check update'),
)),
);
}
void initializeUpdateCenter() {
// Main plugin settings
updateCenter = UpdateCenter(
context: context,
urlJson: _apiUrl, // URL to get JSON data for updates (replace with yours)
config: UpdateCenterConfig(
globalConfig: GlobalConfig(
isCheckStart: true, // Whether to check for updates when the app starts
isNoUpdateAvailableToast: false, // Whether to show a toast message if no updates are available
isSourceUrl: false, // You can enable clicking a link instead of downloading a file
isRequestForNotifications: true, // Allows you to request notifications or not
isOpenFile: true, // Determines whether to open an already downloaded file instead of downloading it again (This can be useful if you don't want users to download the file again)
// Widgets for displaying a notification about the unavailability of an update
androidNoUpdateAvailableBuilder: (BuildContext context) {
Fluttertoast.showToast(msg: 'No updates found');
},
iosNoUpdateAvailableBuilder: (BuildContext context) {
Fluttertoast.showToast(msg: 'No updates found');
},
windowsNoUpdateAvailableBuilder: (BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('No updates found'),
duration: Duration(seconds: 2),
));
},
// Constructors of dialogs or widgets for each platform to display the update itself.
androidDialogBuilder: (BuildContext context,
AndroidModel model,
UpdateCenterConfig config,
DownloadState downloadState,
bool allowSkip) {
return showDialog(
context: context,
barrierDismissible: allowSkip,
builder: (BuildContext context) {
return AlertDialog(
title: Text(model.versionName),
content: Text(model.changeLog),
actions: [
if (allowSkip)
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text("Close"),
),
// Changes state to show loading percentage
ValueListenableBuilder<bool>(
valueListenable: downloadState.isDownloading,
builder: (context, isDownloading, child) {
return TextButton(
onPressed: isDownloading
? null
: () async {
downloadState.isDownloading.value =
false;
// Ensure the button does nothing while downloading
await OnDownload.initiateUpdate(
url: model.downloadUrl,
versionName: model.versionName,
config: config,
downloadState: downloadState,
sourceUrl: model.sourceUrl,
launchUrl: (url) => _launchURL(url),
);
},
child: isDownloading
? ValueListenableBuilder<double>(
valueListenable: downloadState.progress,
builder: (context, progress, _) {
int progressValue =
(progress * 100).toInt();
return AnimatedFlipCounter(
suffix: '%',
duration: const Duration(
milliseconds: 500),
value: progressValue,
textStyle: TextStyle(
color: Theme.of(context)
.colorScheme
.onBackground),
);
},
)
: const Text('Download'),
);
},
)
],
);
},
);
},
iosDialogBuilder: (BuildContext context,
IOSModel model,
UpdateCenterConfig config,
DownloadState downloadState,
bool allowSkip) {
return showDialog(
context: context,
barrierDismissible: allowSkip,
builder: (BuildContext context) {
return CupertinoAlertDialog(
title: Text(model.versionName),
content: Text(model.changeLog),
actions: <CupertinoDialogAction>[
CupertinoDialogAction(
isDestructiveAction: true,
child: const Text("Close"),
onPressed: () => Navigator.of(context).pop(),
),
CupertinoDialogAction(
onPressed: () => _launchURL(model.sourceUrl),
child: const Text("Open to url"),
),
],
);
});
},
windowsDialogBuilder: (BuildContext context,
WindowsModel model,
UpdateCenterConfig config,
DownloadState downloadState,
bool allowSkip) {
return showDialog(
context: context,
barrierDismissible: allowSkip,
builder: (BuildContext context) {
return AlertDialog(
title: Text(model.versionName),
content: Text(model.changeLog),
actions: [
if (allowSkip)
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text("Close"),
),
// Changes state to show loading percentage
ValueListenableBuilder<bool>(
valueListenable: downloadState.isDownloading,
builder: (context, isDownloading, child) {
return TextButton(
onPressed: isDownloading
? null
: () async {
// Disables the button so it doesn't cause new load calls when clicked
downloadState.isDownloading.value = false;
// Download call
await OnDownload.initiateUpdate(
url: model.downloadUrl,
versionName: model.versionName,
config: config,
downloadState: downloadState,
sourceUrl: model.sourceUrl,
launchUrl: (url) => _launchURL(url),
);
},
child: isDownloading
? ValueListenableBuilder<double>(
valueListenable: downloadState.progress,
builder: (context, progress, _) {
int progressValue =
(progress * 100).toInt();
return AnimatedFlipCounter(
suffix: '%',
duration: const Duration(
milliseconds: 500),
value: progressValue,
textStyle: TextStyle(
color: Theme.of(context)
.colorScheme
.onBackground),
);
},
)
: const Text('Download'),
);
},
)
],
);
},
);
}),
notificationConfig: NotificationConfig(
defaultIcon: '@drawable/ic_update_center', // Icon for the notification (Replace the path with another icon)
downloadProgressNotificationTextTitle: 'Downloading...', // Title header in notification
downloadProgressNotificationTextBody: '', // Body text for the download progress notification
downloadFailedNotificationTitleText: 'Download failed', // Title for the failed download notification
downloadFailedNotificationBodyText: 'An error occurred while downloading update. Check your internet connections and try again', // Body text for the failed download notification
showProgress: true, // Whether to show download progress in the notification
channelShowBadge: true, // Whether to show a badge on the notification channel
)));
}
//Check for updates
checkUpdate() async {
await updateCenter.check();
}
}