languages

A collection of programs made with different programming languages.
git clone git://evanalba.com/languages
Log | Files | Refs

camera_page.dart (3621B)


      1 import 'dart:io';
      2 import 'package:flutter/foundation.dart';
      3 import 'package:flutter/material.dart';
      4 import 'package:image_picker/image_picker.dart';
      5 import 'package:cloud_firestore/cloud_firestore.dart';
      6 import 'package:firebase_auth/firebase_auth.dart';
      7 
      8 class CameraScreen extends StatefulWidget {
      9   final Map<String, dynamic> machine;
     10 
     11   const CameraScreen({
     12     super.key,
     13     required this.machine,
     14   });
     15 
     16   @override
     17   State<CameraScreen> createState() => _CameraScreenState();
     18 }
     19 
     20 class _CameraScreenState extends State<CameraScreen> {
     21   final ImagePicker _picker = ImagePicker();
     22   File? _pickedImage;
     23   final FirebaseAuth _auth = FirebaseAuth.instance;
     24 
     25   Future<void> _getImage(ImageSource source) async {
     26     final XFile? image = await _picker.pickImage(source: source);
     27     if (image != null) {
     28       setState(() {
     29         _pickedImage = File(image.path);
     30       });
     31     }
     32   }
     33 
     34   Future<void> _removeMachine() async {
     35     final FirebaseFirestore firestore = FirebaseFirestore.instance;
     36     final userEmail = await _getUserEmail();
     37 
     38     final query = firestore
     39         .collection('collected')
     40         .where(
     41           'machine_id',
     42           isEqualTo: widget.machine['machine_id'],
     43         )
     44         .where(
     45           'user_email',
     46           isEqualTo: userEmail,
     47         );
     48 
     49     final querySnapshot = await query.get();
     50 
     51     if (querySnapshot.docs.isNotEmpty) {
     52       final docRef = querySnapshot.docs[0].reference;
     53       await docRef.delete();
     54       Navigator.pop(context);
     55     } else {
     56       if (kDebugMode) {
     57         print("No document found for deletion");
     58       }
     59     }
     60   }
     61 
     62   Future<String?> _getUserEmail() async {
     63     final user = _auth.currentUser;
     64     return user?.email;
     65   }
     66 
     67   @override
     68   Widget build(BuildContext context) {
     69     return Scaffold(
     70       appBar: AppBar(
     71         title: Text(widget.machine['machine_id']),
     72         actions: [
     73           IconButton(
     74             icon: const Icon(Icons.photo_library, color: Colors.black),
     75             onPressed: () => _getImage(ImageSource.gallery),
     76           ),
     77         ],
     78       ),
     79       body: SingleChildScrollView(
     80         child: Center(
     81           child: Column(
     82             children: [
     83               MaterialButton(
     84                 color: Colors.black,
     85                 child: const Text(
     86                   "Take Image from Camera",
     87                   style: TextStyle(
     88                     color: Colors.white,
     89                     fontWeight: FontWeight.bold,
     90                   ),
     91                 ),
     92                 onPressed: () => _getImage(ImageSource.camera),
     93               ),
     94               _pickedImage != null
     95                   ? Image.file(_pickedImage!)
     96                   : Container(
     97                       height: 200,
     98                       width: 200,
     99                       decoration: BoxDecoration(
    100                         border: Border.all(
    101                           color: Colors.black,
    102                         ),
    103                         borderRadius: BorderRadius.circular(10),
    104                       ),
    105                       child: const Center(
    106                         child: Text(
    107                           "No Image Selected",
    108                           style: TextStyle(fontSize: 16),
    109                         ),
    110                       ),
    111                     ),
    112               ElevatedButton(
    113                 onPressed: _removeMachine,
    114                 style: ElevatedButton.styleFrom(
    115                   foregroundColor: Colors.white, backgroundColor: Colors.black, // Set text color to white
    116                 ),
    117                 child: const Text("Remove"),
    118               ),
    119             ],
    120           ),
    121         ),
    122       ),
    123     );
    124   }
    125 }