machine_details_page.dart (3576B)
1 // ignore_for_file: use_build_context_synchronously 2 3 import 'package:flutter/material.dart'; 4 import 'package:cloud_firestore/cloud_firestore.dart'; 5 import 'package:firebase_auth/firebase_auth.dart'; 6 7 class MachineDetailsPage extends StatefulWidget { 8 final Map<String, dynamic> machine; 9 10 const MachineDetailsPage({super.key, required this.machine}); 11 12 @override 13 State<MachineDetailsPage> createState() => _MachineDetailsPageState(); 14 } 15 16 class _MachineDetailsPageState extends State<MachineDetailsPage> { 17 final FirebaseFirestore _firestore = FirebaseFirestore.instance; 18 19 @override 20 Widget build(BuildContext context) { 21 final machine = widget.machine; 22 23 return Scaffold( 24 appBar: AppBar( 25 title: Text(machine['name']), 26 ), 27 body: SingleChildScrollView( 28 child: Padding( 29 padding: const EdgeInsets.all(16.0), 30 child: Column( 31 crossAxisAlignment: CrossAxisAlignment.start, 32 children: [ 33 Text( 34 '${machine['name']}', 35 style: 36 const TextStyle(fontSize: 18, fontWeight: FontWeight.bold), 37 ), 38 const SizedBox(height: 8.0), 39 Text('${machine['address']}'), 40 const SizedBox(height: 8.0), 41 Row( 42 children: [ 43 const Text( 44 'Phone:', 45 style: TextStyle(fontWeight: FontWeight.bold), 46 ), 47 const SizedBox(width: 8.0), 48 Text('${machine['phone'] ?? 'None'}'), 49 ], 50 ), 51 const SizedBox(height: 8.0), 52 Text('${machine['bio'] ?? ''}'), 53 const SizedBox(height: 16.0), 54 SizedBox( 55 width: double.infinity, 56 child: ClipRRect( 57 borderRadius: BorderRadius.circular(8.0), 58 child: Image.network( 59 machine['imageUrl'] ?? '', 60 errorBuilder: (context, error, stackTrace) => 61 const Text('Error loading image'), 62 fit: BoxFit.cover, 63 ), 64 ), 65 ), 66 const SizedBox(height: 8.0), 67 Center( 68 child: ElevatedButton( 69 onPressed: () async { 70 final userEmail = await getUserEmail(); 71 72 await _firestore.collection('collected').add({ 73 'machine_id': machine['name'], 74 'user_email': userEmail, 75 'collectedURL': null, 76 }); 77 ScaffoldMessenger.of(context).showSnackBar( 78 const SnackBar( 79 content: Text('Machine collected successfully!'), 80 ), 81 ); 82 }, 83 style: ElevatedButton.styleFrom( 84 backgroundColor: 85 Colors.black, 86 ), 87 child: const Text( 88 'Collect', 89 style: TextStyle( 90 color: Colors.white, 91 fontWeight: FontWeight.bold, 92 ), 93 ), 94 ), 95 ), 96 ], 97 ), 98 ), 99 ), 100 ); 101 } 102 103 Future<String> getUserEmail() async { 104 if (FirebaseAuth.instance.currentUser != null) { 105 return FirebaseAuth.instance.currentUser!.email!; 106 } 107 return 'example@email.com'; 108 } 109 }