質問編集履歴

1

間違えたコードをのせていたため、修正いたしました

2021/03/28 12:53

投稿

sugoroku
sugoroku

スコア5

test CHANGED
File without changes
test CHANGED
@@ -20,6 +20,8 @@
20
20
 
21
21
  ```dart
22
22
 
23
+
24
+
23
25
  import 'dart:io';
24
26
 
25
27
  import 'package:camera/camera.dart';
@@ -36,470 +38,472 @@
36
38
 
37
39
 
38
40
 
39
- String result;
41
+ String result;
40
-
42
+
41
- String imagePath;
43
+ String imagePath;
42
-
44
+
43
- String image;
45
+ String image;
44
-
46
+
45
- bool _isBusy = false;
47
+ bool _isBusy = false;
46
-
48
+
47
- OpenFileDialogType _dialogType = OpenFileDialogType.image;
49
+ OpenFileDialogType _dialogType = OpenFileDialogType.image;
48
-
49
-
50
-
50
+
51
+
52
+
51
- SourceType _sourceType = SourceType.photoLibrary;
53
+ SourceType _sourceType = SourceType.photoLibrary;
52
-
54
+
53
- bool _allowEditing = false;
55
+ bool _allowEditing = false;
54
-
56
+
55
- File _currentFile;
57
+ File _currentFile;
56
-
58
+
57
- String _savedFilePath;
59
+ String _savedFilePath;
58
-
60
+
59
- bool _localOnly = false;
61
+ bool _localOnly = false;
60
-
62
+
61
- int selectedCameraIndex;
63
+ int selectedCameraIndex;
62
-
64
+
63
- List cameras;
65
+ List cameras;///````
66
+
67
+ ///////`````↓
64
68
 
