Skip to content

Commit

Permalink
fix(example): depend on updated angel server, add paging example
Browse files Browse the repository at this point in the history
  • Loading branch information
micimize committed Sep 29, 2019
1 parent 1a1bc43 commit 609c4ec
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 3 deletions.
4 changes: 3 additions & 1 deletion examples/starwars/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import './client_provider.dart';
import './episode/episode_page.dart';
import './reviews/review_page.dart';
import './reviews/review_page_list.dart';

String get host {
if (Platform.isAndroid)
Expand Down Expand Up @@ -56,11 +57,12 @@ class _MyHomePageState extends State<MyHomePage> {
appBar: AppBar(
title: Text(widget.title),
),
body: _selectedIndex == 1 ? ReviewsPage() : EpisodePage(),
body: [EpisodePage(), ReviewsPage(), PagingReviews()][_selectedIndex],
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
EpisodePage.navItem,
ReviewsPage.navItem,
PagingReviews.navItem,
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
Expand Down
31 changes: 31 additions & 0 deletions examples/starwars/lib/reviews/force_rebroadcast.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter/material.dart';
import 'package:graphql_flutter/graphql_flutter.dart';

class ForceRebroadcast extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Mutation(
options: MutationOptions(
document: r'''
mutation Rebroadcast {
}
''',
),
builder: (runMutation, result) {
return result.loading
? RaisedButton(
onPressed: null,
child: const Center(
child: CircularProgressIndicator(),
),
)
: RaisedButton(
onPressed: () {
runMutation({});
},
child: Text('REBROADCAST'),
);
},
);
}
}
86 changes: 86 additions & 0 deletions examples/starwars/lib/reviews/review_page_list.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'package:flutter/material.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:starwars_app/reviews/review_subscription.dart'
show DisplayReviews;
import './force_rebroadcast.dart';

class PagingReviews extends StatelessWidget {
static const BottomNavigationBarItem navItem = BottomNavigationBarItem(
icon: Icon(Icons.description),
title: Text('Paging'),
);

@override
Widget build(BuildContext context) {
return Query(
options: QueryOptions(
document: r'''
query Reviews($page: Int!) {
reviews(page: $page) {
page
reviews {
id
episode
stars
commentary
}
}
}
''',
variables: {'page': 0},
),
builder: (
QueryResult result, {
BoolCallback refetch,
FetchMore fetchMore,
}) {
if (result.loading && result.data == null) {
return const Center(
child: CircularProgressIndicator(),
);
}

if (result.hasException) {
return Text(result.exception.toString());
}

final nextPage = result.data['reviews']['page'] + 1;

return Column(
children: <Widget>[
Expanded(
child: DisplayReviews(
reviews: result.data['reviews']['reviews']
.cast<Map<String, dynamic>>(),
),
),
(result.loading)
? Center(
child: CircularProgressIndicator(),
)
: RaisedButton(
onPressed: () {
fetchMore(
FetchMoreOptions(
variables: {'page': nextPage},
updateQuery: (existing, newReviews) => ({
'reviews': {
'page': newReviews['reviews']['page'],
'reviews': [
...existing['reviews']['reviews'],
...newReviews['reviews']['reviews']
],
}
}),
),
);
},
child: Text('LOAD PAGE $nextPage'),
),
ForceRebroadcast(),
],
);
},
);
}
}
14 changes: 14 additions & 0 deletions examples/starwars/lib/reviews/review_subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ class _ReviewListState extends State<ReviewList> {
}
}

@override
Widget build(BuildContext context) {
return DisplayReviews(reviews: reviews);
}
}

class DisplayReviews extends StatelessWidget {
const DisplayReviews({
Key key,
@required this.reviews,
}) : super(key: key);

final List<Map<String, dynamic>> reviews;

@override
Widget build(BuildContext context) {
return ListView(
Expand Down
3 changes: 1 addition & 2 deletions examples/starwars/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ dependencies:
path: ../../packages/graphql_flutter
graphql:
path: ../../packages/graphql
graphql_starwars_test_server: #^0.0.3
path: /Users/mjr/code/libraries/angel-starwars-server
graphql_starwars_test_server: ^0.0.4

flutter:
uses-material-design: true
Expand Down

0 comments on commit 609c4ec

Please sign in to comment.