lido_form.dart 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // ignore_for_file: use_build_context_synchronously
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:gap/gap.dart';
  5. import 'package:go_router/go_router.dart';
  6. import 'package:hive_flutter/hive_flutter.dart';
  7. import 'package:tp5/core/basic_page.dart';
  8. import 'package:tp5/core/core.dart';
  9. import 'package:tp5/core/utils.dart';
  10. import 'package:tp5/lido/lido_api.dart';
  11. class LidoForm extends ConsumerStatefulWidget {
  12. const LidoForm({super.key});
  13. @override
  14. createState() => _LidoFormState();
  15. }
  16. class _LidoFormState extends ConsumerState<LidoForm> {
  17. final _formKey = GlobalKey<FormState>();
  18. AutovalidateMode? _autovalidateMode;
  19. bool _isSubmitting = false;
  20. final _userCtrl = TextEditingController()
  21. ..text = Hive.box("profile").get("lido_user") ?? "";
  22. final _passCtrl = TextEditingController()
  23. ..text = Hive.box("profile").get("lido_pass") ?? "";
  24. @override
  25. Widget build(BuildContext context) {
  26. return BasicPage(
  27. title: "Lido / Connect",
  28. body: SingleChildScrollView(
  29. child: Form(
  30. key: _formKey,
  31. autovalidateMode: _autovalidateMode,
  32. child: Column(
  33. children: <Widget>[
  34. const Padding(
  35. padding: EdgeInsets.all(20),
  36. child: Center(
  37. child: Text("Lido Connect", style: TextStyle(fontSize: 40)),
  38. ),
  39. ),
  40. Padding(
  41. //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0),
  42. padding: const EdgeInsets.symmetric(horizontal: 15),
  43. child: TextFormField(
  44. controller: _userCtrl,
  45. readOnly: _isSubmitting,
  46. validator: (value) {
  47. if (value == null || value.isEmpty) {
  48. return 'Please enter your Lido username';
  49. }
  50. return null;
  51. },
  52. decoration: const InputDecoration(
  53. border: OutlineInputBorder(),
  54. labelText: 'Lido Username',
  55. hintText: 'matricule'),
  56. ),
  57. ),
  58. Padding(
  59. padding: const EdgeInsets.only(
  60. left: 15.0, right: 15.0, top: 15, bottom: 0),
  61. //padding: EdgeInsets.symmetric(horizontal: 15),
  62. child: TextFormField(
  63. controller: _passCtrl,
  64. readOnly: _isSubmitting,
  65. obscureText: true,
  66. validator: (value) {
  67. if (value == null || value.isEmpty) {
  68. return 'Please enter your Lido password';
  69. }
  70. return null;
  71. },
  72. decoration: const InputDecoration(
  73. border: OutlineInputBorder(),
  74. labelText: 'Lido Password',
  75. hintText: 'Enter Lido Password'),
  76. ),
  77. ),
  78. SizedBox(
  79. //height: 65,
  80. width: 360,
  81. child: Padding(
  82. padding: const EdgeInsets.only(top: 20.0),
  83. child: ElevatedButton(
  84. style: ElevatedButton.styleFrom(
  85. backgroundColor: Colors.green[900]),
  86. onPressed: _isSubmitting
  87. ? null
  88. : () {
  89. if (_formKey.currentState!.validate()) {
  90. _login();
  91. } else {
  92. setState(() {
  93. _autovalidateMode = AutovalidateMode.always;
  94. });
  95. }
  96. },
  97. child: Padding(
  98. padding: const EdgeInsets.all(8.0),
  99. child: _isSubmitting
  100. ? const CircularProgressIndicator()
  101. : const Text(
  102. 'Connect to Lido',
  103. style:
  104. TextStyle(color: Colors.white, fontSize: 20),
  105. ),
  106. ),
  107. ),
  108. ),
  109. ),
  110. const SizedBox(
  111. height: 20,
  112. ),
  113. Center(
  114. child: Row(
  115. children: [
  116. const Padding(
  117. padding: EdgeInsets.only(left: 62),
  118. child: Text('Forgot your login details? '),
  119. ),
  120. Padding(
  121. padding: const EdgeInsets.only(left: 1.0),
  122. child: InkWell(
  123. onTap: () {},
  124. child: const Text(
  125. 'Contact your company \nLido administrator.',
  126. style: TextStyle(fontSize: 14, color: Colors.blue),
  127. )),
  128. ),
  129. ],
  130. ),
  131. ),
  132. const SizedBox(
  133. height: 50,
  134. ),
  135. const Gap(50),
  136. ElevatedButton(
  137. style:
  138. ElevatedButton.styleFrom(backgroundColor: Colors.red[900]),
  139. onPressed: () {
  140. AlertDialog(
  141. title: const Text('Reset Lido'),
  142. content:
  143. // ack["data"]?["msg"] ??
  144. const Text(
  145. 'Are you sure, you want to reset all Lido Data?'),
  146. actions: [
  147. TextButton(
  148. child: const Text("Yes, I do"),
  149. onPressed: () async {
  150. await Hive.box("lido").clear();
  151. context.showSuccess(
  152. "Lido Data cleared, try to login again.");
  153. context.pop();
  154. }),
  155. TextButton(
  156. child: const Text("No, don't do it"),
  157. onPressed: () {
  158. context.pop();
  159. })
  160. ]).show(context);
  161. },
  162. child: const Text("Reset all Lido data"),
  163. )
  164. ],
  165. ),
  166. ),
  167. ),
  168. );
  169. }
  170. Future<void> _login() async {
  171. try {
  172. setState(() {
  173. _isSubmitting = true;
  174. });
  175. //check auth
  176. final Map out = await ref
  177. .read(lidoapiProvider)
  178. .login(user: _userCtrl.text, pass: _passCtrl.text);
  179. setState(() {
  180. _isSubmitting = false;
  181. });
  182. print("lidoform: login: out= $out");
  183. if (out["data"]?["logged"] == true) {
  184. await Hive.box("profile").put("lido_user", _userCtrl.text);
  185. await Hive.box("profile").put("lido_pass", _passCtrl.text);
  186. context.showSuccess(
  187. "Your Credentials are correct and saved. Now you're connected to Lido!!!");
  188. if (mounted) {
  189. context.push("/lido/list");
  190. }
  191. } else if (out["error"] != "") {
  192. context.showError("Unable to connect to Lido!!!\n${out["error"]}");
  193. if (((Hive.box("profile").get("lido_user") ?? "") != "") &&
  194. ((Hive.box("profile").get("lido_pass") ?? "") != "")) {
  195. // context.push("/lido/list");
  196. }
  197. } else if (out["data"]?["logged"] == false) {
  198. context.showError(
  199. "Your Credentials are incorrect. You're disconnected from Lido!!! Try other credentials.");
  200. }
  201. } catch (e) {
  202. setState(() {
  203. _isSubmitting = false;
  204. });
  205. // print(e);
  206. context.showError(e.toString());
  207. }
  208. }
  209. }