From dbe59297769a1077647dd1361e139828dbca1d46 Mon Sep 17 00:00:00 2001 From: Rhevan Kruger Date: Tue, 22 Oct 2024 17:34:57 +0200 Subject: [PATCH] made states for camera page more reliable --- frontend/lib/components/confirm_pop_up.dart | 11 +++++-- frontend/lib/pages/camera.dart | 32 ++++++++++++++------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/frontend/lib/components/confirm_pop_up.dart b/frontend/lib/components/confirm_pop_up.dart index 8d0c480..6da40b0 100644 --- a/frontend/lib/components/confirm_pop_up.dart +++ b/frontend/lib/components/confirm_pop_up.dart @@ -206,7 +206,7 @@ class _ConfirmationPopUpState extends State { } return Transform( alignment: Alignment.center, - transform: Matrix4.rotationY(math.pi), // Flip the image horizontally + transform: Matrix4.rotationY(math.pi * 2), // Flip the image horizontally child: ClipRRect( borderRadius: BorderRadius.circular(0.0), // No rounding for full-screen image child: Container( @@ -244,6 +244,13 @@ class _ConfirmationPopUpState extends State { ? Theme.of(context).colorScheme.secondary // Use secondary color if transcribed text is not null : Colors.white, // Use white if transcribed text is null decoration: TextDecoration.none, // Remove underline + shadows: [ + Shadow( + blurRadius: 10.0, // Adjust for more or less blur + color: Colors.black.withOpacity(1), // Subtle black shadow + offset: Offset(0, 2), // Vertical offset to position the shadow + ), + ], ), textAlign: TextAlign.center, ), @@ -274,7 +281,7 @@ class _ConfirmationPopUpState extends State { heroTag: 'retake', // Unique tag for retake button backgroundColor: Theme.of(context).colorScheme.primary, // Keep the FAB's background transparent onPressed: () { - Navigator.of(context).pop(); // Closes the pop-up + Navigator.of(context).pop(true); // Closes the pop-up }, child: Icon( Icons.refresh, diff --git a/frontend/lib/pages/camera.dart b/frontend/lib/pages/camera.dart index 220a58b..7009a95 100644 --- a/frontend/lib/pages/camera.dart +++ b/frontend/lib/pages/camera.dart @@ -35,6 +35,8 @@ class _CameraPageState extends State { String audioMoodWeight = ''; bool audioReady = false; bool disabledButton = false; + GlobalKey _buttonKey = GlobalKey(); // Create a GlobalKey for the button + final List modes = ["Photo", "Video", "Audio"]; @@ -176,8 +178,8 @@ class _CameraPageState extends State { await _takeForcedPicture(); // Take a picture if none exists } else { int count = 0; - while (!audioReady && count < 10000000000) { - await Future.delayed(Duration(milliseconds: 100)); // Check every 100ms + while (!audioReady && count < 10000) { + await Future.delayed(Duration(milliseconds: 10)); // Check every 100ms count += 1; } disabledButton = false; @@ -240,6 +242,7 @@ class _CameraPageState extends State { ).then((_) { setState(() { pictureFile = null; // Reset picture file after dialog closes + disabledButton = false; }); }); } @@ -297,7 +300,7 @@ class _CameraPageState extends State { print("RETURNED MOODS"); print(returnedMoods.toString()); - if(returnedMoods.length == imagePaths.length) { + if (returnedMoods.length == imagePaths.length) { returnedMoods.add(audioMoodWeight); } @@ -305,7 +308,7 @@ class _CameraPageState extends State { await Future.delayed(Duration(milliseconds: 100)); // Send all the captured images during real-time video as a list - Navigator.push( + final result = await Navigator.push( context, MaterialPageRoute( builder: (context) => ConfirmationPopUp( @@ -317,15 +320,23 @@ class _CameraPageState extends State { isRealTimeVideo: true, ), ), - ).then((_) { + ); + + if (result == true) { + // Set disabledButton to true when navigating back setState(() { - pictureFile = null; - returnedMoods.clear(); - imagePaths.clear(); // Clear image paths after confirmation + disabledButton = false; }); + } + + setState(() { + pictureFile = null; + returnedMoods.clear(); + imagePaths.clear(); // Clear image paths after confirmation }); } + Future _takeForcedPicture() async { // Capture a picture and fetch mood if (controller != null && controller!.value.isInitialized) { @@ -333,8 +344,6 @@ class _CameraPageState extends State { if (pictureFile != null) { imagePaths.add(pictureFile!.path); // Add the image path here await _fetchMood(); // Fetch mood after picture is taken - // Proceed to confirmation after taking the forced picture - // _navigateToConfirmationPage(); } } } @@ -450,7 +459,8 @@ class _CameraPageState extends State { shape: BoxShape.circle, ), child: RawMaterialButton( - onPressed: _handleButtonPress, + key: _buttonKey, // Assign the GlobalKey to this button + onPressed: disabledButton ? null : _handleButtonPress, // Disable if flag is set shape: CircleBorder(), elevation: 2.0, child: null,