-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathfile_view.dart
More file actions
283 lines (241 loc) · 8.45 KB
/
file_view.dart
File metadata and controls
283 lines (241 loc) · 8.45 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'enum/view_status.dart';
import 'enum/x5_status.dart';
import 'file_view_localizations.dart';
import 'flutter_file_view.dart';
/// @Describe: The view of file.
///
/// @Author: LiWeNHuI
/// @Date: 2022/10/10
/// The view of file.
class FileView extends StatefulWidget {
// ignore: public_member_api_docs
const FileView({
Key? key,
required this.controller,
this.onCustomViewStatusBuilder,
this.onCustomX5StatusBuilder,
this.tipTextStyle,
this.buttonTextStyle,
}) : super(key: key);
/// The [FileViewController] responsible for the file being rendered in this
/// widget.
final FileViewController controller;
/// According to different states, display the corresponding layout.
final OnCustomViewStatusBuilder? onCustomViewStatusBuilder;
/// According to different states, display the corresponding layout.
final OnCustomX5StatusBuilder? onCustomX5StatusBuilder;
/// The style of the text for the prompt.
final TextStyle? tipTextStyle;
/// The style of the text for button.
final TextStyle? buttonTextStyle;
@override
State<FileView> createState() => _FileViewState();
}
class _FileViewState extends State<FileView> {
late FileViewLocalizations local = FileViewLocalizations.of(context);
@override
void initState() {
controller.initialize();
controller.addListener(_listener);
super.initState();
}
@override
void didUpdateWidget(covariant FileView oldWidget) {
super.didUpdateWidget(oldWidget);
oldWidget.controller.removeListener(_listener);
controller.addListener(_listener);
}
@override
void deactivate() {
super.deactivate();
controller.removeListener(_listener);
}
@override
void dispose() {
FlutterFileView.currentAndroidViewNumber = 0;
super.dispose();
}
void _listener() {
setState(() {});
}
@override
Widget build(BuildContext context) {
if (value.viewStatus == ViewStatus.DONE) {
return _buildDoneWidget();
}
Widget? child =
widget.onCustomViewStatusBuilder?.call(context, value.viewStatus);
if (value.viewStatus == ViewStatus.UNSUPPORTED_PLATFORM) {
child ??= _buildUnSupportPlatformWidget();
} else if (value.viewStatus == ViewStatus.NON_EXISTENT) {
child ??= _buildNonExistentWidget();
} else if (value.viewStatus == ViewStatus.UNSUPPORTED_FILETYPE) {
child ??= _buildUnSupportTypeWidget();
} else {
child ??= _buildPlaceholderWidget();
}
return child;
}
/// The layout to display when the platform is unsupported.
///
/// This prompt is required because it only supports Android and iOS,
/// and has not been adapted to desktop and web for the time being.
Widget _buildUnSupportPlatformWidget() {
return showTipWidget(local.unSupportedPlatformTip);
}
/// The layout to display when the file does not exist.
Widget _buildNonExistentWidget() {
return showTipWidget(local.nonExistentTip);
}
/// The layout to display when the file type is unsupported.
Widget _buildUnSupportTypeWidget() {
return showTipWidget(sprintf(local.unSupportedType, value.fileType ?? ''));
}
/// Widgets for presenting information
Widget showTipWidget(String tip) {
return Center(child: Text(tip, style: widget.tipTextStyle));
}
/// The layout to display when complete.
Widget _buildDoneWidget() {
if (isAndroid) {
return _createAndroidView();
} else if (isIOS) {
return Stack(
children: <Widget>[
UiKitView(
viewType: viewName,
creationParams: <String, String>{
'filePath': value.filePath ?? '',
'fileType': value.fileType ?? '',
},
creationParamsCodec: const StandardMessageCodec(),
),
if ((value.progressForIOS ?? 0) < 100) _buildPlaceholderWidget(),
],
);
}
return _buildUnSupportPlatformWidget();
}
Widget _createAndroidView() {
if (value.x5status == X5Status.DONE) {
final AndroidViewConfig config =
controller.androidViewConfig ?? AndroidViewConfig();
return AndroidView(
viewType: viewName,
creationParams: <String, dynamic>{
'filePath': value.filePath,
'fileType': value.fileType,
'is_bar_show': config.isBarShow,
'into_downloading': config.intoDownloading,
'is_bar_animating': config.isBarAnimating,
},
onPlatformViewCreated: (int id) {
MethodChannel('${channelName}_$id').invokeMethod<void>(
'openFile',
FlutterFileView.currentAndroidViewNumber++ == 0,
);
},
creationParamsCodec: const StandardMessageCodec(),
);
}
Widget? child =
widget.onCustomX5StatusBuilder?.call(context, value.x5status);
if (value.x5status == X5Status.ERROR) {
child ??= showX5RetryWidget(local.engineFail);
} else if (value.x5status == X5Status.DOWNLOAD_SUCCESS) {
child ??= showX5TipWidget(local.engineDownloadSuccess);
} else if (value.x5status == X5Status.DOWNLOAD_FAIL) {
child ??= showX5RetryWidget(local.engineDownloadFail);
} else if (value.x5status == X5Status.DOWNLOADING) {
child ??= showX5TipWidget(local.engineDownloading);
} else if (value.x5status == X5Status.DOWNLOAD_NON_REQUIRED) {
child ??= showTipWidget(local.engineDownloadNonRequired);
} else if (value.x5status == X5Status.DOWNLOAD_CANCEL_NOT_WIFI) {
child ??= showX5RetryWidget(local.engineDownloadCancelNotWifi);
} else if (value.x5status == X5Status.DOWNLOAD_OUT_OF_ONE) {
child ??= showTipWidget(local.engineDownloadOutOfOne);
} else if (value.x5status == X5Status.DOWNLOAD_CANCEL_REQUESTING) {
child ??= showX5TipWidget(local.engineDownloadCancelRequesting);
} else if (value.x5status == X5Status.DOWNLOAD_NO_NEED_REQUEST) {
child ??= showX5RetryWidget(local.engineDownloadNoNeedRequest);
} else if (value.x5status == X5Status.DOWNLOAD_FLOW_CANCEL) {
child ??= showX5TipWidget(local.engineDownloadFlowCancel);
} else if (value.x5status == X5Status.INSTALL_SUCCESS) {
child ??= showX5TipWidget(local.engineInstallSuccess);
} else if (value.x5status == X5Status.INSTALL_FAIL) {
child ??= showX5RetryWidget(local.engineInstallFail);
} else {
child ??= showX5TipWidget(local.engineLoading);
}
return child;
}
/// The layout to display when loading.
Widget _buildPlaceholderWidget() {
return Center(
child: CircularProgressIndicator(
key: ValueKey<String>('FileView_${hashCode}_Placeholder'),
value: value.progressValue,
),
);
}
/// Widgets for presenting information of x5Status.
Widget showX5TipWidget(String tip) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
CircularProgressIndicator(
key: ValueKey<String>('FileView_${hashCode}_X5_Placeholder'),
value: value.progressValue,
color: Theme.of(context).primaryColor,
backgroundColor: value.progressValue != null
? Theme.of(context).primaryColorLight
: null,
),
const SizedBox(height: 20),
Text(tip, style: widget.tipTextStyle),
],
),
);
}
Widget showX5RetryWidget(String tip) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(tip, style: widget.tipTextStyle),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () {
FlutterFileView.init();
controller.initializeForAndroid();
},
child: Text(local.retry, style: widget.buttonTextStyle),
),
],
),
);
}
/// A replacement operation for [stringTf].
String sprintf(String stringTf, String msg) {
return stringTf.replaceAll('%s', msg);
}
FileViewController get controller => widget.controller;
FileViewValue get value => controller.value;
}
/// According to [status], display different layouts.
///
/// In state [ViewStatus.DONE], the layout cannot be customized.
typedef OnCustomViewStatusBuilder = Widget? Function(
BuildContext context,
ViewStatus status,
);
/// According to [status], display different layouts.
///
/// In state [X5Status.DONE], the layout cannot be customized.
typedef OnCustomX5StatusBuilder = Widget? Function(
BuildContext context,
X5Status status,
);