import
'package:flutter/material.dart'
;
import
'package:http/http.dart'
as http;
import
'dart:convert'
;
import
'dart:core'
;
void
main() {
runApp(MyApp());
}
class
MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return
MaterialApp(
debugShowCheckedModeBanner:
false
,
home: MyHomePage(),
);
}
}
class
MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class
_MyHomePageState extends State<MyHomePage> {
Map pokeData = {};
List pokeList = [];
Future<
void
> getData() async {
http.Response pokeResponse;
String urlString =
Uri uri = Uri.parse(urlString);
pokeResponse = await http.get(uri);
if
(pokeResponse.statusCode == 200) {
pokeData = json.decode(pokeResponse.body);
pokeList = pokeData[
'pokemon'
];
}
else
{
throw
Exception(
'Failed to load data from the API'
);
}
}
void
initState() {
getData();
super.initState();
}
Widget build(BuildContext context) {
return
Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text(
"PokeDex"
),
backgroundColor: Colors.green,
),
body: pokeList == null
? Center(child: Text(
"Wait"
))
: ListView.builder(
itemCount: pokeList == null ? 0 : pokeList.length,
itemBuilder: (context, index) {
return
Container(
child: Column(
children: <Widget>[
Padding(
padding:
const
EdgeInsets.all(10.0),
child: Container(
height: 300,
width: 300,
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(20)),
child: Column(
children: [
Padding(
padding:
const
EdgeInsets.all(8.0),
child: Text(
pokeList[index][
'name'
],
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w600,
color: Colors.white),
),
),
Image.network(
pokeList[index][
'img'
],
width: 300,
height: 200,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Candy: "
,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: Colors.white),
),
Text(pokeList[index][
'candy'
],
style: TextStyle(
fontSize: 15,
color: Colors.white,
fontWeight: FontWeight.w500)),
],
),
SizedBox(
height: 10,
),
],
),
),
)
],
),
);
},
));
}
}