Sure! Here's the edited code with the added function to open the device's camera and switch between front and rear cameras, along with a capture button and flashlight:
Code:
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
class CameraScreen extends StatefulWidget {
@override
_CameraScreenState createState() => _CameraScreenState();
}
class _CameraScreenState extends State<CameraScreen> {
CameraController? _controller;
Future<void>? _initializeControllerFuture;
bool _isFrontCamera = false;
bool _isFlashOn = false;
@override
void initState() {
super.initState();
_initializeCamera();
}
Future<void> _initializeCamera() async {
final cameras = await availableCameras();
final camera = _isFrontCamera ? cameras.last : cameras.first;
_controller = CameraController(camera, ResolutionPreset.high);
_initializeControllerFuture = _controller?.initialize();
if (mounted) {
setState(() {});
}
}
Future<void> _toggleCamera() async {
await _controller?.dispose();
_isFrontCamera = !_isFrontCamera;
_initializeCamera();
}
Future<void> _toggleFlash() async {
if (_isFlashOn) {
await _controller?.setFlashMode(FlashMode.off);
} else {
await _controller?.setFlashMode(FlashMode.torch);
}
_isFlashOn = !_isFlashOn;
setState(() {});
}
@override
void dispose() {
_controller?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
bool isExpanded = false;
return SingleChildScrollView(
child: Column(
children: [
GestureDetector(
onTap: () {
setState(() {
isExpanded = !isExpanded;
});
},
child: AnimatedSize(
duration: const Duration(milliseconds: 200),
curve: Curves.easeInOut,
child: Container(
height: isExpanded ? MediaQuery.of(context).size.height * 0.7 : MediaQuery.of(context).size.height * 0.3,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.deepPurple,
),
child: _controller != null && _controller!.value.isInitialized
? Stack(
children: [
CameraPreview(_controller!),
Align(
alignment: Alignment.topRight,
child: IconButton(
icon: Icon(
_isFlashOn ? Icons.flash_off : Icons.flash_on,
color: Colors.white,
),
onPressed: _toggleFlash,
),
),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.all(16),
child: ElevatedButton(
onPressed: () {
// Capture logic here
},
child: Icon(
Icons.camera,
size: 48,
),
style: ButtonStyle(
shape: MaterialStateProperty.all<CircleBorder>(
CircleBorder(),
),
padding: MaterialStateProperty.all<EdgeInsetsGeometry>(
EdgeInsets.all(16),
),
backgroundColor: MaterialStateProperty.all<Color>(
Colors.white,
),
),
),
),
),
],
)
: Center(
child: CircularProgressIndicator(),
),
),
),
),
],
),
);
}
}
Make sure to add the
camera
package to your
pubspec.yaml
file:
YAML:
dependencies:
camera: ^0.10.0
Please note that you may need to adjust the code according to your specific requirements and use the appropriate package for camera functionality in your app.
paki revise po ng code ko
// ignore_for_file: unused_import, library_private_types_in_public_api, prefer_final_fields, prefer_const_constructors, prefer_const_literals_to_create_immutables, sdk_version_ui_as_code
import 'package:color_blindness/color_blindness.dart';
import 'package:colorapp/function/deuteranopia.dart';
import 'package:colorapp/function/protanopia.dart';
import 'package:colorapp/function/tritanopia.dart';
import 'package:image_picker/image_picker.dart';
import 'package:colorapp/widget/contact.dart';
import 'package:colorapp/widget/guide.dart';
import 'package:colorapp/widget/info.dart';
import 'package:colorapp/widget/privacy_policy.dart';
import 'package:flutter/material.dart';
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:io';
import 'package:colorapp/global.dart';
import 'color_palette.dart';
import 'package:colorapp/function/camera.dart';
import 'package:colorapp/widget/gallery.dart';
class HomePage extends StatefulWidget {
const HomePage({
Key? key,
required this.toggleDarkMode,
required this.currentTheme,
}) : super(key: key);
final Function() toggleDarkMode;
final ThemeData currentTheme;
Override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
bool _isMenuOpen = false;
bool _isSettingsOpen = false;
bool _isDarkModeEnabled = false; // Track dark mode state
bool _isButtonClicked = false;
bool isExpanded = false;
void _toggleMenu() {
setState(() {
_isMenuOpen = !_isMenuOpen;
_isSettingsOpen = false; // Close settings if open
if (_isMenuOpen) {
_scaffoldKey.currentState?.openDrawer();
} else {
_scaffoldKey.currentState?.openEndDrawer();
}
});
}
void _toggleSettings() {
setState(() {
_isSettingsOpen = !_isSettingsOpen;
_isMenuOpen = false; // Close menu if open
if (_isSettingsOpen) {
_scaffoldKey.currentState?.openEndDrawer();
} else {
_scaffoldKey.currentState?.openDrawer();
}
});
}
void _toggleDarkMode() {
setState(() {
_isDarkModeEnabled = !_isDarkModeEnabled;
});
}
Override
Widget build(BuildContext context) {
return MaterialApp(
theme: _isDarkModeEnabled ? ThemeData.dark() : ThemeData.light(),
home: Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: const Text('Dye It'),
backgroundColor: Colors.deepPurple,
titleTextStyle: const TextStyle(fontFamily: 'RobotoMono', fontSize: 24),
),
bottomNavigationBar: CurvedNavigationBar(
backgroundColor: Colors.deepPurple,
buttonBackgroundColor: _isDarkModeEnabled ? Colors.grey : null,
animationDuration: const Duration(milliseconds: 200),
items: [
Icon(Icons.menu, color: _isDarkModeEnabled ? Colors.black : null),
Icon(Icons.home, color: _isDarkModeEnabled ? Colors.black : null),
Icon(Icons.settings, color: _isDarkModeEnabled ? Colors.black : null),
],
onTap: (index) {
if (index == 0) {
_toggleMenu();
} else if (index == 1) {
_isMenuOpen = false;
_isSettingsOpen = false;
setState(() {});
} else if (index == 2) {
_toggleSettings();
}
},
),
body: Stack(
children: [
// Add the code you want here
Opacity(
opacity: _isMenuOpen || _isSettingsOpen ? 0.7 : 1.0,
child: Container(
color: _isMenuOpen || _isSettingsOpen ? Colors.black : null,
),
),
SingleChildScrollView(
child: Column(
children: [
GestureDetector(
onTap: () {
setState(() {
isExpanded = !isExpanded;
});
},
child: AnimatedSize(
duration: const Duration(milliseconds: 200
),
curve: Curves.easeInOut,
child: Container(
height: isExpanded ? MediaQuery.of(context).size.height * 0.7 : MediaQuery.of(context).size.height * 0.3,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.deepPurple,
),
child: const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.camera,
color: Colors.white,
size: 48,
),
SizedBox(height: 10),
Text('CAMERA',
style: TextStyle(color: Colors.white, fontSize: 22),)
],
),
),
),
),
),
SizedBox(
height: 5,
),
Container(
margin: EdgeInsets.all(5),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
OutlinedButton(
onPressed: () {
// Navigate to TritanopiaPage with a cool animation
Navigator.push(
context,
PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) {
return FadeTransition(
opacity: animation,
child: TritanopiaPage(), // Navigate to TritanopiaPage
);
},
transitionDuration: Duration(milliseconds: 300), // Animation duration
),
);
},
style: OutlinedButton.styleFrom(
side: BorderSide(color: Colors.deepPurple),
backgroundColor: _isButtonClicked ? Colors.deepPurple : Colors.transparent,
),
child: Text('Tritanopia'),
),
OutlinedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DeuteranopiaPage(), // Replace with your actual screen name
),
);
},
style: OutlinedButton.styleFrom(
side: BorderSide(color: Colors.deepPurple),
backgroundColor: _isButtonClicked ? Colors.deepPurple : Colors.transparent,
),
child: Text('Deuteranopia'),
),
OutlinedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProtanopiaPage(), // Replace with your actual screen name
),
);
},
style: OutlinedButton.styleFrom(
side: BorderSide(color: Colors.deepPurple),
backgroundColor: _isButtonClicked ? Colors.deepPurple : Colors.transparent,
),
child: Text('Protanopia'),
),
],
),
),
Divider(
color: _isDarkModeEnabled ? Colors.white : Colors.black,
),
Container(
margin: EdgeInsets.symmetric(horizontal: 5), // Adjust the margin as needed
child: Row(
mainAxisAlignment: MainAxisAlignment.start, // Align buttons to the left
children: [
PopupMenuButton<String>(
onSelected: (String value) {
// Add your functionality for the first button here based on the selected value
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
PopupMenuItem<String>(
value: 'Option 1',
child: Text('Option 1'),
),
PopupMenuItem<String>(
value: 'Option 2',
child: Text('Option 2'),
),
// Add more options as needed
],
child: ElevatedButton(
onPressed: null, // Disable the button because it triggers the popup
style: ElevatedButton.styleFrom(
primary: Colors.deepPurple, // Button background color
),
child: Text('Button 1'),
),
),
SizedBox(width: 225), // Add spacing between buttons
TextButton(
onPressed: () {
// Add your functionality for the second button here
},
style: TextButton.styleFrom(
primary: Colors.deepPurple, // Button text color
),
child: Text('Button 2'),
),
],
),
),
Container(
height: MediaQuery.of(context).size.height * 0.3,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20), // Rounded corners
),
child: GridView.builder(
itemCount: 16,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(2.0),
child: Container(
height: 50,
width: 50,
color: Colors.grey,
),
);
}
),
),
],
),
),
if (_isMenuOpen || _isSettingsOpen)
GestureDetector(
onTap: () {
_isMenuOpen = false;
_isSettingsOpen = false;
setState(() {});
},
child: Container(
color: Colors.black.withOpacity(0.7),
),
),
if (_isMenuOpen)
Positioned(
left: 0,
top: 0,
bottom: 0,
width: 200,
child: Drawer(
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(5),
child: ElevatedButton(
onPressed: () {},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.person),
SizedBox(width: 8),
Text(
'Sign Up',
style: TextStyle(fontSize: 20),
),
],
),
),
),
Divider(
color: _isDarkModeEnabled ? Colors.white : Colors.black,
),
ListTile(
title: Text('Color Palette'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ColorPalettePage(
isDarkModeEnabled: _isDarkModeEnabled,
),
),
);
},
),
ListTile(
title: Text('About Us'),
),
ListTile(
title: Text('Privacy Policy'),
),
],
),
),
),
if (_isSettingsOpen)
Positioned(
right: 0,
top: 0,
bottom: 0,
width: 200,
child: Drawer(
child: Column(
children: <Widget>[
Expanded(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
ListTile(
title: Text('DarkMode'),
trailing: Switch(
value: _isDarkModeEnabled,
onChanged: (value) {
_toggleDarkMode();
},
),
),
Divider(
color: _isDarkModeEnabled
? Colors.white
: Colors.black,
),
ListTile(
title: Text('Guide'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => GuidePage(),
),
);
},
),
ListTile(
title: Text('Information'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => InfoPage(
isDarkModeEnabled: _isDarkModeEnabled,
),
),
);
},
),
ListTile(
title: Text('About Us'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ContactPage(),
),
);
},
),
ListTile(
title: Text('Privacy Policy'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
PrivacyPolicyPage(),
),
);
},
),
],
),
),
],
),
),
),
],
),
),
);
}
}