lido_form.dart 9.0 KB

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