1
0

crewlink_form.dart 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/roster/api/crewlink_api.dart';
  11. class CrewlinkForm extends ConsumerStatefulWidget {
  12. const CrewlinkForm({super.key});
  13. @override
  14. createState() => _CrewlinkFormState();
  15. }
  16. class _CrewlinkFormState extends ConsumerState<CrewlinkForm> {
  17. final _formKey = GlobalKey<FormState>();
  18. AutovalidateMode? _autovalidateMode;
  19. bool _isSubmitting = false;
  20. final _userCtrl = TextEditingController()
  21. ..text = Hive.box("settings").get("crewlink_user") ?? "";
  22. final _passCtrl = TextEditingController()
  23. ..text = Hive.box("settings").get("crewlink_pass") ?? "";
  24. @override
  25. Widget build(BuildContext context) {
  26. return BasicPage(
  27. title: "CrewLink / 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:
  38. Text("CrewLink 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 CrewLink username';
  50. }
  51. return null;
  52. },
  53. decoration: const InputDecoration(
  54. border: OutlineInputBorder(),
  55. labelText: 'CrewLink 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 CrewLink password';
  70. }
  71. return null;
  72. },
  73. decoration: const InputDecoration(
  74. border: OutlineInputBorder(),
  75. labelText: 'CrewLink Password',
  76. hintText: 'Enter CrewLink 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 CrewLink',
  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: () {},
  125. child: const Text(
  126. 'Contact your company \nCrewLink administrator.',
  127. style: TextStyle(fontSize: 14, color: Colors.blue),
  128. )),
  129. ),
  130. ],
  131. ),
  132. ),
  133. const SizedBox(
  134. height: 50,
  135. ),
  136. const Gap(50),
  137. ElevatedButton(
  138. style:
  139. ElevatedButton.styleFrom(backgroundColor: Colors.red[900]),
  140. onPressed: () {
  141. AlertDialog(
  142. title: const Text('Reset CrewLink'),
  143. content:
  144. // ack["data"]?["msg"] ??
  145. const Text(
  146. 'Are you sure, you want to reset all CrewLink Data?'),
  147. actions: [
  148. TextButton(
  149. child: const Text("Yes, I do"),
  150. onPressed: () async {
  151. await Hive.box("crewlink").clear();
  152. context.showSuccess(
  153. "CrewLink Data cleared, try to login again.");
  154. context.pop();
  155. }),
  156. TextButton(
  157. child: const Text("No, don't do it"),
  158. onPressed: () {
  159. context.pop();
  160. })
  161. ]).show(context);
  162. },
  163. child: const Text("Reset all Crewlink data"),
  164. )
  165. ],
  166. ),
  167. ),
  168. ),
  169. );
  170. }
  171. Future<void> _login() async {
  172. try {
  173. setState(() {
  174. _isSubmitting = true;
  175. });
  176. //check auth
  177. final Map out = await ref
  178. .read(crewlinkapiProvider)
  179. .login(username: _userCtrl.text, password: _passCtrl.text);
  180. setState(() {
  181. _isSubmitting = false;
  182. });
  183. if (out["data"]?["logged"] == true) {
  184. await Hive.box("settings").put("crewlink_user", _userCtrl.text);
  185. await Hive.box("settings").put("crewlink_pass", _passCtrl.text);
  186. context.showSuccess(
  187. "Your Credentials are correct and saved. Now you're connected to CrewLink!!!");
  188. if (mounted) {
  189. context.push("/crewlink/roster");
  190. }
  191. } else if (out["error"] != "") {
  192. context.showError("Unable to connect to CrewLink!!!\n${out["error"]}");
  193. if (((Hive.box("settings").get("crewlink_user") ?? "") != "") &&
  194. ((Hive.box("settings").get("crewlink_pass") ?? "") != "")) {
  195. //context.push("/crewlink/roster");
  196. }
  197. } else if (out["data"]?["logged"] == false) {
  198. context.showError(
  199. "Your Credentials are incorrect. You're disconnected from CrewLink!!! Try other credentials.");
  200. }
  201. } catch (e) {
  202. setState(() {
  203. _isSubmitting = false;
  204. });
  205. // print(e);
  206. context.showError(e.toString());
  207. }
  208. }
  209. }