-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.dart
116 lines (105 loc) · 2.96 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
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:flutter/material.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:flutter_app_poseidon/signin.dart';
import 'package:flutter_app_poseidon/signup.dart';
import 'package:flutter_login/flutter_login.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'amplifyconfiguration.dart';
void main() {
runApp(MaterialApp(
home: MyApp(),
routes: {
'/secondpage': (_) => SignupPage(),
},
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _amplifyConfigured = false;
Duration get loginTime => Duration(milliseconds: 2250);
bool _isSignin = false;
String email;
// Instantiate Amplify
Amplify amplifyInstance = Amplify();
final emailController = TextEditingController();
final passwordController = TextEditingController();
@override
void initState() {
super.initState();
if (!_amplifyConfigured) _configureAmplify();
}
void _configureAmplify() async {
if (!mounted) return;
AmplifyAuthCognito authPlugin = AmplifyAuthCognito();
amplifyInstance.addPlugin(authPlugins: [authPlugin]);
// Once Plugins are added, configure Amplify
await amplifyInstance.configure(amplifyconfig);
try {
setState(() {
_amplifyConfigured = true;
print('amplify configured: $_amplifyConfigured');
});
} catch (e) {
print(e);
}
}
Future<String> _signupUser(LoginData data) async {
Map<String, dynamic> userAttributes = {
'email': emailController.text,
};
SignUpResult res = await Amplify.Auth.signUp(
username: data.name,
password: data.password,
options: CognitoSignUpOptions(userAttributes: userAttributes));
print(res);
if (res.isSignUpComplete) {
Fluttertoast.showToast(
toastLength: Toast.LENGTH_SHORT,
msg: 'Signup successfully!',
backgroundColor: Colors.blue,
textColor: Colors.white);
return null;
}
return 'error';
}
Future<String> _authUser(LoginData data) async {
SignInResult res = await Amplify.Auth.signIn(
username: data.name,
password: data.password,
);
if (res.isSignedIn) {
_isSignin = true;
email = data.name;
return null;
}
return 'error';
}
void _gotoSecondPage() async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (_) => _isSignin
? SigninPage(
email: email,
)
: SignupPage()),
);
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: FlutterLogin(
title: 'POSEIDON',
logo: 'assets/poseidon.jpg',
onLogin: _authUser,
onSignup: _signupUser,
onSubmitAnimationCompleted: _gotoSecondPage,
onRecoverPassword: (_) => null,
),
);
}
}