Skip to content

Commit

Permalink
version one
Browse files Browse the repository at this point in the history
  • Loading branch information
lekkalaharsha committed Aug 17, 2024
1 parent bf07e53 commit 66556e1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/chatscreen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO Implement this library.
22 changes: 16 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@

import 'package:camapp/screens/homepage.dart';
import 'package:flutter/material.dart';
import 'package:camapp/screens/constapi.dart';
import 'package:flutter_gemini/flutter_gemini.dart';
import 'package:camera/camera.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();

// Initialize the list of available cameras
final cameras = await availableCameras();
final firstCamera = cameras.first;

void main() {
// Initialize Gemini API
Gemini.init(
apiKey: GEMINI_API_KEY,
);
runApp(const MyApp());
);

runApp(MyApp(camera: firstCamera));
}

class MyApp extends StatelessWidget {
const MyApp({super.key});
final CameraDescription camera;

const MyApp({super.key, required this.camera});

@override
Widget build(BuildContext context) {
Expand All @@ -22,7 +32,7 @@ class MyApp extends StatelessWidget {
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: HomeScreen(),
home: HomeScreen(), // Pass the camera to HomeScreen
);
}
}
93 changes: 72 additions & 21 deletions lib/screens/homepage.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:image_picker/image_picker.dart';
import 'chatscreen.dart'; // Assuming the Chatscreen is in another file
import 'chatscreen.dart'; // Import your chat screen here

class HomeScreen extends StatefulWidget {
@override
Expand All @@ -9,13 +10,42 @@ class HomeScreen extends StatefulWidget {

class _HomeScreenState extends State<HomeScreen> {
int _selectedIndex = 0;
late CameraController _cameraController;
List<CameraDescription>? _cameras;

final List<Widget> _screens = [
Center(child: Text('Explore Screen')),
Center(child: Text('Food Labels Screen')),
Center(child: Text('Text Screen')),
Center(child: Text('Documents Screen')),
];
@override
void initState() {
super.initState();
_initializeCamera();
}

Future<void> _initializeCamera() async {
// Fetch the available cameras
_cameras = await availableCameras();
if (_cameras!.isNotEmpty) {
// Initialize the first camera
_cameraController = CameraController(
_cameras!.first,
ResolutionPreset.high,
);

try {
await _cameraController.initialize();
} catch (e) {
print('Error initializing camera: $e');
}

if (mounted) {
setState(() {});
}
}
}

@override
void dispose() {
_cameraController.dispose();
super.dispose();
}

void _onItemTapped(int index) {
setState(() {
Expand All @@ -24,35 +54,56 @@ class _HomeScreenState extends State<HomeScreen> {
}

Future<void> _openCamera(BuildContext context) async {
ImagePicker picker = ImagePicker();
XFile? file = await picker.pickImage(source: ImageSource.camera);

if (file != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Chatscreen(imagePath: file.path),
),
);
if (_cameraController.value.isInitialized) {
try {
XFile file = await _cameraController.takePicture();
if (file != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Chatscreen(
imagePath: file.path,
// prompt: _selectedIndex == 2
// ? "Read the text"
// : "Describe the image?",
),
),
);
}
} catch (e) {
print('Error capturing image: $e');
}
} else {
print('Camera is not initialized');
}
}

@override
Widget build(BuildContext context) {
// Define the screens to be displayed
final List<Widget> _screens = [
Center(child: Text('Explore Screen')),
Center(child: Text('Food Labels Screen')),
Center(child: Text('Text Screen')),
Center(child: Text('Documents Screen')),
];

return Scaffold(
appBar: AppBar(
title: Text('My App'),
backgroundColor: Colors.black,
actions: [
IconButton(
icon: Icon(Icons.person_outline, color: Colors.blue),
onPressed: () {
print("hi");
},
onPressed: () {},
),
],
),
body: _screens[_selectedIndex],
body: _selectedIndex == 0 && _cameraController.value.isInitialized
? SizedBox.expand(
child: CameraPreview(_cameraController),
)
: _screens[_selectedIndex],
bottomNavigationBar: Container(
color: Colors.black,
padding: EdgeInsets.symmetric(vertical: 10),
Expand Down

0 comments on commit 66556e1

Please sign in to comment.