aakash2310

Untitled

Dec 29th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.81 KB | None | 0 0
  1. import 'dart:convert';
  2. import 'dart:io';
  3.  
  4. import 'package:flutter/material.dart';
  5. import 'package:get/get.dart';
  6. import 'package:google_fonts/google_fonts.dart';
  7. import 'package:vicharkranti/colors/Colors.dart';
  8. import 'package:vicharkranti/helper/strings.dart';
  9. import 'package:vicharkranti/main.dart';
  10. import 'package:vicharkranti/screens/LoginScreen.dart';
  11. import 'package:http/http.dart' as http;
  12.  
  13. class SignupScreen extends StatefulWidget {
  14. const SignupScreen({Key? key}) : super(key: key);
  15.  
  16. @override
  17. State<SignupScreen> createState() => _SignupScreenState();
  18. }
  19.  
  20. class _SignupScreenState extends State<SignupScreen> {
  21. TextEditingController emailController = TextEditingController();
  22. TextEditingController passwordController = TextEditingController();
  23. TextEditingController confirmpasswordController = TextEditingController();
  24. TextEditingController usernameController = TextEditingController();
  25. TextEditingController mobilenumberController = TextEditingController();
  26. bool isValidated = false;
  27. bool isloading = false;
  28. bool isPasswordVisible1 = false;
  29. bool isPasswordVisible2 = false;
  30. final _formkey = GlobalKey<FormState>();
  31.  
  32. //api
  33.  
  34. signup() async {
  35. setState(() {
  36. isValidated = true;
  37. });
  38. if (_formkey.currentState!.validate()) {
  39. setState(() {
  40. isloading = true;
  41. });
  42. try {
  43. var data = {
  44. "username": usernameController.text,
  45. "email": emailController.text,
  46. "mobileno": mobilenumberController.text,
  47. "password": passwordController.text,
  48. };
  49. final response = await http.post(
  50. Uri.parse(BaseUrl + "registration"),
  51. body: jsonEncode(data),
  52. );
  53.  
  54. setState(() {
  55. isloading = false;
  56. });
  57. print(response.statusCode);
  58.  
  59. if (response.statusCode == 200) {
  60. var resData = jsonDecode(response.body);
  61. //
  62.  
  63. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  64. //backgroundColor: grey300,
  65. duration: const Duration(seconds: 3),
  66. content: Text(
  67. resData["message"], //
  68. style: GoogleFonts.kameron(
  69. fontSize: 15,
  70. ),
  71. ),
  72. behavior: SnackBarBehavior.floating,
  73. margin: const EdgeInsets.all(10.0),
  74. //animation:,
  75. ));
  76.  
  77. if (resData["status"] == "true") {
  78. //
  79. Navigator.of(context).pushAndRemoveUntil(
  80. MaterialPageRoute(builder: (_) => const LoginScreen()),
  81. (route) => false);
  82. }
  83. }
  84. } on SocketException {
  85. // TODO
  86. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  87. //backgroundColor: grey300,
  88. duration: const Duration(seconds: 3),
  89. content: Text(
  90. "Internet connection not available.", //
  91. style: GoogleFonts.kameron(
  92. fontSize: 15,
  93. ),
  94. ),
  95. behavior: SnackBarBehavior.floating,
  96. margin: const EdgeInsets.all(25.0),
  97. //animation:,
  98. ));
  99. setState(() {
  100. isloading = false;
  101. });
  102. } on HttpException {
  103. //message
  104. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  105. //backgroundColor: grey300,
  106. duration: const Duration(seconds: 3),
  107. content: Text(
  108. "Something went wrong. Please try again later.", //
  109. style: GoogleFonts.kameron(
  110. fontSize: 15,
  111. ),
  112. ),
  113. behavior: SnackBarBehavior.floating,
  114. margin: const EdgeInsets.all(25.0),
  115. //animation:,
  116. ));
  117. setState(() {
  118. isloading = false;
  119. });
  120. } on FormatException {
  121. //message
  122. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  123. //backgroundColor: grey300,
  124. duration: const Duration(seconds: 3),
  125. content: Text(
  126. "Something went wrong. Please try again later.", //
  127. style: GoogleFonts.kameron(
  128. fontSize: 15,
  129. ),
  130. ),
  131. behavior: SnackBarBehavior.floating,
  132. margin: const EdgeInsets.all(25.0),
  133. //animation:,
  134. ));
  135. setState(() {
  136. isloading = false;
  137. });
  138. }
  139. }
  140. }
  141.  
  142. @override
  143. Widget build(BuildContext context) {
  144. return Scaffold(
  145. backgroundColor: isDark ? dark1 : appWhite,
  146. body: SafeArea(
  147. child: SingleChildScrollView(
  148. child: Padding(
  149. padding: const EdgeInsets.all(0),
  150. child: Column(
  151. crossAxisAlignment: CrossAxisAlignment.start,
  152. mainAxisAlignment: MainAxisAlignment.center,
  153. children: [
  154. const SizedBox(
  155. height: 30,
  156. ),
  157. Padding(
  158. padding: const EdgeInsets.fromLTRB(0, 0, 20, 0),
  159. child: Center(
  160. child: Container(
  161. height: 150,
  162. width: MediaQuery.of(context).size.width / 1.25,
  163. decoration: const BoxDecoration(
  164. image: DecorationImage(
  165. image: AssetImage(
  166. "assets/images/vichar_logo.png",
  167. ),
  168. ),
  169. ),
  170. ),
  171. ),
  172. ),
  173. const SizedBox(
  174. height: 20,
  175. ),
  176. Padding(
  177. padding: const EdgeInsets.fromLTRB(25, 0, 25, 0),
  178. child: Form(
  179. key: _formkey,
  180. child: Column(
  181. children: [
  182. SizedBox(
  183. // height: 54,
  184. child: TextFormField(
  185. textCapitalization: TextCapitalization.sentences,
  186. onChanged: (value) {
  187. if (isValidated) {
  188. _formkey.currentState!.validate();
  189. }
  190. },
  191. validator: (value) {
  192. if (value == null || value.isEmpty) {
  193. return "Please enter valid username.";
  194. }
  195. return null;
  196. },
  197. controller: usernameController,
  198. keyboardType: TextInputType.name,
  199. decoration: InputDecoration(
  200. contentPadding: const EdgeInsets.fromLTRB(0, 18, 0, 18),
  201. hintText: "Username",
  202. hintStyle: GoogleFonts.kameron(
  203. color: grey,
  204. ),
  205. prefixIcon: const Padding(
  206. padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
  207. child: Icon(
  208. Icons.person,
  209. size: 21,
  210. color: grey,
  211. ),
  212. ),
  213. errorStyle: GoogleFonts.kameron(fontSize: 14),
  214. focusedErrorBorder: const OutlineInputBorder(
  215. borderRadius: BorderRadius.all(
  216. Radius.circular(27),
  217. ),
  218. borderSide: BorderSide(color: appred),
  219. ),
  220. errorBorder: const OutlineInputBorder(
  221. borderRadius: BorderRadius.all(
  222. Radius.circular(27),
  223. ),
  224. borderSide: BorderSide(color: appred),
  225. ),
  226. enabledBorder: const OutlineInputBorder(
  227. borderRadius: BorderRadius.all(
  228. Radius.circular(27),
  229. ),
  230. borderSide: BorderSide(color: grey),
  231. ),
  232. focusedBorder: const OutlineInputBorder(
  233. borderRadius: BorderRadius.all(
  234. Radius.circular(27),
  235. ),
  236. borderSide: BorderSide(color: appcolor),
  237. ),
  238. ),
  239. cursorColor: appcolor,
  240. cursorHeight: 21,
  241. style: GoogleFonts.kameron(
  242. color: isDark ? appWhite : appBlack87,
  243. ),
  244. ),
  245. ),
  246. const SizedBox(
  247. height: 20,
  248. ),
  249. SizedBox(
  250. //height: 54,
  251. child: TextFormField(
  252. onChanged: (value) {
  253. if (isValidated) {
  254. _formkey.currentState!.validate();
  255. }
  256. },
  257. validator: (value) {
  258. if (value == null ||
  259. value.isEmpty ||
  260. !(value.isEmail)) {
  261. return "Please enter valid email.";
  262. }
  263. return null;
  264. },
  265. controller: emailController,
  266. keyboardType: TextInputType.emailAddress,
  267. decoration: InputDecoration(
  268. contentPadding: const EdgeInsets.fromLTRB(0, 18, 0, 18),
  269. hintText: "Email",
  270. hintStyle: GoogleFonts.kameron(
  271. color: grey,
  272. ),
  273. prefixIcon: const Padding(
  274. padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
  275. child: Icon(
  276. Icons.email,
  277. size: 21,
  278. color: grey,
  279. ),
  280. ),
  281. errorStyle: GoogleFonts.kameron(fontSize: 14),
  282. focusedErrorBorder: const OutlineInputBorder(
  283. borderRadius: BorderRadius.all(
  284. Radius.circular(27),
  285. ),
  286. borderSide: BorderSide(color: appred),
  287. ),
  288. errorBorder: const OutlineInputBorder(
  289. borderRadius: BorderRadius.all(
  290. Radius.circular(27),
  291. ),
  292. borderSide: BorderSide(color: appred),
  293. ),
  294. enabledBorder: const OutlineInputBorder(
  295. borderRadius: BorderRadius.all(
  296. Radius.circular(27),
  297. ),
  298. borderSide: BorderSide(color: grey),
  299. ),
  300. focusedBorder: const OutlineInputBorder(
  301. borderRadius: BorderRadius.all(
  302. Radius.circular(27),
  303. ),
  304. borderSide: BorderSide(color: appcolor),
  305. ),
  306. ),
  307. cursorColor: appcolor,
  308. cursorHeight: 21,
  309. style: GoogleFonts.kameron(
  310. color: isDark ? appWhite : appBlack87,
  311. ),
  312. ),
  313. ),
  314. const SizedBox(
  315. height: 20,
  316. ),
  317. SizedBox(
  318. // height: 54,
  319. child: TextFormField(
  320. onChanged: (value) {
  321. if (isValidated) {
  322. _formkey.currentState!.validate();
  323. }
  324. },
  325. validator: (value) {
  326. if (value == null ||
  327. value.isEmpty ||
  328. !(value.isPhoneNumber) ||
  329. value.contains(".") ||
  330. value.length < 10) {
  331. return "Please enter valid mobile number.";
  332. }
  333. return null;
  334. },
  335. maxLength: 10,
  336. controller: mobilenumberController,
  337. keyboardType: TextInputType.phone,
  338. decoration: InputDecoration(
  339. contentPadding: const EdgeInsets.fromLTRB(0, 18, 0, 18),
  340. counterText: "",
  341. hintText: "Mobile Number",
  342. hintStyle: GoogleFonts.kameron(
  343. color: grey,
  344. ),
  345. prefixIcon: const Padding(
  346. padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
  347. child: Icon(
  348. Icons.phone,
  349. size: 21,
  350. color: grey,
  351. ),
  352. ),
  353. errorStyle: GoogleFonts.kameron(fontSize: 14),
  354. focusedErrorBorder: const OutlineInputBorder(
  355. borderRadius: BorderRadius.all(
  356. Radius.circular(27),
  357. ),
  358. borderSide: BorderSide(color: appred),
  359. ),
  360. errorBorder: const OutlineInputBorder(
  361. borderRadius: BorderRadius.all(
  362. Radius.circular(27),
  363. ),
  364. borderSide: BorderSide(color: appred),
  365. ),
  366. enabledBorder: const OutlineInputBorder(
  367. borderRadius: BorderRadius.all(
  368. Radius.circular(27),
  369. ),
  370. borderSide: BorderSide(color: grey),
  371. ),
  372. focusedBorder: const OutlineInputBorder(
  373. borderRadius: BorderRadius.all(
  374. Radius.circular(27),
  375. ),
  376. borderSide: BorderSide(color: appcolor),
  377. ),
  378. ),
  379. cursorColor: appcolor,
  380. cursorHeight: 21,
  381. style: GoogleFonts.kameron(
  382. color: isDark ? appWhite : appBlack87,
  383. ),
  384. ),
  385. ),
  386. const SizedBox(
  387. height: 20,
  388. ),
  389. SizedBox(
  390. // height: 54,
  391. child: TextFormField(
  392. onChanged: (value) {
  393. if (isValidated) {
  394. _formkey.currentState!.validate();
  395. }
  396. },
  397. validator: (value) {
  398. if (value == null || value.isEmpty) {
  399. return "Please enter valid password.";
  400. }
  401. if (value.length < 6) {
  402. return "Password length must be atleast 6 characters.";
  403. }
  404.  
  405. return null;
  406. },
  407. controller: passwordController,
  408. keyboardType: TextInputType.visiblePassword,
  409. obscureText: isPasswordVisible1,
  410. decoration: InputDecoration(
  411. contentPadding: const EdgeInsets.fromLTRB(0, 18, 0, 18),
  412. hintText: "Password",
  413. hintStyle: GoogleFonts.kameron(
  414. color: grey,
  415. ),
  416. prefixIcon: const Padding(
  417. padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
  418. child: Icon(
  419. Icons.lock,
  420. size: 21,
  421. color: grey,
  422. ),
  423. ),
  424. suffixIcon: Padding(
  425. padding:
  426. const EdgeInsets.symmetric(horizontal: 7.5),
  427. child: IconButton(
  428. icon: Icon(
  429. !isPasswordVisible1
  430. ? Icons.visibility
  431. : Icons.visibility_off,
  432. color: grey,
  433. ),
  434. onPressed: () {
  435. setState(() {
  436. isPasswordVisible1 = !isPasswordVisible1;
  437. });
  438. },
  439. ),
  440. ),
  441. errorStyle: GoogleFonts.kameron(fontSize: 14),
  442. focusedErrorBorder: const OutlineInputBorder(
  443. borderRadius: BorderRadius.all(
  444. Radius.circular(27),
  445. ),
  446. borderSide: BorderSide(color: appred),
  447. ),
  448. errorBorder: const OutlineInputBorder(
  449. borderRadius: BorderRadius.all(
  450. Radius.circular(27),
  451. ),
  452. borderSide: BorderSide(color: appred),
  453. ),
  454. enabledBorder: const OutlineInputBorder(
  455. borderRadius: BorderRadius.all(
  456. Radius.circular(27),
  457. ),
  458. borderSide: BorderSide(color: grey),
  459. ),
  460. focusedBorder: const OutlineInputBorder(
  461. borderRadius: BorderRadius.all(
  462. Radius.circular(27),
  463. ),
  464. borderSide: BorderSide(color: appcolor),
  465. ),
  466. ),
  467. cursorColor: appcolor,
  468. cursorHeight: 21,
  469. style: GoogleFonts.kameron(
  470. color: isDark ? appWhite : appBlack87,
  471. ),
  472. ),
  473. ),
  474. const SizedBox(
  475. height: 20,
  476. ),
  477. SizedBox(
  478. //height: 54,
  479. child: TextFormField(
  480. onChanged: (value) {
  481. if (isValidated) {
  482. _formkey.currentState!.validate();
  483. }
  484. },
  485. validator: (value) {
  486. if (value == null || value.isEmpty) {
  487. return "Please enter valid password.";
  488. }
  489. if (value.length < 6) {
  490. return "Password length must be atleast 6 characters.";
  491. }
  492. if (value != passwordController.text) {
  493. return "Passwords does not match.";
  494. }
  495.  
  496. return null;
  497. },
  498. controller: confirmpasswordController,
  499. keyboardType: TextInputType.visiblePassword,
  500. obscureText: isPasswordVisible2,
  501. decoration: InputDecoration(
  502. contentPadding: const EdgeInsets.fromLTRB(0, 18, 0, 18),
  503. hintText: "Confirm Password",
  504. hintStyle: GoogleFonts.kameron(
  505. color: grey,
  506. ),
  507. prefixIcon: const Padding(
  508. padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
  509. child: Icon(
  510. Icons.lock,
  511. size: 21,
  512. color: grey,
  513. ),
  514. ),
  515. suffixIcon: Padding(
  516. padding:
  517. const EdgeInsets.symmetric(horizontal: 7.5),
  518. child: IconButton(
  519. icon: Icon(
  520. !isPasswordVisible2
  521. ? Icons.visibility
  522. : Icons.visibility_off,
  523. color: grey,
  524. ),
  525. onPressed: () {
  526. setState(() {
  527. isPasswordVisible2 = !isPasswordVisible2;
  528. });
  529. },
  530. ),
  531. ),
  532. errorStyle: GoogleFonts.kameron(fontSize: 14),
  533. focusedErrorBorder: const OutlineInputBorder(
  534. borderRadius: BorderRadius.all(
  535. Radius.circular(27),
  536. ),
  537. borderSide: BorderSide(color: appred),
  538. ),
  539. errorBorder: const OutlineInputBorder(
  540. borderRadius: BorderRadius.all(
  541. Radius.circular(27),
  542. ),
  543. borderSide: BorderSide(color: appred),
  544. ),
  545. enabledBorder: const OutlineInputBorder(
  546. borderRadius: BorderRadius.all(
  547. Radius.circular(27),
  548. ),
  549. borderSide: BorderSide(color: grey),
  550. ),
  551. focusedBorder: const OutlineInputBorder(
  552. borderRadius: BorderRadius.all(
  553. Radius.circular(27),
  554. ),
  555. borderSide: BorderSide(color: appcolor),
  556. ),
  557. ),
  558. cursorColor: appcolor,
  559. cursorHeight: 21,
  560. style: GoogleFonts.kameron(
  561. color: isDark ? appWhite : appBlack87,
  562. ),
  563. ),
  564. ),
  565. const SizedBox(
  566. height: 35,
  567. ),
  568. SizedBox(
  569. height: 54,
  570. child: Center(
  571. child: ElevatedButton(
  572. style: ElevatedButton.styleFrom(
  573. backgroundColor: isDark ? dark2 : appcolor,
  574. fixedSize: const Size(400, 60),
  575. shape: RoundedRectangleBorder(
  576. borderRadius: BorderRadius.circular(27),
  577. side: BorderSide(
  578. color: isDark ? dark2 : appcolor,
  579. ),
  580. ),
  581. ),
  582. onPressed: () {
  583. signup();
  584. },
  585. child: isloading
  586. ? const CircularProgressIndicator(
  587. color: appWhite,
  588. strokeWidth: 2.5,
  589. )
  590. : Text(
  591. 'Signup',
  592. style: GoogleFonts.kameron(
  593. fontWeight: FontWeight.normal,
  594. fontSize: 18,
  595. color: Theme.of(context)
  596. .scaffoldBackgroundColor,
  597. ),
  598. ),
  599. ),
  600. ),
  601. ),
  602. const SizedBox(
  603. height: 30,
  604. ),
  605. GestureDetector(
  606. onTap: () {
  607. Navigator.of(context).pushAndRemoveUntil(
  608. MaterialPageRoute(builder: (_) => const LoginScreen()),
  609. (route) => false);
  610. },
  611. child: Center(
  612. child: Row(
  613. mainAxisAlignment: MainAxisAlignment.center,
  614. children: [
  615. Text(
  616. "Already have an account? ",
  617. style: GoogleFonts.kameron(
  618. fontSize: 15,
  619. color: grey,
  620. fontWeight: FontWeight.w400,
  621. ),
  622. ),
  623. Text(
  624. "Login",
  625. style: GoogleFonts.kameron(
  626. fontSize: 15,
  627. color: Theme.of(context).primaryColor,
  628. fontWeight: FontWeight.w500,
  629. ),
  630. ),
  631. ],
  632. ),
  633. ),
  634. ),
  635. const SizedBox(
  636. height: 20,
  637. ),
  638. ],
  639. ),
  640. ),
  641. ),
  642. ],
  643. ),
  644. ),
  645. )),
  646. );
  647. }
  648. }
Advertisement
Add Comment
Please, Sign In to add comment