Created
May 9, 2025 14:57
-
-
Save Egidio-Bocchino/cee645236aec30f7c20e622cb569174f to your computer and use it in GitHub Desktop.
Lezione_11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:intl/intl.dart'; | |
import 'package:uuid/uuid.dart'; | |
const uuid = Uuid(); | |
final formatter = DateFormat('dd/MM/yyyy'); | |
class Personaggi { | |
final String idPg; | |
String fullName; | |
HogwartsHouse hogwartsHouse; | |
String interpretedBy; | |
Personaggi({ | |
required this.fullName, | |
required this.hogwartsHouse, | |
required this.interpretedBy, | |
}) : idPg = uuid.v4(); | |
} | |
class HogwartsHouse { | |
final String idHouse; | |
String house; | |
String emoji; | |
String founder; | |
List<String> colors; | |
String animal; | |
HogwartsHouse({ | |
required this.house, | |
required this.emoji, | |
required this.founder, | |
required this.colors, | |
required this.animal, | |
}) : idHouse = uuid.v4(); | |
} | |
class Libri { | |
final String idLibri; | |
String title; | |
DateTime releaseDate; | |
String description; | |
Libri({ | |
required this.title, | |
required this.releaseDate, | |
required this.description, | |
}) : idLibri = uuid.v4(); | |
String get formattedDate { | |
return formatter.format(releaseDate); | |
} | |
} | |
class Magie { | |
final String idMagie; | |
String spell; | |
String use; | |
Magie({ | |
required this.spell, | |
required this.use, | |
}) : idMagie = uuid.v4(); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
import 'package:lezione_11/models/information.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.tealAccent), | |
), | |
home: const MyHomePage(title: 'PotterPedia'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
String linkLibri = 'https://potterapi-fedeperin.vercel.app/it/books'; | |
String linkCase = 'https://potterapi-fedeperin.vercel.app/it/houses'; | |
String linkPg = 'https://potterapi-fedeperin.vercel.app/it/characters'; | |
String linkMagie = 'https://potterapi-fedeperin.vercel.app/it/spells'; | |
List<dynamic> libri = []; | |
List<dynamic> pg = []; | |
List<dynamic> spells = []; | |
List<dynamic> cases = []; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Theme | |
.of(context) | |
.colorScheme | |
.inversePrimary, | |
centerTitle: true, | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Center( | |
child: Column( | |
children: [ | |
GestureDetector( | |
onTap: () { | |
showBooks(); | |
}, | |
child: | |
SizedBox( | |
width: 200, | |
height: 80, | |
child: Card( | |
margin: const EdgeInsets.all(10), | |
child: Center( | |
child: const Text('Libri'), | |
), | |
), | |
), | |
), | |
GestureDetector( | |
onTap: () {}, | |
child: | |
SizedBox( | |
width: 200, | |
height: 80, | |
child: Card( | |
margin: const EdgeInsets.all(10), | |
child: Center( | |
child: const Text('Personaggi'), | |
), | |
), | |
), | |
), | |
GestureDetector( | |
onTap: () { | |
}, | |
child: | |
SizedBox( | |
width: 200, | |
height: 80, | |
child: Card( | |
margin: const EdgeInsets.all(10), | |
child: Center( | |
child: const Text('Magie'), | |
), | |
), | |
), | |
), | |
GestureDetector( | |
onTap: () {}, | |
child: | |
SizedBox( | |
width: 200, | |
height: 80, | |
child: Card( | |
margin: const EdgeInsets.all(10), | |
child: Center( | |
child: const Text('Case'), | |
), | |
), | |
), | |
), | |
], | |
), | |
), | |
], | |
), | |
), // This trailing comma makes auto-formatting nicer for build methods. | |
); | |
} | |
void getLibri() async { | |
Uri url = Uri.parse(linkLibri); | |
http.Response response = await http.get(url); | |
if (response.statusCode == 200) { | |
setState(() { | |
libri = jsonDecode(response.body); | |
}); | |
} else { | |
throw Exception('Errore nella richiesta'); | |
} | |
} | |
void showBooks() { | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) => BooksScreen(books: libri), | |
), | |
); | |
} | |
void getPg() async { | |
Uri url = Uri.parse(linkPg); | |
http.Response response = await http.get(url); | |
if (response.statusCode == 200) { | |
return jsonDecode(response.body); | |
} else { | |
throw Exception('Errore nella richiesta'); | |
} | |
} | |
void getSpells() async { | |
Uri url = Uri.parse(linkMagie); | |
http.Response response = await http.get(url); | |
if (response.statusCode == 200) { | |
return jsonDecode(response.body); | |
} else { | |
throw Exception('Errore nella richiesta'); | |
} | |
} | |
void getCase() async { | |
Uri url = Uri.parse(linkCase); | |
http.Response response = await http.get(url); | |
if (response.statusCode == 200) { | |
return jsonDecode(response.body); | |
} else { | |
throw Exception('Errore nella richiesta'); | |
} | |
} | |
} | |
class BooksScreen extends StatelessWidget { | |
final List<dynamic> books; | |
const BooksScreen({Key? key, required this.books}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('Libri')), | |
body: ListView.builder( | |
itemCount: books.length, | |
itemBuilder: (context, index) { | |
return ListTile( | |
title: Text(books[index]['title']), | |
subtitle: Text(books[index]['description']), | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment