forked from zino-hofmann/graphql-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples/starwars): top level nav
- Loading branch information
Showing
11 changed files
with
406 additions
and
130 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'dart:convert'; | ||
|
||
/// The episodes in the Star Wars trilogy | ||
enum Episode { | ||
NEWHOPE, | ||
EMPIRE, | ||
JEDI, | ||
} | ||
|
||
String episodeToJson(Episode e) { | ||
switch (e) { | ||
case Episode.NEWHOPE: | ||
return 'NEWHOPE'; | ||
case Episode.EMPIRE: | ||
return 'EMPIRE'; | ||
case Episode.JEDI: | ||
return 'JEDI'; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
Episode episodeFromJson(String e) { | ||
switch (e) { | ||
case 'NEWHOPE': | ||
return Episode.NEWHOPE; | ||
case 'EMPIRE': | ||
return Episode.EMPIRE; | ||
case 'JEDI': | ||
return Episode.JEDI; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
String getPrettyJSONString(Object jsonObject) { | ||
return const JsonEncoder.withIndent(' ').convert(jsonObject); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
import './episode.dart'; | ||
import './hero_query.dart'; | ||
|
||
class EpisodePage extends StatefulWidget { | ||
static const BottomNavigationBarItem navItem = BottomNavigationBarItem( | ||
icon: Icon(Icons.movie_filter), | ||
title: Text('Episodes'), | ||
); | ||
|
||
@override | ||
_EpisodePageState createState() => _EpisodePageState(); | ||
} | ||
|
||
class _EpisodePageState extends State<EpisodePage> { | ||
Episode currentEpisode = Episode.EMPIRE; | ||
|
||
void _selectEpisode(Episode ep) { | ||
setState(() { | ||
currentEpisode = ep; | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
EpisodeSelect( | ||
selected: currentEpisode, | ||
onSelect: _selectEpisode, | ||
), | ||
const Text( | ||
'Hero for this episode:', | ||
), | ||
HeroForEpisode( | ||
episode: currentEpisode, | ||
) | ||
], | ||
), | ||
); | ||
} | ||
} | ||
|
||
typedef OnSelect = void Function(Episode episode); | ||
|
||
class EpisodeSelect extends StatelessWidget { | ||
const EpisodeSelect({ | ||
@required this.onSelect, | ||
@required this.selected, | ||
}); | ||
|
||
final OnSelect onSelect; | ||
final Episode selected; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return DropdownButton<Episode>( | ||
value: selected, | ||
onChanged: onSelect, | ||
items: Episode.values.map<DropdownMenuItem<Episode>>((Episode value) { | ||
return DropdownMenuItem<Episode>( | ||
value: value, | ||
child: Text(value.toString()), | ||
); | ||
}).toList(), | ||
); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:graphql_flutter/graphql_flutter.dart'; | ||
|
||
import './episode.dart'; | ||
|
||
class HeroForEpisode extends StatelessWidget { | ||
const HeroForEpisode({@required this.episode}); | ||
|
||
final Episode episode; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Query( | ||
options: QueryOptions( | ||
document: r''' | ||
query HeroForEpisode($ep: Episode!) { | ||
hero(episode: $ep) { | ||
__typename | ||
name | ||
... on Droid { | ||
primaryFunction | ||
} | ||
... on Human { | ||
height | ||
homePlanet | ||
} | ||
} | ||
} | ||
''', | ||
variables: <String, String>{ | ||
'ep': episodeToJson(episode), | ||
}, | ||
), | ||
builder: (QueryResult result, {BoolCallback refetch}) { | ||
if (result.errors != null) { | ||
return Text(result.errors.toString()); | ||
} | ||
|
||
if (result.loading) { | ||
return Center( | ||
child: const CircularProgressIndicator(), | ||
); | ||
} | ||
return Column( | ||
children: <Widget>[ | ||
Text(getPrettyJSONString(result.data)), | ||
RaisedButton( | ||
onPressed: () => print(refetch()), | ||
child: const Text('REFETCH'), | ||
), | ||
], | ||
); | ||
}, | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'dart:convert'; | ||
|
||
/// The episodes in the Star Wars trilogy | ||
enum Episode { | ||
NEWHOPE, | ||
EMPIRE, | ||
JEDI, | ||
} | ||
|
||
String episodeToJson(Episode e) { | ||
switch (e) { | ||
case Episode.NEWHOPE: | ||
return 'NEWHOPE'; | ||
case Episode.EMPIRE: | ||
return 'EMPIRE'; | ||
case Episode.JEDI: | ||
return 'JEDI'; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
Episode episodeFromJson(String e) { | ||
switch (e) { | ||
case 'NEWHOPE': | ||
return Episode.NEWHOPE; | ||
case 'EMPIRE': | ||
return Episode.EMPIRE; | ||
case 'JEDI': | ||
return Episode.JEDI; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
String getPrettyJSONString(Object jsonObject) { | ||
return const JsonEncoder.withIndent(' ').convert(jsonObject); | ||
} |
Oops, something went wrong.