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.
181 righe
6.1 KiB
181 righe
6.1 KiB
import 'package:flutter/material.dart'; |
|
import 'package:flutter_easyloading/flutter_easyloading.dart'; |
|
import 'package:hexcolor/hexcolor.dart'; |
|
import 'package:idrocap/api/api_service.dart'; |
|
import 'package:idrocap/private/dettaglio_pratica.dart'; |
|
import 'package:idrocap/private/model/notifiche.dart'; |
|
import 'package:idrocap/private/widget/menu.dart'; |
|
import 'package:http/http.dart' as http; |
|
import 'package:idrocap/utility/utility.dart'; |
|
import 'constant/globals.dart' as globals; |
|
import 'dart:convert'; |
|
|
|
class Notifiche extends StatefulWidget { |
|
const Notifiche({super.key}); |
|
|
|
@override |
|
State<Notifiche> createState() => _NotificheState(); |
|
} |
|
|
|
class _NotificheState extends State<Notifiche> { |
|
late Future<List<RootNotification>> shows; |
|
@override |
|
void initState() { |
|
shows = Future.value([]); |
|
shows = fetchShows(); |
|
super.initState(); |
|
} |
|
|
|
@override |
|
void dispose() { |
|
EasyLoading.dismiss(); |
|
super.dispose(); |
|
} |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
return new PopScope( |
|
canPop: true, |
|
child: Scaffold( |
|
appBar: AppBar( |
|
leading: InkWell( |
|
onTap: () { |
|
Navigator.pop(context); |
|
}, |
|
child: Icon( |
|
Icons.arrow_back_ios, |
|
color: Colors.white, |
|
), |
|
), |
|
iconTheme: IconThemeData(color: Colors.white), |
|
backgroundColor: HexColor(globals.colorAppbar), |
|
title: Center( |
|
child: Text( |
|
"Sezione notifiche", |
|
style: TextStyle(color: Colors.white), |
|
), |
|
), |
|
), |
|
drawer: Menu(), |
|
body: SingleChildScrollView( |
|
child: FutureBuilder<List<RootNotification>>( |
|
future: shows, |
|
builder: (context, snapshot) { |
|
if (snapshot.connectionState == ConnectionState.waiting) { |
|
return Center( |
|
child: Padding( |
|
padding: EdgeInsets.only(top: 80), |
|
child: CircularProgressIndicator(), |
|
), |
|
); |
|
} |
|
|
|
if (snapshot.hasError) { |
|
return Padding( |
|
padding: EdgeInsets.all(10), |
|
child: Center( |
|
child: Text('Errore nel caricamento delle notifiche'), |
|
), |
|
); |
|
} |
|
|
|
final data = snapshot.data; |
|
|
|
if (data == null || data.isEmpty) { |
|
globals.userAccess.user.notifications = false; |
|
return Padding( |
|
padding: EdgeInsets.all(50), |
|
child: Center( |
|
child: ListTile( |
|
leading: Icon(Icons.warning), |
|
title: Text("La ricerca non ha fornito alcun risultato"), |
|
), |
|
), |
|
); |
|
} |
|
|
|
return ListView.builder( |
|
physics: NeverScrollableScrollPhysics(), |
|
scrollDirection: Axis.vertical, |
|
shrinkWrap: true, |
|
itemCount: data.length, |
|
itemBuilder: (BuildContext context, int index) { |
|
final notification = data[index].notificheModel; |
|
|
|
return Column( |
|
children: [ |
|
GestureDetector( |
|
onTap: () { |
|
EasyLoading.show(status: "Caricamento in corso!"); |
|
ApiService().setNotification(data[index].id.toString(), context); |
|
if (notification.link.contains("water_drawing_paperworks/view/")) { |
|
ApiService().getData(0, notification.link, 1).then((value) { |
|
EasyLoading.dismiss(); |
|
|
|
if (value["statusCode"] == 200 && value["result"] is Map<String, dynamic>) { |
|
Navigator.push( |
|
context, |
|
MaterialPageRoute( |
|
builder: (context) => PraticheDettaglio( |
|
oggettoRicevuto: value, |
|
), |
|
), |
|
).then((_) { |
|
setState(() { |
|
shows = fetchShows(); |
|
}); |
|
}); |
|
} else { |
|
ScaffoldMessenger.of(context).showSnackBar( |
|
SnackBar(content: Text(value["result"] ?? "Errore sconosciuto")), |
|
); |
|
} |
|
}); |
|
} |
|
EasyLoading.dismiss(); |
|
setState(() { |
|
shows = fetchShows(); |
|
}); |
|
return; |
|
}, |
|
child: ListTile( |
|
leading: Icon(Icons.info, size: 40, color: Colors.blue), |
|
title: Text( |
|
notification.title ?? '', |
|
style: TextStyle(fontWeight: FontWeight.bold), |
|
), |
|
subtitle: Text( |
|
notification.body ?? '', |
|
style: TextStyle(fontStyle: FontStyle.italic), |
|
), |
|
), |
|
), |
|
Divider(), |
|
], |
|
); |
|
}, |
|
); |
|
}, |
|
)), |
|
), |
|
); |
|
} |
|
|
|
Future<List<RootNotification>> fetchShows() async { |
|
var response; |
|
|
|
var url = Uri.parse(globals.url_endpoint + globals.notifiche); |
|
response = await http.get(url, headers: globals.headers); |
|
var test = json.decode(utf8.decode(response.bodyBytes)); |
|
if (response.statusCode == 200) { |
|
var topShowsJson = (test['notifications']) as List; |
|
|
|
return topShowsJson.map((show) => RootNotification.fromJson(show)).toList(); |
|
} else if (response.statusCode == 401) { |
|
EasyLoading.dismiss(); |
|
Utility().dialogAlert(this.context); |
|
} |
|
EasyLoading.dismiss(); |
|
throw Exception('Problemi nel caricare i dati '); |
|
} |
|
}
|
|
|