languages

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

search_page.dart (3866B)


      1 import 'package:flutter/foundation.dart';
      2 import 'package:flutter/material.dart';
      3 import 'package:cloud_firestore/cloud_firestore.dart';
      4 import 'machine_details_page.dart';
      5 
      6 class SearchScreen extends StatefulWidget {
      7   const SearchScreen({super.key});
      8 
      9   @override
     10   State<SearchScreen> createState() => _SearchScreenState();
     11 }
     12 
     13 class _SearchScreenState extends State<SearchScreen> {
     14   CollectionReference collectionReference =
     15       FirebaseFirestore.instance.collection('machines');
     16   List<Map<String, dynamic>>? allMachines;
     17   List<Map<String, dynamic>>? machines;
     18   String searchText = '';
     19 
     20   @override
     21   void initState() {
     22     super.initState();
     23     _fetchMachines();
     24   }
     25 
     26   Future<void> _fetchMachines() async {
     27     try {
     28       final snapshot = await collectionReference.get();
     29       setState(() {
     30         allMachines = snapshot.docs
     31             .map((doc) => doc.data() as Map<String, dynamic>)
     32             .toList()
     33           ..sort((a, b) => a['name'].compareTo(b['name']));
     34         machines = allMachines;
     35       });
     36     } catch (error) {
     37       if (kDebugMode) {
     38         print("Error fetching machines: $error");
     39       }
     40     }
     41   }
     42 
     43   void _onSearchTextChanged(String text) {
     44     setState(() {
     45       searchText = text;
     46       if (text.isEmpty) {
     47         machines = allMachines;
     48       } else {
     49         machines = allMachines
     50             ?.where((machine) =>
     51                 machine['name'].toLowerCase().contains(text.toLowerCase()))
     52             .toList();
     53       }
     54     });
     55   }
     56 
     57   void _navigateToDetailsPage(Map<String, dynamic> machine) {
     58     Navigator.push(
     59       context,
     60       MaterialPageRoute(
     61         builder: (context) => MachineDetailsPage(machine: machine),
     62       ),
     63     );
     64   }
     65 
     66   @override
     67   Widget build(BuildContext context) {
     68     return Scaffold(
     69       body: SafeArea(
     70         child: Column(
     71           children: [
     72             Padding(
     73               padding: const EdgeInsets.all(8.0),
     74               child: TextField(
     75                 decoration: const InputDecoration(
     76                   hintText: 'Search Machines...',
     77                   enabledBorder: UnderlineInputBorder(
     78                     borderSide: BorderSide(color: Colors.black),
     79                   ),
     80                   focusedBorder: UnderlineInputBorder(
     81                     borderSide: BorderSide(color: Colors.black),
     82                   ),
     83                   border: UnderlineInputBorder(
     84                     borderSide: BorderSide(color: Colors.black),
     85                   ),
     86                 ),
     87                 onChanged: _onSearchTextChanged,
     88               ),
     89             ),
     90             Expanded(
     91               child: machines != null
     92                   ? (machines!.isEmpty)
     93                       ? const Center(child: Text('No machines found.'))
     94                       : ListView.builder(
     95                           itemCount: machines!.length,
     96                           itemBuilder: (context, index) {
     97                             final machine = machines![index];
     98                             return ListTile(
     99                               title: Text(machine['name']),
    100                               subtitle: Column(
    101                                 crossAxisAlignment: CrossAxisAlignment.start,
    102                                 children: [
    103                                   Text(machine['address']),
    104                                   Text(
    105                                     'Status: ${machine['status']}',
    106                                     style: const TextStyle(color: Colors.black),
    107                                   ),
    108                                 ],
    109                               ),
    110                               onTap: () => _navigateToDetailsPage(machine),
    111                             );
    112                           },
    113                         )
    114                   : const Center(child: CircularProgressIndicator()),
    115             ),
    116           ],
    117         ),
    118       ),
    119     );
    120   }
    121 }