前提・実現したいこと
Flutter初心者です。
カメラアプリを開発中、カメラのインアウトを切り替えようにしたいのですがエラーが発生してしまいます。
どなたかご回答お願いしたいです。
発生している問題・エラーメッセージ
エラーメッセージは表示されないのですが、onpressedでonswitchcameraが機能しないです。
該当ウィジェット
onSwitchCamera() { selectedCameraIndex = selectedCameraIndex < cameras.length - 1 ? selectedCameraIndex + 1 : 0; CameraDescription selectedCamera = cameras[selectedCameraIndex]; initCamera(selectedCamera); }
該当のソースコード
dart
1import 'dart:io'; 2import 'package:camera/camera.dart'; 3import 'package:flutter/cupertino.dart'; 4import 'package:flutter/material.dart'; 5import 'dart:async'; 6import 'package:flutter_file_dialog/flutter_file_dialog.dart'; 7import 'package:flutter/services.dart'; 8 9 String result; 10 String imagePath; 11 String image; 12 bool _isBusy = false; 13 OpenFileDialogType _dialogType = OpenFileDialogType.image; 14 15 SourceType _sourceType = SourceType.photoLibrary; 16 bool _allowEditing = false; 17 File _currentFile; 18 String _savedFilePath; 19 bool _localOnly = false; 20 int selectedCameraIndex; 21 List cameras; 22 23Future initCamera(CameraDescription cameraDescription) async { 24 CameraController cameraController; 25 if (cameraController != null) { 26 await cameraController.dispose(); 27 } 28} 29 30class TakePictureScreen extends StatefulWidget { 31 32 final CameraDescription camera; 33 34 const TakePictureScreen({ 35 Key key, 36 this.camera, 37 }) : super(key: key); 38 39 40 TakePictureScreenState createState() => TakePictureScreenState(); 41} 42 43class TakePictureScreenState extends State<TakePictureScreen> { 44 CameraController _controller; 45 Future<void> _initializeControllerFuture; 46 47 48 void initState() { 49 super.initState(); 50 51 _controller = CameraController( 52 widget.camera, 53 ResolutionPreset.medium, 54 ); 55 availableCameras().then((value) { 56 cameras = value; 57 if(cameras.length > 0){ 58 setState(() { 59 selectedCameraIndex = 0; 60 }); 61 initCamera(cameras[selectedCameraIndex]).then((value) { 62 }); 63 } 64 }); 65 _initializeControllerFuture = _controller.initialize(); 66 } 67 68 69 void dispose() { 70 // Dispose of the controller when the widget is disposed. 71 _controller.dispose(); 72 super.dispose(); 73 } 74 75Widget _buildControlBar() { 76 return Row( 77 mainAxisAlignment: MainAxisAlignment.spaceAround, 78 children: <Widget>[ 79 IconButton( 80 color: Colors.white, 81 icon: Icon(Icons.flash_auto), 82 onPressed: () {}, 83 ), 84 FloatingActionButton ( 85 child: Icon(Icons.camera_alt), 86 // Provide an onPressed callback. 87 onPressed: () async { 88 // Take the Picture in a try / catch block. If anything goes wrong, 89 // catch the error. 90 try { 91 // Ensure that the camera is initialized. 92 await _initializeControllerFuture; 93 94 // Attempt to take a picture and get the file `image` 95 // where it was saved. 96 final image = await _controller.takePicture(); 97 98 // If the picture was taken, display it on a new screen. 99 Navigator.push( 100 context, 101 MaterialPageRoute( 102 builder: (context) => DisplayPictureScreen( 103 imagePath: image?.path, 104 ), 105 ), 106 ); 107 } catch (e) { 108 // If an error occurs, log the error to the console. 109 print(e); 110 } 111 }, 112 ), 113 IconButton( 114 color: Colors.white, 115 icon: Icon(Icons.switch_camera), 116 onPressed:(){onSwitchCamera();} 117 ), 118 ], 119 ); 120 } 121 122 123 onSwitchCamera() { 124 selectedCameraIndex = 125 selectedCameraIndex < cameras.length - 1 ? selectedCameraIndex + 1 : 0; 126 CameraDescription selectedCamera = cameras[selectedCameraIndex]; 127 initCamera(selectedCamera); 128 } 129 130 131 132 Widget build(BuildContext context) { 133 return Scaffold( 134 appBar: AppBar(title: Text('Take a picture')), 135 // Wait until the controller is initialized before displaying the 136 // camera preview. Use a FutureBuilder to display a loading spinner 137 // until the controller has finished initializing. 138 body: Stack( 139 children: <Widget>[ 140 FutureBuilder<void>( 141 future: _initializeControllerFuture, 142 builder: (context, snapshot) { 143 if (snapshot.connectionState == ConnectionState.done) { 144 return CameraPreview(_controller); 145 } else { 146 return Center(child: CircularProgressIndicator()); 147 } 148 }, 149 ), 150 Column( 151 mainAxisAlignment: MainAxisAlignment.end, 152 children: <Widget>[ 153 154 _buildControlBar(), 155 Container( 156 padding: EdgeInsets.symmetric(vertical: 10.0), 157 child: Text( 158 'Tap for photo', 159 style: Theme.of(context) 160 .textTheme 161 .subtitle1 162 .copyWith(color: Colors.white), 163 ), 164 ) 165 ], 166 ), 167 ], 168 ), 169 ); 170 } 171} 172 173// A widget that displays the picture taken by the user. 174class DisplayPictureScreen extends StatefulWidget { 175 final String imagePath; 176 DisplayPictureScreen({Key key, this.imagePath}) ; 177 178 179 _DisplayPictureScreenState createState() => _DisplayPictureScreenState(imagePath:imagePath); 180} 181 182class _DisplayPictureScreenState extends State<DisplayPictureScreen>{ 183 final String imagePath; 184 _DisplayPictureScreenState({this.imagePath}); 185 186 Widget build(BuildContext context) { 187 return Scaffold( 188 appBar: AppBar(title: Text('Display the Picture')), 189 // The image is stored as a file on the device. Use the `Image.file` 190 // constructor with the given path to display the image. 191 body: Image.file(File(imagePath)), 192 193 floatingActionButton:FloatingActionButton( 194 onPressed:imagePath==null?null :()=> _saveFile(false), 195 child:Text('save'), 196 ), 197 ); 198 } 199 200 201 Future<void> _saveFile(bool useData) async { 202 try { 203 setState(() { 204 _isBusy = true; 205 if(imagePath!=null){ 206 _currentFile = File(imagePath); 207 } else { 208 _currentFile=null; 209 } 210 211 }); 212 final data = useData ? await _currentFile.readAsBytes() : null; 213 final params = SaveFileDialogParams( 214 sourceFilePath: useData ? null : _currentFile.path, 215 data: data, 216 localOnly: _localOnly, 217 fileName: useData ? "untitled" : null); 218 result= await FlutterFileDialog.saveFile(params: params); 219 print(result); 220 } on PlatformException catch (e) { 221 print(e); 222 } finally { 223 setState(() { 224 _savedFilePath = result ?? _savedFilePath; 225 _isBusy = false; 226 }); 227 } 228 } 229} 230 231
試したこと
onswitchcameraウィジェットの中身について調べて見たのですが、記事などを参考にしたところどの記事も同じような記法で書かれていたので私が調べた限りでは特に問題はないと思います...
補足情報(FW/ツールのバージョンなど)
パッケージの情報です
dependencies: flutter: sdk: flutter cupertino_icons: ^0.1.2 camera: ^0.8.0 path_provider: ^2.0.1 flutter_native_splash: ^1.0.3 flutter_file_dialog: ^2.0.0 storage_path: ^0.2.0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。