Advertisement
RaphCpp

Untitled

Apr 12th, 2022
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. import 'dart:io';
  2. import 'package:device_info/device_info.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:running_app/services/auth.dart';
  6. import 'package:running_app/view/view_register.dart';
  7.  
  8. class LoginView extends StatefulWidget {
  9. @override
  10. _LoginViewState createState() => _LoginViewState();
  11. }
  12.  
  13. class _LoginViewState extends State<LoginView> {
  14. TextEditingController _emailController = TextEditingController();
  15. TextEditingController _passwordController = TextEditingController();
  16. final _formKey = GlobalKey<FormState>();
  17. DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  18. late String _deviceName;
  19.  
  20.  
  21. @override
  22. void initState(){
  23. getDeviceName();
  24. super.initState();
  25. }
  26.  
  27.  
  28. void getDeviceName() async {
  29. try {
  30. if (Platform.isAndroid) {
  31. AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
  32. _deviceName = androidInfo.model;
  33. } else if (Platform.isIOS) {
  34. IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
  35. _deviceName = iosInfo.utsname.machine;
  36. }
  37. } catch (e) {
  38. print(e);
  39. }
  40. }
  41.  
  42.  
  43. void dispose(){
  44. _emailController.dispose();
  45. _passwordController.dispose();
  46. super.dispose();
  47. }
  48. Widget build(BuildContext context) {
  49. return Scaffold(
  50. body: Padding(
  51. padding: const EdgeInsets.all(20.0),
  52. child: Form(
  53. key: _formKey,
  54. child: Column(
  55. mainAxisAlignment: MainAxisAlignment.center,
  56. children: [
  57. TextFormField(
  58. controller: _emailController,
  59. validator: (value) => value!.isEmpty ? 'please enter valid email' : null
  60. ),
  61. TextFormField(
  62. controller: _passwordController,
  63. validator: (value) => value!.isEmpty ? 'please enter valid password' : null
  64. ),
  65. SizedBox(height: 10,),
  66. FlatButton(
  67. minWidth: double.infinity,
  68. color: Colors.blue,
  69. child: Text('Login', style: TextStyle(color: Colors.white)),
  70. onPressed: (){
  71. Map creds = {
  72. 'email' : _emailController.text,
  73. 'password' : _passwordController.text,
  74. 'device_name' : _deviceName ?? 'unknown',
  75. };
  76. if(_formKey.currentState!.validate()){
  77. Provider.of<Auth>(context, listen: false).login(creds: creds);
  78. Navigator.pop(context);
  79. }
  80. },
  81. ),
  82. FlatButton(
  83. minWidth: double.infinity,
  84. color: Colors.blue,
  85. child: Text('Create account', style: TextStyle(color: Colors.white)),
  86. onPressed: () {
  87. Navigator.of(context).push(MaterialPageRoute(builder: (context)=>RegisterView()));
  88. },
  89. )
  90. ],
  91. ),
  92. ),
  93. )
  94. );
  95. }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement