@@ -29,60 +29,63 @@ class MyApp extends StatelessWidget {
29
29
}
30
30
31
31
class MyHomePage extends StatefulWidget {
32
- MyHomePage ({Key key, this .title}) : super (key: key);
32
+ MyHomePage ({Key ? key, this .title}) : super (key: key);
33
33
34
- final String title;
34
+ final String ? title;
35
35
36
36
@override
37
37
_MyHomePageState createState () => _MyHomePageState ();
38
38
}
39
39
40
40
class _MyHomePageState extends State <MyHomePage > {
41
- PickedFile _imageFile;
41
+ PickedFile ? _imageFile;
42
42
dynamic _pickImageError;
43
43
bool isVideo = false ;
44
- VideoPlayerController _controller;
45
- VideoPlayerController _toBeDisposed;
46
- String _retrieveDataError;
44
+ VideoPlayerController ? _controller;
45
+ VideoPlayerController ? _toBeDisposed;
46
+ String ? _retrieveDataError;
47
47
48
48
final ImagePicker _picker = ImagePicker ();
49
49
final TextEditingController maxWidthController = TextEditingController ();
50
50
final TextEditingController maxHeightController = TextEditingController ();
51
51
final TextEditingController qualityController = TextEditingController ();
52
52
53
- Future <void > _playVideo (PickedFile file) async {
53
+ Future <void > _playVideo (PickedFile ? file) async {
54
54
if (file != null && mounted) {
55
55
await _disposeVideoController ();
56
+ late VideoPlayerController controller;
56
57
if (kIsWeb) {
57
- _controller = VideoPlayerController .network (file.path);
58
- // In web, most browsers won't honor a programmatic call to .play
59
- // if the video has a sound track (and is not muted).
60
- // Mute the video so it auto-plays in web!
61
- // This is not needed if the call to .play is the result of user
62
- // interaction (clicking on a "play" button, for example).
63
- await _controller.setVolume (0.0 );
58
+ controller = VideoPlayerController .network (file.path);
64
59
} else {
65
- _controller = VideoPlayerController .file (File (file.path));
66
- await _controller.setVolume (1.0 );
60
+ controller = VideoPlayerController .file (File (file.path));
67
61
}
68
- await _controller.initialize ();
69
- await _controller.setLooping (true );
70
- await _controller.play ();
62
+ _controller = controller;
63
+ // In web, most browsers won't honor a programmatic call to .play
64
+ // if the video has a sound track (and is not muted).
65
+ // Mute the video so it auto-plays in web!
66
+ // This is not needed if the call to .play is the result of user
67
+ // interaction (clicking on a "play" button, for example).
68
+ final double volume = kIsWeb ? 0.0 : 1.0 ;
69
+ await controller.setVolume (volume);
70
+ await controller.initialize ();
71
+ await controller.setLooping (true );
72
+ await controller.play ();
71
73
setState (() {});
72
74
}
73
75
}
74
76
75
- void _onImageButtonPressed (ImageSource source, {BuildContext context}) async {
77
+ void _onImageButtonPressed (ImageSource source,
78
+ {BuildContext ? context}) async {
76
79
if (_controller != null ) {
77
- await _controller.setVolume (0.0 );
80
+ await _controller! .setVolume (0.0 );
78
81
}
79
82
if (isVideo) {
80
- final PickedFile file = await _picker.getVideo (
83
+ final PickedFile ? file = await _picker.getVideo (
81
84
source: source, maxDuration: const Duration (seconds: 10 ));
82
85
await _playVideo (file);
83
86
} else {
84
- await _displayPickImageDialog (context,
85
- (double maxWidth, double maxHeight, int quality) async {
87
+ await _displayPickImageDialog (context! ,
88
+ (double ? maxWidth, double ? maxHeight, int ? quality) async {
86
89
try {
87
90
final pickedFile = await _picker.getImage (
88
91
source: source,
@@ -105,8 +108,8 @@ class _MyHomePageState extends State<MyHomePage> {
105
108
@override
106
109
void deactivate () {
107
110
if (_controller != null ) {
108
- _controller.setVolume (0.0 );
109
- _controller.pause ();
111
+ _controller! .setVolume (0.0 );
112
+ _controller! .pause ();
110
113
}
111
114
super .deactivate ();
112
115
}
@@ -122,14 +125,14 @@ class _MyHomePageState extends State<MyHomePage> {
122
125
123
126
Future <void > _disposeVideoController () async {
124
127
if (_toBeDisposed != null ) {
125
- await _toBeDisposed.dispose ();
128
+ await _toBeDisposed! .dispose ();
126
129
}
127
130
_toBeDisposed = _controller;
128
131
_controller = null ;
129
132
}
130
133
131
134
Widget _previewVideo () {
132
- final Text retrieveError = _getRetrieveErrorWidget ();
135
+ final Text ? retrieveError = _getRetrieveErrorWidget ();
133
136
if (retrieveError != null ) {
134
137
return retrieveError;
135
138
}
@@ -146,18 +149,18 @@ class _MyHomePageState extends State<MyHomePage> {
146
149
}
147
150
148
151
Widget _previewImage () {
149
- final Text retrieveError = _getRetrieveErrorWidget ();
152
+ final Text ? retrieveError = _getRetrieveErrorWidget ();
150
153
if (retrieveError != null ) {
151
154
return retrieveError;
152
155
}
153
156
if (_imageFile != null ) {
154
157
if (kIsWeb) {
155
158
// Why network?
156
159
// See https://pub.dev/packages/image_picker#getting-ready-for-the-web-platform
157
- return Image .network (_imageFile.path);
160
+ return Image .network (_imageFile! .path);
158
161
} else {
159
162
return Semantics (
160
- child: Image .file (File (_imageFile.path)),
163
+ child: Image .file (File (_imageFile! .path)),
161
164
label: 'image_picker_example_picked_image' );
162
165
}
163
166
} else if (_pickImageError != null ) {
@@ -189,15 +192,15 @@ class _MyHomePageState extends State<MyHomePage> {
189
192
});
190
193
}
191
194
} else {
192
- _retrieveDataError = response.exception.code;
195
+ _retrieveDataError = response.exception! .code;
193
196
}
194
197
}
195
198
196
199
@override
197
200
Widget build (BuildContext context) {
198
201
return Scaffold (
199
202
appBar: AppBar (
200
- title: Text (widget.title),
203
+ title: Text (widget.title! ),
201
204
),
202
205
body: Center (
203
206
child: ! kIsWeb && defaultTargetPlatform == TargetPlatform .android
@@ -288,9 +291,9 @@ class _MyHomePageState extends State<MyHomePage> {
288
291
);
289
292
}
290
293
291
- Text _getRetrieveErrorWidget () {
294
+ Text ? _getRetrieveErrorWidget () {
292
295
if (_retrieveDataError != null ) {
293
- final Text result = Text (_retrieveDataError);
296
+ final Text result = Text (_retrieveDataError! );
294
297
_retrieveDataError = null ;
295
298
return result;
296
299
}
@@ -336,13 +339,13 @@ class _MyHomePageState extends State<MyHomePage> {
336
339
TextButton (
337
340
child: const Text ('PICK' ),
338
341
onPressed: () {
339
- double width = maxWidthController.text.isNotEmpty
342
+ double ? width = maxWidthController.text.isNotEmpty
340
343
? double .parse (maxWidthController.text)
341
344
: null ;
342
- double height = maxHeightController.text.isNotEmpty
345
+ double ? height = maxHeightController.text.isNotEmpty
343
346
? double .parse (maxHeightController.text)
344
347
: null ;
345
- int quality = qualityController.text.isNotEmpty
348
+ int ? quality = qualityController.text.isNotEmpty
346
349
? int .parse (qualityController.text)
347
350
: null ;
348
351
onPick (width, height, quality);
@@ -355,40 +358,40 @@ class _MyHomePageState extends State<MyHomePage> {
355
358
}
356
359
357
360
typedef void OnPickImageCallback (
358
- double maxWidth, double maxHeight, int quality);
361
+ double ? maxWidth, double ? maxHeight, int ? quality);
359
362
360
363
class AspectRatioVideo extends StatefulWidget {
361
364
AspectRatioVideo (this .controller);
362
365
363
- final VideoPlayerController controller;
366
+ final VideoPlayerController ? controller;
364
367
365
368
@override
366
369
AspectRatioVideoState createState () => AspectRatioVideoState ();
367
370
}
368
371
369
372
class AspectRatioVideoState extends State <AspectRatioVideo > {
370
- VideoPlayerController get controller => widget.controller;
373
+ VideoPlayerController ? get controller => widget.controller;
371
374
bool initialized = false ;
372
375
373
376
void _onVideoControllerUpdate () {
374
377
if (! mounted) {
375
378
return ;
376
379
}
377
- if (initialized != controller.value.initialized ) {
378
- initialized = controller.value.initialized ;
380
+ if (initialized != controller! .value.isInitialized ) {
381
+ initialized = controller! .value.isInitialized ;
379
382
setState (() {});
380
383
}
381
384
}
382
385
383
386
@override
384
387
void initState () {
385
388
super .initState ();
386
- controller.addListener (_onVideoControllerUpdate);
389
+ controller! .addListener (_onVideoControllerUpdate);
387
390
}
388
391
389
392
@override
390
393
void dispose () {
391
- controller.removeListener (_onVideoControllerUpdate);
394
+ controller! .removeListener (_onVideoControllerUpdate);
392
395
super .dispose ();
393
396
}
394
397
@@ -397,8 +400,8 @@ class AspectRatioVideoState extends State<AspectRatioVideo> {
397
400
if (initialized) {
398
401
return Center (
399
402
child: AspectRatio (
400
- aspectRatio: controller.value? .aspectRatio,
401
- child: VideoPlayer (controller),
403
+ aspectRatio: controller! .value.aspectRatio,
404
+ child: VideoPlayer (controller! ),
402
405
),
403
406
);
404
407
} else {
0 commit comments