import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:hexcolor/hexcolor.dart'; import 'package:http/http.dart' as http; import 'package:idrocap/private/model/userList.dart'; import 'package:idrocap/utility/utility.dart'; import 'constant/globals.dart' as globals; import 'profile_users.dart'; import 'widget/menu.dart'; class Users extends StatefulWidget { const Users({Key? key}) : super(key: key); @override State createState() => _UsersState(); } class _UsersState extends State { late Future> shows; String searchString = ""; @override void initState() { super.initState(); shows = fetchShows(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( iconTheme: IconThemeData(color: Colors.white), backgroundColor: HexColor(globals.colorAppbar), title: Center(child: Text("Utenti", style: TextStyle(color: Colors.white))), actions: [ IconButton( onPressed: () {}, icon: Icon(Icons.notifications), ) ], ), drawer: Menu(), floatingActionButtonLocation: FloatingActionButtonLocation.miniEndFloat, floatingActionButton: FloatingActionButton( onPressed: () { setState(() { shows = fetchShows(); }); }, tooltip: 'refresh', child: Icon(Icons.refresh, color: HexColor(globals.colorAppbar)), backgroundColor: Colors.white, ), body: SingleChildScrollView( child: Column( children: [ Padding( padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10), child: TextField( onChanged: (value) { setState(() { searchString = value.toLowerCase(); }); }, decoration: InputDecoration(labelText: 'Cerca Utente ', suffixIcon: Icon(Icons.search)), ), ), FutureBuilder>( future: shows, builder: (context, snapshot) { if (snapshot.connectionState != ConnectionState.done) { return Center(child: CircularProgressIndicator()); } if (snapshot.hasData) { final filteredUsers = snapshot.data!.where((user) { final lowerName = user.name.toLowerCase(); final lowerSurname = user.surname.toLowerCase(); final lowerSearchString = searchString.toLowerCase(); return lowerName.contains(lowerSearchString) || lowerSurname.contains(lowerSearchString); }).toList(); if (filteredUsers.isEmpty) { return Center( child: Text('Nessun utente trovato'), ); } return ListView.separated( physics: NeverScrollableScrollPhysics(), scrollDirection: Axis.vertical, shrinkWrap: true, itemCount: filteredUsers.length, itemBuilder: (BuildContext context, int index) { return GestureDetector( onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => ProfileUser( userList: filteredUsers[index], ), ), ); }, child: ListTile( leading: Icon( Icons.account_circle_outlined, size: 40, color: Colors.blue, ), title: Text('${filteredUsers[index].name} ${filteredUsers[index].surname}'), ), ); }, separatorBuilder: (BuildContext context, int index) { return Divider(); }, ); } if (snapshot.hasError) { return Center( child: Text('Errore: ${snapshot.error}'), ); } return Center(child: CircularProgressIndicator()); }, ), ], ), ), ); } Future> fetchShows() async { try { var url = Uri.parse(globals.url_endpoint + globals.listaUtenti); var response = await http.get(url, headers: globals.headers); var data = json.decode(utf8.decode(response.bodyBytes)); if (response.statusCode == 200) { var usersJson = (data['users'] as List).cast>(); return usersJson.map((user) => UserList.fromJson(user)).toList(); } else if (response.statusCode == 401) { Utility().dialogAlert(context); return []; // Restituire una lista vuota per gestire l'errore } else { ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(data['result'].toString()))); return []; // Restituire una lista vuota per gestire l'errore } } catch (e) { EasyLoading.dismiss(); throw Exception('Problemi nel caricare i dati: $e'); } } }