Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camera Feature (WIP) #170

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions game/assets/icons/Shutter.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions game/assets/icons/back.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
168 changes: 168 additions & 0 deletions game/lib/gameplay/camera_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'dart:async';
import 'dart:io';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_svg/flutter_svg.dart';

class CameraPage extends StatefulWidget {
const CameraPage({Key? key}) : super(key: key);

@override
State<CameraPage> createState() => _CameraPageState();
}

class _CameraPageState extends State<CameraPage> {
late List<CameraDescription> _cameras;
late CameraController _controller;
int _selected = 0;
String imagePath = "";

@override
void initState() {
super.initState();
setupCamera();
}

Future<void> setupCamera() async {
await [
Permission.camera,
].request();
_cameras = await availableCameras();

_controller = CameraController(_cameras[_selected], ResolutionPreset.max);
_controller.initialize().then((_) {
if (!mounted) {
return;
}
setState(() {});
}).catchError((Object e) {
if (e is CameraException) {
switch (e.code) {
case 'CameraAccessDenied':
// Handle access errors here.
break;
default:
// Handle other errors here.
break;
}
}
});
}

toggleCamera() async {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a return type here Future<...> otherwise this returns dynamic

int newSelected = (_selected + 1) % _cameras.length;
_selected = newSelected;

var controller =
await CameraController(_cameras[_selected], ResolutionPreset.max);
setState(() => _controller = controller);
}

void takePhoto() async {
try {
final image = await _controller.takePicture();
setState(() {
imagePath = image.path;
//TODO: save image path to backend
});
} catch (e) {
print(e);
}
}

Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: EdgeInsets.only(top: 80.0),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Color.fromARGB(255, 237, 86, 86),
Color.fromARGB(77, 237, 86, 86)
], // Adjust colors as needed
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
stops: [0.0, 0.5], // Adjust stops as needed
tileMode: TileMode.clamp,
),
),
child: Column(
children: [
Row(
children: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
style: TextButton.styleFrom(
backgroundColor: Color.fromARGB(0, 0, 0, 0)),
child: Row(
children: [
Padding(
padding: EdgeInsets.only(right: 8.0),
child: SvgPicture.asset(
"assets/icons/back.svg",
width: 6,
height: 14,
colorFilter: ColorFilter.mode(
Color.fromARGB(255, 96, 67, 91), BlendMode.srcIn),
),
),
Text(
"Leave Photo",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
),
),
],
),
),
Spacer(),
ElevatedButton(
onPressed: () {},
child: Text(
"Challenge",
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w400,
),
),
)
],
),
Text(
"Remember this moment with a photo!",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w700,
),
),
imagePath == ""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you can use a better state here instead of imagePath == "". It's confusing to see why an empty image path means we have a camera preview. If you plan to keep this, please add a comment explaining why you make this comparison.

? Container(
width: 600,
height: 600,
child: AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: CameraPreview(_controller),
),
)
: Container(
width: 600,
height: 600,
child: Image.file(
File(imagePath),
),
),
IconButton(
onPressed: takePhoto,
icon: SvgPicture.asset('assets/icons/Shutter.svg'),
),
// Image.file(),
],
),
),
);
}
}
4 changes: 3 additions & 1 deletion game/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:game/gameplay/gameplay_page.dart';
import 'package:game/global_leaderboard/global_leaderboard_widget.dart';
import 'package:game/journeys/journeys_page.dart';
import 'package:game/login/login_page.dart';
import 'package:game/gameplay/camera_page.dart';
import 'package:game/model/challenge_model.dart';
import 'package:game/model/event_model.dart';
import 'package:game/model/group_model.dart';
Expand Down Expand Up @@ -76,7 +77,8 @@ class MyApp extends StatelessWidget {
supportedLocales: const [Locale('en', '')],
theme: ThemeData(
fontFamily: 'Poppins', primarySwatch: ColorPalette.BigRed),
home: SplashPageWidget(),
// home: SplashPageWidget(),
home: CameraPage(),
)));
}
}
3 changes: 3 additions & 0 deletions game/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ dependencies:
latlong2: ^0.9.0
device_info_plus: ^9.1.0
flutter_svg: ^2.0.8
path_provider: ^2.0.9
path: ^1.8.0
camera: ^0.9.4+18

flutter_icons:
android: true
Expand Down