Created
January 16, 2020 23:49
-
-
Save HudsonAfonso/e2124700cc6e29eed2285c30b6e477c4 to your computer and use it in GitHub Desktop.
Pokémon GO Pokédex
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:flutter/material.dart'; | |
import 'package:hasura_connect/hasura_connect.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Pokédex', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blueGrey, | |
), | |
home: const MyHomePage(title: 'Pokédex'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final Repository _repository = Repository(); | |
List<dynamic> _pokemons = <dynamic>[]; | |
String _selectedPokemon; | |
@override | |
void initState() { | |
super.initState(); | |
_repository.getGrass().then((List<dynamic> pokemons) { | |
print(pokemons); | |
setState(() { | |
_pokemons = pokemons; | |
}); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
_pokemons.isEmpty | |
? const CircularProgressIndicator(value: null) | |
: DropdownButton<String>( | |
value: _selectedPokemon, | |
items: _pokemons.map((dynamic pokemon) { | |
return DropdownMenuItem<String>( | |
value: pokemon['id'].toString(), | |
child: Text(pokemon['name']), | |
); | |
}).toList(), | |
onChanged: (String pokemonId) { | |
setState(() { | |
_selectedPokemon = pokemonId; | |
}); | |
}, | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class Repository { | |
final HasuraConnect connection = HasuraConnect('https://pokemon-go-pokedex.herokuapp.com/v1/graphql'); | |
Future<List<dynamic>> getGrass() async { | |
const String query = ''' | |
query { | |
pokemon(where: {type: {_eq: "grass"}}) { | |
id | |
name | |
} | |
} | |
'''; | |
final Map<String, dynamic> jsonMap = await connection.query(query); | |
print(jsonMap); | |
return jsonMap['data']['pokemon']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment