Created
November 4, 2020 15:33
-
-
Save joaquinacuna/d220fec72f49c05ac3eb1db5585bfbd0 to your computer and use it in GitHub Desktop.
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
import 'package:flutter/material.dart'; | |
import "package:graphql_flutter/graphql_flutter.dart"; | |
import 'graphQLConf.dart'; | |
import 'queryMutation.dart'; | |
GraphQLConfiguration graphQLConfiguration = GraphQLConfiguration(); | |
void main() { | |
WidgetsFlutterBinding.ensureInitialized(); | |
runApp( GraphQLProvider( | |
client: graphQLConfiguration.client, | |
child: CacheProvider(child: MyApp()), | |
),); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Login Demo Asap', | |
theme: new ThemeData( | |
primarySwatch: Colors.red | |
), | |
home: new LoginPage(), | |
); | |
} | |
} | |
class LoginPage extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() => new _LoginPageState(); | |
} | |
// Used for controlling whether the user is loggin or creating an account | |
enum FormType { | |
login, | |
register | |
} | |
class _LoginPageState extends State<LoginPage> { | |
GraphQLConfiguration graphQLConfiguration = GraphQLConfiguration(); | |
QueryMutation addMutation = QueryMutation(); | |
final TextEditingController _emailFilter = new TextEditingController(); | |
final TextEditingController _passwordFilter = new TextEditingController(); | |
String _email = ""; | |
String _password = ""; | |
FormType _form = FormType.login; // our default setting is to login, and we should switch to creating an account when the user chooses to | |
_LoginPageState() { | |
_emailFilter.addListener(_emailListen); | |
_passwordFilter.addListener(_passwordListen); | |
} | |
void _emailListen() { | |
if (_emailFilter.text.isEmpty) { | |
_email = ""; | |
} else { | |
_email = _emailFilter.text; | |
} | |
} | |
void _passwordListen() { | |
if (_passwordFilter.text.isEmpty) { | |
_password = ""; | |
} else { | |
_password = _passwordFilter.text; | |
} | |
} | |
// Swap in between our two forms, registering and logging in | |
void _formChange () async { | |
setState(() { | |
if (_form == FormType.register) { | |
_form = FormType.login; | |
} else { | |
_form = FormType.register; | |
} | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: _buildBar(context), | |
body: new Container( | |
padding: EdgeInsets.all(16.0), | |
child: new Column( | |
children: <Widget>[ | |
_buildTextFields(), | |
_buildButtons(), | |
], | |
), | |
), | |
); | |
} | |
Widget _buildBar(BuildContext context) { | |
return new AppBar( | |
title: new Text("Simple Login ASAP"), | |
centerTitle: true, | |
); | |
} | |
Widget _buildTextFields() { | |
return new Container( | |
child: new Column( | |
children: <Widget>[ | |
new Container( | |
child: new TextField( | |
controller: _emailFilter, | |
decoration: new InputDecoration( | |
labelText: 'Email' | |
), | |
), | |
), | |
new Container( | |
child: new TextField( | |
controller: _passwordFilter, | |
decoration: new InputDecoration( | |
labelText: 'Password' | |
), | |
obscureText: true, | |
), | |
) | |
], | |
), | |
); | |
} | |
Widget _buildButtons() { | |
if (_form == FormType.login) { | |
return new Container( | |
child: new Column( | |
children: <Widget>[ | |
new RaisedButton( | |
child: new Text('Login'), | |
onPressed: _loginPressed, | |
), | |
new FlatButton( | |
child: new Text('Dont have an account? Tap here to register.'), | |
onPressed: _formChange, | |
), | |
new FlatButton( | |
child: new Text('Forgot Password?'), | |
onPressed: _passwordReset, | |
) | |
], | |
), | |
); | |
} else { | |
return new Container( | |
child: new Column( | |
children: <Widget>[ | |
new RaisedButton( | |
child: new Text('Create an Account'), | |
onPressed: _createAccountPressed, | |
), | |
new FlatButton( | |
child: new Text('Have an account? Click here to login.'), | |
onPressed: _formChange, | |
) | |
], | |
), | |
); | |
} | |
} | |
// These functions can self contain any user auth logic required, they all have access to _email and _password | |
void _loginPressed () async{ | |
print('The user wants to login with $_email and $_password'); | |
} | |
void _createAccountPressed () async { | |
print('The user wants to create an accoutn with $_email and $_password'); | |
GraphQLClient _client = graphQLConfiguration.clientToQuery(); | |
QueryResult result = await _client.mutate( | |
MutationOptions( | |
document: addMutation.register('$_email','','$_password'), | |
), | |
); | |
if (result.data["success"]!=null){ | |
print('The user was created succesfully!'); | |
}else{ | |
print('There was an error!'); | |
print(result.data["register"]["errors"]["email"][0]["message"]); | |
} | |
} | |
void _passwordReset () { | |
print("The user wants a password reset request sent to $_email"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment