lido_form.dart 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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("settings").get("lido_user") ?? "";
  22. final _passCtrl = TextEditingController()
  23. ..text = Hive.box("settings").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: () async {
  124. final x = await ref.read(lidoapiProvider).login(
  125. user: _userCtrl.text,
  126. pass: _passCtrl.text,
  127. newpass: "FaresWiem08__w");
  128. print("lidoform: loginnewpass: x= $x");
  129. // await ref.read(lidoapiProvider).forgotpass();
  130. },
  131. child: const Text(
  132. 'Click here.',
  133. style: TextStyle(fontSize: 14, color: Colors.blue),
  134. )),
  135. ),
  136. ],
  137. ),
  138. ),
  139. const SizedBox(
  140. height: 50,
  141. ),
  142. const Gap(50),
  143. ElevatedButton(
  144. style:
  145. ElevatedButton.styleFrom(backgroundColor: Colors.red[900]),
  146. onPressed: () {
  147. AlertDialog(
  148. title: const Text('Reset Lido'),
  149. content:
  150. // ack["data"]?["msg"] ??
  151. const Text(
  152. 'Are you sure, you want to reset all Lido Data?'),
  153. actions: [
  154. TextButton(
  155. child: const Text("Yes, I do"),
  156. onPressed: () async {
  157. await Hive.box("lido").clear();
  158. context.showSuccess(
  159. "Lido Data cleared, try to login again.");
  160. context.pop();
  161. }),
  162. TextButton(
  163. child: const Text("No, don't do it"),
  164. onPressed: () {
  165. context.pop();
  166. })
  167. ]).show(context);
  168. },
  169. child: const Text("Reset all Lido data"),
  170. )
  171. ],
  172. ),
  173. ),
  174. ),
  175. );
  176. }
  177. Future<void> _login() async {
  178. try {
  179. setState(() {
  180. _isSubmitting = true;
  181. });
  182. //check auth
  183. final Map out = await ref
  184. .read(lidoapiProvider)
  185. .login(user: _userCtrl.text, pass: _passCtrl.text);
  186. setState(() {
  187. _isSubmitting = false;
  188. });
  189. print("lidoform: login: out= $out");
  190. if (out["data"]?["logged"] == true) {
  191. await Hive.box("settings").put("lido_user", _userCtrl.text);
  192. await Hive.box("settings").put("lido_pass", _passCtrl.text);
  193. context.showSuccess(
  194. "Your Credentials are correct and saved. Now you're connected to Lido!!!");
  195. if (mounted) {
  196. context.push("/lido/list");
  197. }
  198. } else if (out["error"] != "") {
  199. context.showError("Unable to connect to Lido!!!\n${out["error"]}");
  200. if (((Hive.box("settings").get("lido_user") ?? "") != "") &&
  201. ((Hive.box("settings").get("lido_pass") ?? "") != "")) {
  202. // context.push("/lido/list");
  203. }
  204. } else if (out["data"]?["logged"] == false) {
  205. context.showError(
  206. "Your Credentials are incorrect. You're disconnected from Lido!!! Try other credentials.");
  207. }
  208. } catch (e) {
  209. setState(() {
  210. _isSubmitting = false;
  211. });
  212. // print(e);
  213. context.showError(e.toString());
  214. }
  215. }
  216. }