Advertisement
azka12345

Register

Feb 21st, 2020
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 10.33 KB | None | 0 0
  1. import 'dart:convert';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:font_awesome_flutter/font_awesome_flutter.dart';
  5. import 'package:giffy_dialog/giffy_dialog.dart';
  6. import 'package:image_picker/image_picker.dart';
  7. import 'package:shared_preferences/shared_preferences.dart';
  8. import 'package:vivanews/share/constants.dart';
  9. import 'package:vivanews/view/component/loader.dart';
  10. import 'package:http/http.dart' as http;
  11. import 'package:email_validator/email_validator.dart';
  12. import 'package:vivanews/model/response.dart';
  13. import 'package:vivanews/model//usermodel.dart';
  14. import 'package:vivanews/view/screen/home_screen.dart';
  15.  
  16. const List<Key> keys = [
  17.   Key('Network'),
  18.   Key('Network Dialog'),
  19.   Key('Flare'),
  20.   Key('Flare Dialog'),
  21.   Key('Asset'),
  22.   Key('Asset dialog'),
  23. ];
  24.  
  25. String networking = 'https://media.giphy.com/media/26gJyevfC9ypAfLaM/giphy.gif';
  26.  
  27. String networking2 = 'https://media.giphy.com/media/qwqqCWbiffgQM/giphy.gif';
  28.  
  29. class Register extends StatefulWidget {
  30.   final String valueEmail, valueNama;
  31.  
  32.   Register({Key key, this.valueEmail, this.valueNama}) : super(key: key);
  33.  
  34.   @override
  35.   _RegisterState createState() => new _RegisterState();
  36. }
  37.  
  38. class _RegisterState extends State<Register> {
  39.   TextEditingController controllerUsername = new TextEditingController();
  40.   TextEditingController controllerEmail = new TextEditingController();
  41.   TextEditingController controllerFullname = new TextEditingController();
  42.   TextEditingController controllerPhone = new TextEditingController();
  43.   TextEditingController controllerPassword = new TextEditingController();
  44.   TextEditingController confirmPassword = new TextEditingController();
  45.  
  46.   var _formKey = GlobalKey<FormState>();
  47.  
  48.   void addData() {
  49.     var urlviva = Constants.REGISTER;
  50.  
  51.     http.post(urlviva, body: {
  52.       "username": controllerUsername.text,
  53.       "email": controllerEmail.text,
  54.       "fullname": controllerFullname.text,
  55.       "phone": controllerPhone.text,
  56.       "password": controllerPassword.text
  57.     }).then((response) {
  58.       String responseBody = response.body;
  59.       print("Response status: ${response.statusCode}");
  60.       print("Response body: ${response.body}");
  61.  
  62.       Map userMap = jsonDecode(responseBody);
  63.       var rs = Response.fromJson(userMap);
  64.  
  65.       print('${rs.message}!');
  66.       print('${rs.status}');
  67.  
  68.       String message = rs.message;
  69.  
  70.       if (rs.status == 0) {
  71.         showDialog(
  72.             context: context,
  73.             builder: (_) => FlareGiffyDialog(
  74.                   key: keys[3],
  75.                   flarePath: 'assets/teddy.flr',
  76.                   flareAnimation: 'fail',
  77.                   title: Text(
  78.                     "VIVA.CO.ID",
  79.                     style: TextStyle(
  80.                       fontSize: 22.0,
  81.                       fontWeight: FontWeight.bold,
  82.                     ),
  83.                   ),
  84.                   description: Text(
  85.                     message,
  86.                     textAlign: TextAlign.center,
  87.                   ),
  88.                   entryAnimation: EntryAnimation.TOP_LEFT,
  89.                   onOkButtonPressed: () {
  90.                     Navigator.pop(context);
  91.                   },
  92.                 ));
  93.         new Future.delayed(const Duration(seconds: 5), () {
  94.           Navigator.of(context, rootNavigator: true).pop();
  95.         });
  96.         return false;
  97.       } else {
  98.         showDialog(
  99.             context: context,
  100.             builder: (_) => FlareGiffyDialog(
  101.                   key: keys[3],
  102.                   flarePath: 'assets/teddy.flr',
  103.                   flareAnimation: 'success',
  104.                   title: Text(
  105.                     "VIVA.CO.ID",
  106.                     style: TextStyle(
  107.                       fontSize: 22.0,
  108.                       fontWeight: FontWeight.bold,
  109.                     ),
  110.                   ),
  111.                   description: Text(
  112.                     message,
  113.                     textAlign: TextAlign.center,
  114.                   ),
  115.                   entryAnimation: EntryAnimation.TOP_LEFT,
  116.                   onOkButtonPressed: () {
  117.                     Navigator.pop(context);
  118.                   },
  119.                 ));
  120.         new Future.delayed(const Duration(seconds: 5), () {
  121.           Navigator.of(context, rootNavigator: true).pop();
  122.         });
  123.         return true;
  124.       }
  125.     });
  126.   }
  127.  
  128.   bool _isHidePassword = true;
  129.  
  130.   void _togglePasswordVisibility() {
  131.     setState(() {
  132.       _isHidePassword = !_isHidePassword;
  133.     });
  134.   }
  135.  
  136.   void _togglePasswordVisibility_c() {
  137.     setState(() {
  138.       _isHidePassword = !_isHidePassword;
  139.     });
  140.   }
  141.  
  142.   bool dialog = false;
  143.  
  144.   void _showDialog() {
  145.     setState(() {
  146.       dialog = true;
  147.     });
  148.     showDialog(
  149.         context: context,
  150.         barrierDismissible: false,
  151.         builder: (BuildContext context) {
  152.           return Center(child: Loader());
  153.         }).then((_) {
  154.       addData();
  155.       if (mounted) {
  156.         setState(() {
  157.           dialog = false; // dialog was closed
  158.         });
  159.       }
  160.     });
  161.     new Future.delayed(const Duration(seconds: 2), () {
  162.       Navigator.of(context, rootNavigator: true).pop();
  163.     });
  164.   }
  165.  
  166.   @override
  167.   Widget build(BuildContext context) {
  168.     controllerFullname.text = "${widget.valueNama ?? ''}";
  169.     controllerEmail.text = "${widget.valueEmail ?? ''}";
  170.  
  171.     final username = TextFormField(
  172.       controller: controllerUsername,
  173.       validator: (value) {
  174.         if (value.isEmpty)
  175.           return 'Username Harus di isi';
  176.         else
  177.           return null;
  178.       },
  179.       keyboardType: TextInputType.text,
  180.       autofocus: false,
  181.       enabled: true,
  182.       decoration: InputDecoration(
  183.         labelText: 'Username',
  184.       ),
  185.     );
  186.  
  187.     final emailtext = TextFormField(
  188.       controller: controllerEmail,
  189.       validator: (value) {
  190.         if (value.isEmpty) return 'Email Harus di isi';
  191.         if (!EmailValidator.validate(value, true))
  192.           return 'Email Tidak Valid';
  193.         else
  194.           return null;
  195.       },
  196.       keyboardType: TextInputType.emailAddress,
  197.       enabled: true,
  198.       autofocus: false,
  199.       decoration: InputDecoration(
  200.         labelText: 'Email',
  201.       ),
  202.     );
  203.  
  204.     final fullname = TextFormField(
  205.       controller: controllerFullname,
  206.       validator: (value) {
  207.         if (value.isEmpty)
  208.           return 'Nama Lengkap Harus di isi';
  209.         else
  210.           return null;
  211.       },
  212.       keyboardType: TextInputType.text,
  213.       autofocus: false,
  214.       enabled: true,
  215.       decoration: InputDecoration(
  216.         labelText: 'Nama Lengkap',
  217.       ),
  218.     );
  219.  
  220.     final phone = TextFormField(
  221.       controller: controllerPhone,
  222.       validator: (value) {
  223.         if (value.isEmpty) return 'No Handphone Harus di isi';
  224.         if (value.length < 8) return "No Handphone Minimal 10 angka";
  225.         if (value.length > 13)
  226.           return "No Handphone Maksimal 13 Angka";
  227.         else
  228.           return null;
  229.       },
  230.       keyboardType: TextInputType.number,
  231.       autofocus: false,
  232.       enabled: true,
  233.       decoration: InputDecoration(
  234.         labelText: 'No Telephone',
  235.       ),
  236.     );
  237.  
  238.     final password = TextFormField(
  239.       controller: controllerPassword,
  240.       validator: (value) {
  241.         if (value.isEmpty) return 'Password Harus di isi';
  242.         if (value.length < 6)
  243.           return "Password Minimal 6 Karakter";
  244.         else
  245.           return null;
  246.       },
  247.       obscureText: _isHidePassword,
  248.       autofocus: false,
  249.       keyboardType: TextInputType.text,
  250.       decoration: InputDecoration(
  251.         labelText: 'Password',
  252.         suffixIcon: GestureDetector(
  253.           onTap: () {
  254.             _togglePasswordVisibility();
  255.           },
  256.           child: Icon(
  257.             _isHidePassword ? Icons.visibility_off : Icons.visibility,
  258.             color: _isHidePassword ? Colors.grey : Colors.purple,
  259.           ),
  260.         ),
  261.         isDense: true,
  262.       ),
  263.     );
  264.  
  265.     final c_password = TextFormField(
  266.       controller: confirmPassword,
  267.       validator: (value) {
  268.         if (value.isEmpty) return 'Konfirmasi Password Harus di isi';
  269.         if (value != controllerPassword.text)
  270.           return 'Konfirmasi Password Harus Sama';
  271.         if (value.length < 6)
  272.           return "Konfirmasi Password Minimal 6 Karakter";
  273.         else
  274.           return null;
  275.       },
  276.       obscureText: _isHidePassword,
  277.       autofocus: false,
  278.       keyboardType: TextInputType.text,
  279.       decoration: InputDecoration(
  280.         labelText: 'Konfirmasi Password',
  281.         suffixIcon: GestureDetector(
  282.           onTap: () {
  283.             _togglePasswordVisibility_c();
  284.           },
  285.           child: Icon(
  286.             _isHidePassword ? Icons.visibility_off : Icons.visibility,
  287.             color: _isHidePassword ? Colors.grey : Colors.purple,
  288.           ),
  289.         ),
  290.         isDense: true,
  291.       ),
  292.     );
  293.  
  294.     final register = RaisedButton(
  295.       onPressed: () {
  296.         if (_formKey.currentState.validate()) {
  297.           _showDialog();
  298.         }
  299.       },
  300.       color: Colors.purple,
  301.       child: Padding(
  302.         padding: const EdgeInsets.all(8.0),
  303.         child: Text(
  304.           'Register',
  305.           style: TextStyle(fontSize: 25, color: Colors.white),
  306.         ),
  307.       ),
  308.       elevation: 5,
  309.       shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(40)),
  310.     );
  311.  
  312.     return Scaffold(
  313.       appBar: new AppBar(
  314.         title: new Text('Register Member',
  315.             style: TextStyle(
  316.                 fontSize: 20.0,
  317.                 color: Colors.black,
  318.                 fontWeight: FontWeight.bold)),
  319.         backgroundColor: Colors.white,
  320.         titleSpacing: -10,
  321.         leading: new IconButton(
  322.             icon: new Icon(FontAwesomeIcons.user, color: Colors.black)),
  323.       ),
  324.       body: Form(
  325.         key: _formKey,
  326.         child: ListView(
  327.           shrinkWrap: true,
  328.           padding: EdgeInsets.only(left: 24.0, right: 24.0),
  329.           children: <Widget>[
  330.             SizedBox(height: 24.0),
  331.             username,
  332.             SizedBox(height: 8.0),
  333.             emailtext,
  334.             SizedBox(height: 8.0),
  335.             fullname,
  336.             SizedBox(height: 8.0),
  337.             phone,
  338.             SizedBox(height: 8.0),
  339.             password,
  340.             SizedBox(height: 8.0),
  341.             c_password,
  342.             SizedBox(height: 24.0),
  343.             register
  344.           ],
  345.         ),
  346.       ),
  347.     );
  348.   }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement