collection_page.dart (3575B)
1 import 'package:flutter/material.dart'; 2 import 'package:cloud_firestore/cloud_firestore.dart'; 3 import 'package:firebase_auth/firebase_auth.dart'; 4 import 'package:penny/pages/camera_page.dart'; 5 6 class CollectionScreen extends StatefulWidget { 7 const CollectionScreen({super.key}); 8 9 @override 10 State<CollectionScreen> createState() => _CollectionScreenState(); 11 } 12 13 class _CollectionScreenState extends State<CollectionScreen> { 14 final FirebaseFirestore _firestore = FirebaseFirestore.instance; 15 final FirebaseAuth _auth = FirebaseAuth.instance; 16 List<Map<String, dynamic>> collectedMachines = []; 17 List<Map<String, dynamic>> displayedMachines = []; 18 String searchText = ''; 19 20 @override 21 void initState() { 22 super.initState(); 23 _fetchCollectedMachines(); 24 } 25 26 Future<void> _fetchCollectedMachines() async { 27 final userEmail = await _getUserEmail(); 28 if (userEmail == null) return; 29 30 final snapshot = await _firestore 31 .collection('collected') 32 .where('user_email', isEqualTo: userEmail) 33 .get(); 34 35 collectedMachines = snapshot.docs.map((doc) => doc.data()).toList(); 36 displayedMachines = collectedMachines; 37 setState(() {}); 38 } 39 40 Future<String?> _getUserEmail() async { 41 final user = _auth.currentUser; 42 return user?.email; 43 } 44 45 void _onSearchTextChanged(String text) { 46 setState(() { 47 searchText = text; 48 displayedMachines = collectedMachines 49 .where((machine) => 50 machine['machine_id'] 51 .toString() 52 .toLowerCase() 53 .contains(text.toLowerCase()) || 54 (machine['name']?.toString().toLowerCase() ?? '') 55 .contains(text.toLowerCase())) 56 .toList(); 57 }); 58 } 59 60 @override 61 Widget build(BuildContext context) { 62 return Scaffold( 63 body: SafeArea( 64 child: Column( 65 children: [ 66 Padding( 67 padding: const EdgeInsets.all(8.0), 68 child: TextField( 69 decoration: const InputDecoration( 70 hintText: 'Search Machines...', 71 enabledBorder: UnderlineInputBorder( 72 borderSide: BorderSide(color: Colors.black), 73 ), 74 focusedBorder: UnderlineInputBorder( 75 borderSide: BorderSide(color: Colors.black), 76 ), 77 border: UnderlineInputBorder( 78 borderSide: BorderSide(color: Colors.black), 79 ), 80 ), 81 onChanged: _onSearchTextChanged, 82 ), 83 ), 84 Expanded( 85 child: displayedMachines.isEmpty 86 ? const Center(child: Text('No collected machines yet')) 87 : ListView.builder( 88 itemCount: displayedMachines.length, 89 itemBuilder: (context, index) { 90 final machine = displayedMachines[index]; 91 return ListTile( 92 title: 93 Text(machine['machine_id'] ?? 'Unknown machine'), 94 onTap: () => Navigator.push( 95 context, 96 MaterialPageRoute( 97 builder: (context) => 98 CameraScreen(machine: machine), 99 ), 100 ), 101 ); 102 }, 103 ), 104 ), 105 ], 106 ), 107 ), 108 ); 109 } 110 }