65
69
  Future initCamera(CameraDescription cameraDescription) async {
66
70
 
67
71
  CameraController cameraController;
68
72
 
69
-
70
-
71
- if (cameraController != null) {
73
+ if (cameraController != null) {
72
-
74
+
73
- await cameraController.dispose();
75
+ await cameraController.dispose();
76
+
77
+ }
78
+
79
+ }
80
+
81
+ ///////``````↑
82
+
83
+ class TakePictureScreen extends StatefulWidget {
84
+
85
+
86
+
87
+ final CameraDescription camera;
88
+
89
+
90
+
91
+ const TakePictureScreen({
92
+
93
+ Key key,
94
+
95
+ @required this.camera,
96
+
97
+ }) : super(key: key);
98
+
99
+
100
+
101
+ @override
102
+
103
+ TakePictureScreenState createState() => TakePictureScreenState();
104
+
105
+ }
106
+
107
+
108
+
109
+ class TakePictureScreenState extends State<TakePictureScreen> {
110
+
111
+ CameraController _controller;
112
+
113
+ Future<void> _initializeControllerFuture;
114
+
115
+
116
+
117
+ @override
118
+
119
+ void initState() {
120
+
121
+ super.initState();
122
+
123
+
124
+
125
+ _controller = CameraController(
126
+
127
+
128
+
129
+ widget.camera,
130
+
131
+
132
+
133
+ ResolutionPreset.medium,
134
+
135
+ );
136
+
137
+ availableCameras().then((value) {////````↓
138
+
139
+ cameras = value;
140
+
141
+ if(cameras.length > 0){
142
+
143
+ setState(() {
144
+
145
+ selectedCameraIndex = 0;
146
+
147
+ });
148
+
149
+ initCamera(cameras[selectedCameraIndex]).then((value) {
150
+
151
+
152
+
153
+ });
154
+
155
+ }
156
+
157
+ });////````↑
158
+
159
+
160
+
161
+ _initializeControllerFuture = _controller.initialize();
74
162
 
75
163
  }
76
164
 
165
+
166
+
167
+ @override
168
+
169
+ void dispose() {
170
+
171
+ // Dispose of the controller when the widget is disposed.
172
+
173
+ _controller.dispose();
174
+
175
+ super.dispose();
176
+
177
+ }
178
+
179
+
180
+
181
+ Widget _buildControlBar() {
182
+
183
+ return Row(
184
+
185
+ mainAxisAlignment: MainAxisAlignment.spaceAround,
186
+
187
+ children: <Widget>[
188
+
189
+ IconButton(
190
+
191
+ color: Colors.white,
192
+
193
+ icon: Icon(Icons.flash_auto),
194
+
195
+ onPressed: () {},
196
+
197
+ ),
198
+
199
+ FloatingActionButton (
200
+
201
+ child: Icon(Icons.camera_alt),
202
+
203
+ // Provide an onPressed callback.
204
+
205
+ onPressed: () async {
206
+
207
+ // Take the Picture in a try / catch block. If anything goes wrong,
208
+
209
+ // catch the error.
210
+
211
+ try {
212
+
213
+ // Ensure that the camera is initialized.
214
+
215
+ await _initializeControllerFuture;
216
+
217
+
218
+
219
+ // Attempt to take a picture and get the file `image`
220
+
221
+ // where it was saved.
222
+
223
+ final image = await _controller.takePicture();
224
+
225
+
226
+
227
+ // If the picture was taken, display it on a new screen.
228
+
229
+ Navigator.push(
230
+
231
+ context,
232
+
233
+ MaterialPageRoute(
234
+
235
+ builder: (context) => DisplayPictureScreen(
236
+
237
+
238
+
239
+ imagePath: image?.path,
240
+
241
+
242
+
243
+ ),
244
+
245
+ ),
246
+
247
+ );
248
+
249
+ } catch (e) {
250
+
251
+ // If an error occurs, log the error to the console.
252
+
253
+ print(e);
254
+
255
+ }
256
+
257
+ },
258
+
259
+ ),
260
+
261
+ IconButton(
262
+
263
+ color: Colors.white,
264
+
265
+ icon: Icon(Icons.switch_camera),
266
+
267
+ onPressed:(){onSwitchCamera();}
268
+
269
+ ),
270
+
271
+ ],
272
+
273
+ );
274
+
275
+ }
276
+
277
+
278
+
279
+
280
+
281
+ onSwitchCamera() {/////```````↓
282
+
283
+ selectedCameraIndex =
284
+
285
+ selectedCameraIndex < cameras.length - 1 ? selectedCameraIndex + 1 : 0;
286
+
287
+ CameraDescription selectedCamera = cameras[selectedCameraIndex];
288
+
289
+ initCamera(selectedCamera);
290
+
291
+ }///////```````↑
292
+
293
+ @override
294
+
295
+ Widget build(BuildContext context) {
296
+
297
+ return Scaffold(
298
+
299
+ appBar: AppBar(title: Text('Take a picture')),
300
+
301
+ // Wait until the controller is initialized before displaying the
302
+
303
+ // camera preview. Use a FutureBuilder to display a loading spinner
304
+
305
+ // until the controller has finished initializing.
306
+
307
+ body: Stack(
308
+
309
+ children: <Widget>[
310
+
311
+ FutureBuilder<void>(
312
+
313
+ future: _initializeControllerFuture,
314
+
315
+ builder: (context, snapshot) {
316
+
317
+ if (snapshot.connectionState == ConnectionState.done) {
318
+
319
+ return CameraPreview(_controller);
320
+
321
+ } else {
322
+
323
+ return Center(child: CircularProgressIndicator());
324
+
325
+ }
326
+
327
+ },
328
+
329
+ ),
330
+
331
+ Column(
332
+
333
+ mainAxisAlignment: MainAxisAlignment.end,
334
+
335
+ children: <Widget>[
336
+
337
+
338
+
339
+ _buildControlBar(),
340
+
341
+ Container(
342
+
343
+ padding: EdgeInsets.symmetric(vertical: 10.0),
344
+
345
+ child: Text(
346
+
347
+ 'Tap for photo',
348
+
349
+ style: Theme.of(context)
350
+
351
+ .textTheme
352
+
353
+ .subtitle1
354
+
355
+ .copyWith(color: Colors.white),
356
+
357
+ ),
358
+
359
+ )
360
+
361
+ ],
362
+
363
+ ),
364
+
365
+ ],
366
+
367
+ ),
368
+
369
+
370
+
371
+ );
372
+
373
+ }
374
+
375
+
376
+
377
+
378
+
77
379
  }
78
380
 
381
+
382
+
383
+ // A widget that displays the picture taken by the user.
384
+
79
- class TakePictureScreen extends StatefulWidget {
385
+ class DisplayPictureScreen extends StatefulWidget {
80
-
81
-
82
-
386
+
83
- final CameraDescription camera;
387
+ final String imagePath;
84
-
85
-
86
-
388
+
87
- const TakePictureScreen({
389
+ DisplayPictureScreen({Key key, this.imagePath}) ;
88
-
89
- Key key,
90
-
91
- @required this.camera,
92
-
93
- }) : super(key: key);
94
390
 
95
391
 
96
392
 
97
393
  @override
98
394
 
99
- TakePictureScreenState createState() => TakePictureScreenState();
395
+ _DisplayPictureScreenState createState() => _DisplayPictureScreenState(imagePath:imagePath);
396
+
397
+
100
398
 
101
399
  }
102
400
 
103
401
 
104
402
 
105
- class TakePictureScreenState extends State<TakePictureScreen> {
403
+ class _DisplayPictureScreenState extends State<DisplayPictureScreen>{
106
-
404
+
107
- CameraController _controller;
405
+ final String imagePath;
108
-
406
+
109
- Future<void> _initializeControllerFuture;
407
+ _DisplayPictureScreenState({this.imagePath});
110
-
111
-
112
408
 
113
409
  @override
114
410
 
411
+ Widget build(BuildContext context) {
412
+
115
- void initState() {
413
+ return Scaffold(
414
+
116
-
415
+ appBar: AppBar(title: Text('Display the Picture')),
416
+
417
+ // The image is stored as a file on the device. Use the `Image.file`
418
+
419
+ // constructor with the given path to display the image.
420
+
117
- super.initState();
421
+ body: Image.file(File(imagePath)),
118
-
119
-
120
-
422
+
423
+
424
+
425
+
426
+
121
- _controller = CameraController(
427
+ floatingActionButton:FloatingActionButton(
122
-
123
-
124
-
428
+
125
- widget.camera,
429
+ onPressed:imagePath==null?null :()=> _saveFile(false),
126
-
127
-
128
-
430
+
129
- ResolutionPreset.medium,
431
+ child:Text('save'),
432
+
433
+
434
+
435
+
436
+
437
+ ),
438
+
439
+
130
440
 
131
441
  );
132
442
 
133
- availableCameras().then((value) {
134
-
135
- cameras = value;
136
-
137
- if(cameras.length > 0){
138
-
139
- setState(() {
140
-
141
- selectedCameraIndex = 0;
142
-
143
- });
144
-
145
- initCamera(cameras[selectedCameraIndex]).then((value) {
146
-
147
-
148
-
149
- });
150
-
151
- }
152
-
153
- });
154
-
155
-
156
-
157
- _initializeControllerFuture = _controller.initialize();
158
-
159
443
  }
160
444
 
161
-
445
+ Future<void> _saveFile(bool useData) async {
162
-
446
+
447
+
448
+
163
- @override
449
+ try {
164
-
450
+
165
- void dispose() {
451
+ setState(() {
452
+
166
-
453
+ _isBusy = true;
454
+
455
+ if(imagePath!=null){
456
+
167
- // Dispose of the controller when the widget is disposed.
457
+ _currentFile = File(imagePath);
458
+
168
-
459
+ }else{
460
+
169
- _controller.dispose();
461
+ _currentFile=null;
462
+
170
-
463
+ }
464
+
465
+
466
+
467
+ });
468
+
469
+ final data = useData ? await _currentFile.readAsBytes() : null;
470
+
471
+ final params = SaveFileDialogParams(
472
+
473
+ sourceFilePath: useData ? null : _currentFile.path,
474
+
475
+ data: data,
476
+
477
+ localOnly: _localOnly,
478
+
479
+ fileName: useData ? "untitled" : null);
480
+
481
+ result= await FlutterFileDialog.saveFile(params: params);
482
+
171
- super.dispose();
483
+ print(result);
484
+
485
+ } on PlatformException catch (e) {
486
+
487
+ print(e);
488
+
489
+ } finally {
490
+
491
+ setState(() {
492
+
493
+ _savedFilePath = result ?? _savedFilePath;
494
+
495
+ _isBusy = false;
496
+
497
+ });
498
+
499
+ }
172
500
 
173
501
  }
174
502
 
175
-
176
-
177
- Widget _buildControlBar() {
178
-
179
- return Row(
180
-
181
- mainAxisAlignment: MainAxisAlignment.spaceAround,
182
-
183
- children: <Widget>[
184
-
185
- IconButton(
186
-
187
- color: Colors.white,
188
-
189
- icon: Icon(Icons.flash_auto),
190
-
191
- onPressed: () {},
192
-
193
- ),
194
-
195
- FloatingActionButton (
196
-
197
- child: Icon(Icons.camera_alt),
198
-
199
- // Provide an onPressed callback.
200
-
201
- onPressed: () async {
202
-
203
- // Take the Picture in a try / catch block. If anything goes wrong,
204
-
205
- // catch the error.
206
-
207
- try {
208
-
209
- // Ensure that the camera is initialized.
210
-
211
- await _initializeControllerFuture;
212
-
213
-
214
-
215
- // Attempt to take a picture and get the file `image`
216
-
217
- // where it was saved.
218
-
219
- final image = await _controller.takePicture();
220
-
221
-
222
-
223
- // If the picture was taken, display it on a new screen.
224
-
225
- Navigator.push(
226
-
227
- context,
228
-
229
- MaterialPageRoute(
230
-
231
- builder: (context) => DisplayPictureScreen(
232
-
233
-
234
-
235
- imagePath: image?.path,
236
-
237
-
238
-
239
- ),
240
-
241
- ),
242
-
243
- );
244
-
245
- } catch (e) {
246
-
247
- // If an error occurs, log the error to the console.
248
-
249
- print(e);
250
-
251
- }
252
-
253
- },
254
-
255
- ),
256
-
257
- IconButton(
258
-
259
- color: Colors.white,
260
-
261
- icon: Icon(Icons.switch_camera),
262
-
263
- onPressed: onSwitchCamera,
264
-
265
- ),
266
-
267
- ],
268
-
269
- );
270
-
271
- }
272
-
273
-
274
-
275
-
276
-
277
- void onSwitchCamera() {
278
-
279
- selectedCameraIndex =
280
-
281
- selectedCameraIndex < cameras.length - 1 ? selectedCameraIndex + 1 : 0;
282
-
283
- CameraDescription selectedCamera = cameras[selectedCameraIndex];
284
-
285
- initCamera(selectedCamera);
286
-
287
- }
288
-
289
- @override
290
-
291
- Widget build(BuildContext context) {
292
-
293
- return Scaffold(
294
-
295
- appBar: AppBar(title: Text('Take a picture')),
296
-
297
- // Wait until the controller is initialized before displaying the
298
-
299
- // camera preview. Use a FutureBuilder to display a loading spinner
300
-
301
- // until the controller has finished initializing.
302
-
303
- body: Stack(
304
-
305
- children: <Widget>[
306
-
307
- FutureBuilder<void>(
308
-
309
- future: _initializeControllerFuture,
310
-
311
- builder: (context, snapshot) {
312
-
313
- if (snapshot.connectionState == ConnectionState.done) {
314
-
315
- return CameraPreview(_controller);
316
-
317
- } else {
318
-
319
- return Center(child: CircularProgressIndicator());
320
-
321
- }
322
-
323
- },
324
-
325
- ),
326
-
327
- Column(
328
-
329
- mainAxisAlignment: MainAxisAlignment.end,
330
-
331
- children: <Widget>[
332
-
333
-
334
-
335
- _buildControlBar(),
336
-
337
- Container(
338
-
339
- padding: EdgeInsets.symmetric(vertical: 10.0),
340
-
341
- child: Text(
342
-
343
- 'Tap for photo',
344
-
345
- style: Theme.of(context)
346
-
347
- .textTheme
348
-
349
- .subtitle1
350
-
351
- .copyWith(color: Colors.white),
352
-
353
- ),
354
-
355
- )
356
-
357
- ],
358
-
359
- ),
360
-
361
- ],
362
-
363
- ),
364
-
365
-
366
-
367
- );
368
-
369
- }
370
-
371
-
372
-
373
-
374
-
375
503
  }
376
504
 
377
505
 
378
506
 
379
- // A widget that displays the picture taken by the user.
380
-
381
- class DisplayPictureScreen extends StatefulWidget {
382
-
383
- final String imagePath;
384
-
385
- DisplayPictureScreen({Key key, this.imagePath}) ;
386
-
387
-
388
-
389
- @override
390
-
391
- _DisplayPictureScreenState createState() => _DisplayPictureScreenState(imagePath:imagePath);
392
-
393
-
394
-
395
- }
396
-
397
-
398
-
399
- class _DisplayPictureScreenState extends State<DisplayPictureScreen>{
400
-
401
- final String imagePath;
402
-
403
- _DisplayPictureScreenState({this.imagePath});
404
-
405
- @override
406
-
407
- Widget build(BuildContext context) {
408
-
409
- return Scaffold(
410
-
411
- appBar: AppBar(title: Text('Display the Picture')),
412
-
413
- // The image is stored as a file on the device. Use the `Image.file`
414
-
415
- // constructor with the given path to display the image.
416
-
417
- body: Image.file(File(imagePath)),
418
-
419
-
420
-
421
-
422
-
423
- floatingActionButton:FloatingActionButton(
424
-
425
- onPressed:imagePath==null?null :()=> _saveFile(false),
426
-
427
- child:Text('save'),
428
-
429
-
430
-
431
-
432
-
433
- ),
434
-
435
-
436
-
437
- );
438
-
439
- }
440
-
441
- Future<void> _saveFile(bool useData) async {
442
-
443
-
444
-
445
- try {
446
-
447
- setState(() {
448
-
449
- _isBusy = true;
450
-
451
- if(imagePath!=null){
452
-
453
- _currentFile = File(imagePath);
454
-
455
- }else{
456
-
457
- _currentFile=null;
458
-
459
- }
460
-
461
-
462
-
463
- });
464
-
465
- final data = useData ? await _currentFile.readAsBytes() : null;
466
-
467
- final params = SaveFileDialogParams(
468
-
469
- sourceFilePath: useData ? null : _currentFile.path,
470
-
471
- data: data,
472
-
473
- localOnly: _localOnly,
474
-
475
- fileName: useData ? "untitled" : null);
476
-
477
- result= await FlutterFileDialog.saveFile(params: params);
478
-
479
- print(result);
480
-
481
- } on PlatformException catch (e) {
482
-
483
- print(e);
484
-
485
- } finally {
486
-
487
- setState(() {
488
-
489
- _savedFilePath = result ?? _savedFilePath;
490
-
491
- _isBusy = false;
492
-
493
- });
494
-
495
- }
496
-
497
- }
498
-
499
- }
500
-
501
-
502
-
503
507
 
504
508
 
505
509
  ```