Created
April 5, 2022 19:43
-
-
Save mkiisoft/3baef17f750547a7cc0857206f569530 to your computer and use it in GitHub Desktop.
Free Countries
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'; | |
void main() => runApp(MainApp()); | |
class MainApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.light(), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold(body: MainScreen()), | |
); | |
} | |
} | |
class MainScreen extends StatefulWidget { | |
@override | |
State<MainScreen> createState() => _MainScreenState(); | |
} | |
class _MainScreenState extends State<MainScreen> { | |
late Future<Parse> fetch; | |
@override | |
void initState() { | |
fetch = GetData.fetch(); | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Material( | |
child: Column( | |
children: [ | |
const SizedBox(height: 20), | |
const Text( | |
'Free Countries ✈️🌎', | |
style: TextStyle( | |
fontSize: 24, | |
fontWeight: FontWeight.bold, | |
), | |
), | |
const SizedBox(height: 10), | |
Expanded( | |
child: FutureBuilder<Parse>( | |
future: fetch, | |
builder: (context, snapshot) { | |
if (snapshot.connectionState == ConnectionState.waiting) { | |
return const Center(child: Text('Loading...')); | |
} else if (snapshot.hasError) { | |
return Center(child: Text(snapshot.error.toString())); | |
} | |
final data = snapshot.data!; | |
return ListView.builder( | |
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10), | |
itemBuilder: (context, index) { | |
return Padding( | |
padding: const EdgeInsets.symmetric(vertical: 5), | |
child: Card( | |
elevation: 10, | |
child: Padding( | |
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20), | |
child: Text(data.countries[index].name), | |
), | |
), | |
); | |
}, | |
itemCount: data.countries.length, | |
); | |
}, | |
), | |
), | |
const Padding( | |
padding: EdgeInsets.symmetric(vertical: 16), | |
child: Center(child: Text('Made with ❤️ by Mariano Zorrilla')), | |
), | |
], | |
), | |
); | |
} | |
} | |
class GetData { | |
static Future<Parse> fetch() async { | |
final response = await get(Uri.parse("https://free-countries.glitch.me/countries")); | |
final json = jsonDecode(response.body); | |
return Parse.fromJson(json); | |
} | |
} | |
class Country { | |
final String name; | |
Country(this.name); | |
factory Country.fromJson(Map<String, dynamic> json) => Country(json['name']); | |
} | |
class Parse { | |
List<Country> countries = []; | |
List<Country> newCountries = []; | |
int lastUpdate = 0; | |
Parse(this.countries, this.newCountries, this.lastUpdate); | |
factory Parse.fromJson(Map<String, dynamic> json) => Parse( | |
List<dynamic>.from(json['countries']) | |
.map((i) => Country.fromJson(i)) | |
.toList(), | |
List<dynamic>.from(json['new_countries']) | |
.map((i) => Country.fromJson(i)) | |
.toList(), | |
json['last_update'], | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment