-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
330 lines (296 loc) · 9.55 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Import MaterialApp and other widgets which we can use to quickly create a material app
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
// Code written in Dart starts exectuting from the main function. runApp is part of
// Flutter, and requires the component which will be our app's container. In Flutter,
// every component is known as a "widget".
// void main() => runApp(MyApp());
void main() {
// ValueNotifier<Client> client = ValueNotifier(
// Client(
// endPoint: 'https://hasurainternship.herokuapp.com/v1alpha1/graphql',
// cache: InMemoryCache(),
// apiToken: '',
// ),
// );
//to run the application
runApp(MyApp());
}
ValueNotifier<Client> client = ValueNotifier(
Client(
endPoint: 'https://hasurainternship.herokuapp.com/v1alpha1/graphql',
cache: InMemoryCache(),
apiToken: '',
),
);
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GraphqlProvider(
client: client,
child: MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
),
);
}
}
//for normal query
String readTasks = """
query{
todolist{
id
tasks
}
}
""" .replaceAll('\n', ' ');
//for mutation
//change this accordingly to perform the action when the button is pressed and also to store the value which goes not some random text
String mutation = """
mutation addTask(\$id: ID!) {
addtask(input: {newtask: \$newtask}){
todolist{
tasks
}
}
}
""".replaceAll('\n', ' ');
class MyHomePage extends StatelessWidget{
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = new GoogleSignIn();
Future<FirebaseUser> _signIn() async{
GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
GoogleSignInAuthentication gSA = await googleSignInAccount.authentication;
FirebaseUser user = await _auth.signInWithGoogle(
idToken: gSA.idToken,
accessToken: gSA.accessToken
);
print("Username: ${user.displayName}");
return user;
}
void _signOut(){
googleSignIn.signOut();
print("User Signed Out");
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text('Google Firebase Authentication'),
),
body: new Padding(
padding: const EdgeInsets.all(20.0),
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new RaisedButton(
onPressed: (){
_signIn().then((FirebaseUser user)=> print(user)).catchError((e)=>print(e));
Navigator.push(context,MaterialPageRoute(builder: (context) => TodoList()),);
},
child: new Text('Sign In'),
color: Colors.green,
shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)),
),
new Padding(
padding: const EdgeInsets.all(10.0),
),
new RaisedButton(
onPressed: _signOut,
child: new Text('Sign Out'),
color: Colors.red,
shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)),
),
],
),
),
);
}
}
class TodoList extends StatefulWidget {
@override
createState() => new TodoListState();
}
class TodoListState extends State<TodoList> {
List<String> _todoItems = [];
void _addTodoItem(String task) {
// Only add the task if the user actually entered something
if(task.length > 0) {
// Putting our code inside "setState" tells the app that our state has changed, and
// it will automatically re-render the list
setState(() => _todoItems.add(task));
// //also try to add the mutation here.**
// Mutation(
// mutation,
// builder: (
// runMutation,{
// bool loading,
// var data,
// Exception error,
// }){
// return FloatingActionButton(
// onPressed: ()=>runMutation({
// 'newtask': task,
// })
// );
// },
// onCompleted: (Map<String, dynamic> data){
// showDialog(
// context: context,
// builder: (BuildContext context){
// return AlertDialog(
// title: Text('Task added'),
// actions: <Widget>[
// SimpleDialogOption(
// child: Text('Dismiss'),
// onPressed: (){
// Navigator.of(context).pop();
// },
// )
// ],
// );
// }
// );
// }
// );
}
}
void _removeTodoItem(int index) {
setState(() => _todoItems.removeAt(index));
}
void _promptRemoveTodoItem(int index) {
showDialog(
context: context,
builder: (BuildContext context) {
return new AlertDialog(
title: new Text('Mark "${_todoItems[index]}" as done?'),
actions: <Widget>[
new FlatButton(
child: new Text('CANCEL'),
// The alert is actually part of the navigation stack, so to close it, we
// need to pop it.
onPressed: () => Navigator.of(context).pop()
),
new FlatButton(
child: new Text('MARK AS DONE'),
onPressed: () {
_removeTodoItem(index);
Navigator.of(context).pop();
}
)
]
);
}
);
}
// Build the whole list of todo items
Widget _buildTodoList() {
return new ListView.builder(
itemBuilder: (context, index) {
// itemBuilder will be automatically be called as many times as it takes for the
// list to fill up its available space, which is most likely more than the
// number of todo items we have. So, we need to check the index is OK.
if(index < _todoItems.length) {
return _buildTodoItem(_todoItems[index], index);
}
},
);
}
// Build a single todo item
Widget _buildTodoItem(String todoText, int index) {
return new ListTile(
title: new Text(todoText),
onTap: () => _promptRemoveTodoItem(index)
);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Todo List'),
actions: <Widget>[
new FlatButton(
child: new Text('SignOut'),
color: Colors.blue,
textColor: Colors.white,
onPressed: (){
Navigator.of(context).pop();
},
),
// Query(
// readTasks,
// builder: ({
// bool loading,
// var data,
// Exception error,
// }){
// if (error != null){
// return Text(error.toString());
// }
// if (loading){
// return Text('Loading');
// }
// List tasks = data['tasks'];
// return ListView.builder(
// itemCount: tasks.length,
// itemBuilder: (context,index){
// final taskList = tasks[index];
// return Text(taskList['name']);
// }
// );
// }
// ),
],
),
body: _buildTodoList(),
floatingActionButton: new FloatingActionButton(
onPressed: _pushAddTodoScreen,
tooltip: 'Add task',
child: new Icon(Icons.add)
),
);
}
void _pushAddTodoScreen() {
// Push this page onto the stack
Navigator.of(context).push(
// MaterialPageRoute will automatically animate the screen entry, as well as adding
// a back button to close it
new MaterialPageRoute(
builder: (context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Add a new task')
),
body: new TextField(
autofocus: true,
onSubmitted: (val) {
_addTodoItem(val);
Navigator.pop(context); // Close the add todo screen
},
decoration: new InputDecoration(
hintText: 'Enter something to do...',
contentPadding: const EdgeInsets.all(16.0)
),
)
);
}
)
);
}
}