-
-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from zino-app/providers-1
`GraphqlProvider` and `GraphqlConsumer` widgets
- Loading branch information
Showing
10 changed files
with
208 additions
and
84 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
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
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,23 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
import 'package:graphql_flutter/src/client.dart'; | ||
import 'package:graphql_flutter/src/widgets/graphql_provider.dart'; | ||
|
||
typedef Widget GraphqlConsumerBuilder(Client client); | ||
|
||
class GraphqlConsumer extends StatelessWidget { | ||
GraphqlConsumer({ | ||
final Key key, | ||
@required this.builder, | ||
}) : super(key: key); | ||
|
||
final GraphqlConsumerBuilder builder; | ||
|
||
Widget build(BuildContext context) { | ||
/// Gets the client from the closest wrapping [GraphqlProvider]. | ||
Client client = GraphqlProvider.of(context).value; | ||
assert(client != null); | ||
|
||
return builder(client); | ||
} | ||
} |
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,66 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
import 'package:graphql_flutter/src/client.dart'; | ||
|
||
class GraphqlProvider extends StatefulWidget { | ||
const GraphqlProvider({ | ||
Key key, | ||
this.client, | ||
this.child, | ||
}) : super(key: key); | ||
|
||
final ValueNotifier<Client> client; | ||
final Widget child; | ||
|
||
static ValueNotifier<Client> of(BuildContext context) { | ||
_InheritedGraphqlProvider inheritedGraphqlProvider = | ||
context.inheritFromWidgetOfExactType(_InheritedGraphqlProvider); | ||
|
||
return inheritedGraphqlProvider.client; | ||
} | ||
|
||
@override | ||
State<StatefulWidget> createState() => new _GraphqlProviderState(); | ||
} | ||
|
||
class _GraphqlProviderState extends State<GraphqlProvider> { | ||
void didValueChange() => setState(() {}); | ||
|
||
@override | ||
initState() { | ||
super.initState(); | ||
|
||
widget.client.addListener(didValueChange); | ||
} | ||
|
||
@override | ||
dispose() { | ||
widget.client?.removeListener(didValueChange); | ||
|
||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return new _InheritedGraphqlProvider( | ||
client: widget.client, | ||
child: widget.child, | ||
); | ||
} | ||
} | ||
|
||
class _InheritedGraphqlProvider extends InheritedWidget { | ||
_InheritedGraphqlProvider({ | ||
this.client, | ||
this.child, | ||
}) : clientValue = client.value; | ||
|
||
final ValueNotifier<Client> client; | ||
final Widget child; | ||
final Client clientValue; | ||
|
||
@override | ||
bool updateShouldNotify(_InheritedGraphqlProvider oldWidget) { | ||
return clientValue != oldWidget.clientValue; | ||
} | ||
} |
Oops, something went wrong.