I’m working on a Flutter project where I need to open the front-facing camera, but no matter what I try, the camera always starts facing the back. I’m unfamiliar with the project, and I’m having trouble figuring out how to get the front-facing camera to open by default.
I’m running that getCameras() function and when that completes it should update my cameras list. Then setting my current camera to the one with front facing camera. I also tried choosing a specific index from the list but it was only ever the back camera
class _CameraState extends State<Camera>
with WidgetsBindingObserver, SingleTickerProviderStateMixin {
final GlobalKey<ConvexAppBarState> appBarKey;
_CameraState({this.appBarKey});
//Global Scaffold Key
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
FlutterSecureStorage storage = FlutterSecureStorage();
//Video and Camera variables
CameraController cameraController;
VideoPlayerController videoController;
VoidCallback videoPlayerListener;
XFile videoFile;
String uploadVideoPath;
bool enableAudio = true;
double _minAvailableExposureOffset = 0.0;
double _maxAvailableExposureOffset = 0.0;
double _currentExposureOffset = 0.0;
double _minAvailableZoom = 1.0;
double _maxAvailableZoom = 1.0;
double _currentScale = 1.0;
double _baseScale = 1.0;
int _pointers = 0;
bool _recordFirstPress;
bool showPath = true;
Timer _countdownTimer;
bool showCounter;
int _start = 3;
bool changeIcon;
AnimationController _animationController;
List<CameraDescription> cameras = [];
bool _cameraOn = true;
CameraDescription currentCamera;
//Video Upload Variable
final ImagePicker _picker = ImagePicker();
//Spoon Variables
bool _spoonActive;
double circles = 1.0;
bool spoonButtonUpdate;
bool showMessage;
double spreadRadius;
Color spreadColor;
List<SpoonSet> spoonSets = [];
SpoonSet currentSet = SpoonSet();
String _templateValue = 'Standard Template';
List<DropdownMenuItem> _templates = [];
List<Widget> _spoonWidgets = [];
List<dynamic> _spoonQuestions;
List<Widget> dots = [];
int currentSpoon = 1;
//Initialize screen
@override
void initState() {
super.initState();
showMessage = true;
spoonButtonUpdate = false;
checkSpoonAlert();
spreadRadius = 0;
spreadColor = Colors.transparent;
showCounter = false;
changeIcon = false;
WidgetsBinding.instance.addObserver(this);
_animationController = AnimationController(
vsync: this,
duration: Duration(seconds: 120),
)..addStatusListener((status) {
if (status == AnimationStatus.completed) {
onStopButtonPressed();
setState(() {
_animationController.value = 0;
});
}
});
_animationController.value = 0.0;
_recordFirstPress = true;
_spoonActive = false;
loadSpoon();
Future.delayed(Duration(milliseconds: 1000)).whenComplete(() {
setState(() {
spoonButtonUpdate = !spoonButtonUpdate;
spreadRadius = 2.0;
spreadColor = Colors.white.withOpacity(0.8);
});
});
getCameras().whenComplete(() {
currentCamera = cameras.firstWhere(
(camera) => camera.lensDirection == CameraLensDirection.front);
onNewCameraSelected(currentCamera);
});
}
Jess_c_lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.