You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
4.8 KiB
152 lines
4.8 KiB
import 'package:flutter/material.dart'; |
|
import 'package:odoo_contact_app/config.dart'; |
|
import 'secureStorage.dart'; |
|
import 'graphqlScript.dart'; |
|
import 'homepage.dart'; |
|
import 'dart:convert'; |
|
|
|
|
|
|
|
void main() { |
|
runApp(MyApp()); |
|
} |
|
|
|
class MyApp extends StatelessWidget { |
|
@override |
|
Widget build(BuildContext context) { |
|
return MaterialApp( |
|
debugShowCheckedModeBanner: false, |
|
home: LoginPage(), |
|
); |
|
} |
|
} |
|
|
|
class LoginPage extends StatefulWidget { |
|
@override |
|
_LoginPageState createState() => _LoginPageState(); |
|
} |
|
|
|
class _LoginPageState extends State<LoginPage> { |
|
TextEditingController _loginFieldController = TextEditingController(); |
|
TextEditingController _passwordFieldController = TextEditingController(); |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
return Scaffold( |
|
backgroundColor: Colors.white, |
|
appBar: AppBar( |
|
title: Text("Login Page"), |
|
), |
|
body: SingleChildScrollView( |
|
child: Column( |
|
children: <Widget>[ |
|
Padding( |
|
padding: const EdgeInsets.only(top: 60.0), |
|
child: Center( |
|
child: Container( |
|
width: 200, |
|
height: 150, |
|
/*decoration: BoxDecoration( |
|
color: Colors.red, |
|
borderRadius: BorderRadius.circular(50.0)),*/ |
|
child: Image.network('https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg')), |
|
), |
|
), |
|
Padding( |
|
//padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), |
|
padding: EdgeInsets.symmetric(horizontal: 15), |
|
child: TextField( |
|
controller: _loginFieldController, |
|
decoration: InputDecoration( |
|
border: OutlineInputBorder(), |
|
labelText: 'Login', |
|
hintText: 'Enter Login'), |
|
), |
|
), |
|
Padding( |
|
padding: const EdgeInsets.only( |
|
left: 15.0, right: 15.0, top: 15, bottom: 0), |
|
//padding: EdgeInsets.symmetric(horizontal: 15), |
|
child: TextField( |
|
controller: _passwordFieldController, |
|
obscureText: true, |
|
decoration: InputDecoration( |
|
border: OutlineInputBorder(), |
|
labelText: 'Password', |
|
hintText: 'Enter password'), |
|
), |
|
), |
|
TextButton( |
|
onPressed: (){ |
|
//TODO FORGOT PASSWORD SCREEN GOES HERE |
|
}, |
|
child: Text( |
|
'Forgot Password', |
|
style: TextStyle(color: Colors.blue, fontSize: 15), |
|
), |
|
), |
|
Container( |
|
height: 50, |
|
width: 250, |
|
decoration: BoxDecoration( |
|
color: Colors.blue, borderRadius: BorderRadius.circular(20)), |
|
child: TextButton( |
|
onPressed: () async { |
|
final loginRequest = makeApiLoginRequest( |
|
username: _loginFieldController.text, password: _passwordFieldController.text, |
|
baseEndpoint: BaseEndpoint, authEndpoint: AuthEndpoint |
|
); |
|
// Retrieve API key from secure storage |
|
loginRequest.then((value){ |
|
if (value.statusCode == 200) { |
|
final String apikey = json.decode(value.body)['your-api-key']; |
|
userCreds.write(key: 'api-key', value: apikey).then((val) { |
|
Navigator.pushReplacement( |
|
context, MaterialPageRoute(builder: (_) => HomePage(apikey: apikey))); |
|
},); |
|
} |
|
else { |
|
showAlertBox(); |
|
} |
|
}).catchError((onError){ |
|
showAlertBox(); |
|
}); |
|
}, |
|
child: Text( |
|
'Login', |
|
style: TextStyle(color: Colors.white, fontSize: 25), |
|
), |
|
), |
|
), |
|
SizedBox( |
|
height: 130, |
|
), |
|
Text('New User? Create Account') |
|
], |
|
), |
|
), |
|
); |
|
} |
|
|
|
showAlertBox() { |
|
return showDialog( |
|
context: context, |
|
builder: (ctx) => AlertDialog( |
|
title: const Text("Invalid Credentials"), |
|
content: const Text("Provide correct login and password"), |
|
actions: <Widget>[ |
|
TextButton( |
|
onPressed: () { |
|
Navigator.of(ctx).pop(); |
|
}, |
|
child: Container( |
|
color: Color.fromARGB(255, 247, 148, 0), |
|
padding: const EdgeInsets.all(14), |
|
child: const Text("Ok"), |
|
), |
|
), |
|
], |
|
), |
|
); |
|
} |
|
}
|
|
|