Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
146 righe
5.3 KiB
146 righe
5.3 KiB
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/notifiche.dart'; |
|
import 'package:idrocap/private/profile_organisations.dart'; |
|
import 'package:idrocap/utility/utility.dart'; |
|
|
|
import 'constant/globals.dart' as globals; |
|
import 'model/organizzazioni.dart'; |
|
import 'widget/menu.dart'; |
|
|
|
class Organisations extends StatefulWidget { |
|
const Organisations({Key? key}) : super(key: key); |
|
|
|
@override |
|
_OrganisationsState createState() => _OrganisationsState(); |
|
} |
|
|
|
class _OrganisationsState extends State<Organisations> { |
|
late Future<List<Organizzazioni>> 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("Organizzazioni", style: TextStyle(color: Colors.white))), |
|
actions: [ |
|
IconButton( |
|
onPressed: () { |
|
Navigator.push(context, MaterialPageRoute(builder: (context) => Notifiche())); |
|
}, |
|
icon: Icon(Icons.notifications), |
|
) |
|
], |
|
), |
|
drawer: Menu(), |
|
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 Organizzazione', suffixIcon: Icon(Icons.search)), |
|
), |
|
), |
|
FutureBuilder<List<Organizzazioni>>( |
|
future: shows, |
|
builder: (context, snapshot) { |
|
if (snapshot.connectionState != ConnectionState.done) { |
|
return Center(child: CircularProgressIndicator()); |
|
} |
|
|
|
if (snapshot.hasData) { |
|
return ListView.separated( |
|
physics: NeverScrollableScrollPhysics(), |
|
shrinkWrap: true, |
|
itemCount: snapshot.data!.length, |
|
itemBuilder: (BuildContext context, int index) { |
|
var organizzazione = snapshot.data![index]; |
|
if (organizzazione.actor.description.toLowerCase().contains(searchString)) { |
|
return GestureDetector( |
|
onTap: () { |
|
Navigator.push( |
|
context, |
|
MaterialPageRoute( |
|
builder: (context) => ProfileOrganisations(organizzazioni: organizzazione), |
|
), |
|
); |
|
}, |
|
child: ListTile( |
|
leading: Icon(Icons.account_circle_outlined, size: 40, color: Colors.blue), |
|
title: Text(organizzazione.actor.description.toString()), |
|
), |
|
); |
|
} else { |
|
return Container(); |
|
} |
|
}, |
|
separatorBuilder: (BuildContext context, int index) { |
|
var organizzazione = snapshot.data![index]; |
|
return organizzazione.actor.description.toLowerCase().contains(searchString) ? Divider() : Container(); |
|
}, |
|
); |
|
} else if (snapshot.hasError) { |
|
return Center(child: Text('Organizzazione non trovata')); |
|
} |
|
|
|
return Center(child: CircularProgressIndicator()); |
|
}, |
|
), |
|
], |
|
), |
|
), |
|
floatingActionButtonLocation: FloatingActionButtonLocation.miniEndFloat, |
|
floatingActionButton: FloatingActionButton( |
|
onPressed: () { |
|
setState(() { |
|
shows = fetchShows(); |
|
}); |
|
}, |
|
tooltip: 'refresh', |
|
child: Icon(Icons.refresh, color: HexColor(globals.colorAppbar)), |
|
backgroundColor: Colors.white, |
|
), |
|
); |
|
} |
|
|
|
Future<List<Organizzazioni>> fetchShows() async { |
|
try { |
|
var url = Uri.parse(globals.url_endpoint + globals.organizzazioni); |
|
var response = await http.get(url, headers: globals.headers); |
|
var data = json.decode(utf8.decode(response.bodyBytes)); |
|
|
|
if (response.statusCode == 200) { |
|
var organizzazioniJson = data['organisations'] as List; |
|
return organizzazioniJson.map((org) => Organizzazioni.fromJson(org)).toList(); |
|
} else if (response.statusCode == 401) { |
|
Utility().dialogAlert(context); |
|
} else { |
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(data['result'].toString()))); |
|
} |
|
} catch (e) { |
|
EasyLoading.dismiss(); |
|
throw Exception('Problemi nel caricare i dati'); |
|
} |
|
EasyLoading.dismiss(); |
|
return []; // In caso di errore, ritorna una lista vuota |
|
} |
|
}
|
|
|