Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:io';
- import 'package:dio/dio.dart';
- import 'package:flushbar/flushbar.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_secure_storage/flutter_secure_storage.dart';
- import 'package:i_manual/appLocalizations.dart';
- import 'package:i_manual/customDio.dart';
- import 'package:image_picker/image_picker.dart';
- const LISTTOPPADDING = 30 / 812;
- const LISTDETAILTOPPADDING = 20 / 812;
- const HORIZONTALPADDINGDETAILEDITEM = 10 / 375;
- const LEFTTITLESPADDING = 34 / 375;
- const ITEMWIDTH = 300.0;
- const PROBLEMSBOTTOMPADDING = 11 / 812;
- const PROBLEMLEFTPADDING = 40 / 375;
- const BUTTONHORIZONTALPADDING = 20 / 375;
- const BUTTONBOTTOMPADDING = 20 / 812;
- const MODALICONBOTTOMPADDING = 40 / 812;
- const MODALBUTTOMTOPPADDING = 40 / 812;
- const MODALBUTTONHORIZONTALPADDING = 25 / 375;
- class Register extends StatefulWidget {
- @override
- _RegisterState createState() => _RegisterState();
- }
- class _RegisterState extends State<Register> {
- File _image;
- TextEditingController _firstNameController;
- TextEditingController _lastNameController;
- TextEditingController _emailController;
- TextEditingController _passwordController;
- FocusNode _passwordFocusNode;
- FocusNode _lastNameFocusNode;
- FocusNode _emailFocusNode;
- FocusNode _birdthDayFocusNode;
- FocusNode _genderFocusNode;
- DateTime birdthDayValue;
- String genderValue;
- String englishGenderValue;
- bool submitting = false;
- bool createdAccount = false;
- @override
- void initState() {
- super.initState();
- _firstNameController = TextEditingController(text: '');
- _lastNameController = TextEditingController(text: '');
- _emailController = TextEditingController(text: '');
- _passwordController = TextEditingController(text: '');
- _passwordFocusNode = FocusNode();
- _lastNameFocusNode = FocusNode();
- _emailFocusNode = FocusNode();
- _birdthDayFocusNode = FocusNode();
- _genderFocusNode = FocusNode();
- }
- Future getImage() async {
- var image = await ImagePicker.pickImage(source: ImageSource.camera);
- setState(() {
- _image = image;
- });
- }
- genderActionSheetMethod(BuildContext context) {
- final AppLocalizations localization = AppLocalizations.of(context);
- final List _genderOptions = [
- {"locale": localization.male, "english": "male"},
- {"locale": localization.female, "english": "female"},
- {"locale": localization.noGender, "english": "pnts"}
- ];
- showCupertinoModalPopup(
- context: context,
- builder: (context) {
- return Container(
- height: MediaQuery.of(context).size.height / 5,
- child: CupertinoPicker(
- onSelectedItemChanged: (item) {
- setState(() {
- genderValue = _genderOptions[item]["locale"];
- englishGenderValue = _genderOptions[item]["english"];
- });
- },
- itemExtent: 32.0,
- children: _genderOptions
- .map((gender) => Text(gender["locale"]))
- .toList(),
- ),
- );
- });
- }
- birthDateTapHandler(BuildContext context) {
- showCupertinoModalPopup(
- context: context,
- builder: (context) {
- return Container(
- height: MediaQuery.of(context).size.height / 5,
- child: CupertinoDatePicker(
- mode: CupertinoDatePickerMode.date,
- onDateTimeChanged: (DateTime date) {
- setState(() {
- birdthDayValue = date;
- });
- }),
- );
- });
- }
- handleCreate(Size deviceSize) async {
- final AppLocalizations localization = AppLocalizations.of(context);
- setState(() {
- submitting = true;
- });
- try {
- final dio = await CustomDio.getInstance();
- await dio.post("users", data: {
- "first_name": _firstNameController.text,
- "last_name": _lastNameController.text,
- "gender": englishGenderValue,
- "email": _emailController.text,
- "username": _emailController.text,
- "password": _passwordController.text,
- "birthdate": birdthDayValue.toIso8601String()
- });
- final loginResponse = await dio
- .post("http://manual-252420.appspot.com/auth/login", data: {
- "username": _emailController.text,
- "password": _passwordController.text
- });
- final storage = FlutterSecureStorage();
- await storage.write(
- key: "accessToken", value: loginResponse.data["access_token"]);
- await storage.write(
- key: "refreshToken", value: loginResponse.data["refresh_token"]);
- final firstTimeOpen = await storage.read(key: "firstTimeOpen");
- setState(() {
- createdAccount = true;
- submitting = false;
- });
- Flushbar(
- title: "Success",
- message: localization.userCreateSuccessMessage,
- icon: Icon(
- Icons.check,
- color: CupertinoColors.white,
- ),
- duration: Duration(seconds: 3),
- onStatusChanged: (FlushbarStatus status) async {
- if (status == FlushbarStatus.DISMISSED) {
- if (firstTimeOpen == null)
- await Navigator.pushNamedAndRemoveUntil(
- context, "/tutorial", (Route<dynamic> route) => false);
- else
- await Navigator.pushNamedAndRemoveUntil(
- context, "/myHomePage", (Route<dynamic> route) => false);
- }
- },
- backgroundColor: Colors.green,
- flushbarStyle: FlushbarStyle.GROUNDED,
- )..show(context);
- } on DioError catch (e) {
- String errorMessage = '';
- setState(() {
- submitting = false;
- });
- if (e.response != null) {
- if (e.response.statusCode == 422) {
- if (e.response.data.runtimeType == String)
- errorMessage = e.response.data;
- else
- e.response.data.forEach((key, errors) {
- final withoutDotsErrorMessage =
- errors.map((error) => error.replaceAll(".", "")).toList();
- final errorField = key.toString();
- errorMessage = errorMessage +
- "${errorField[0].toUpperCase()}${errorField.substring(1)}: ${withoutDotsErrorMessage.join(", ")}";
- });
- } else
- errorMessage = localization.unexpectedError;
- } else {
- errorMessage = localization.networkError;
- }
- Flushbar(
- title: localization.unexpectedError,
- message: errorMessage,
- icon: Icon(
- Icons.error_outline,
- color: CupertinoColors.white,
- ),
- duration: Duration(seconds: 3),
- backgroundColor: Color(0xFFf44336),
- flushbarStyle: FlushbarStyle.GROUNDED,
- )..show(context);
- } catch (e) {
- setState(() {
- submitting = false;
- });
- Flushbar(
- title: localization.unexpectedError,
- message: localization.unexpectedError,
- icon: Icon(
- Icons.error_outline,
- color: CupertinoColors.white,
- ),
- duration: Duration(seconds: 3),
- backgroundColor: Color(0xFFf44336),
- flushbarStyle: FlushbarStyle.GROUNDED,
- )..show(context);
- }
- }
- @override
- Widget build(BuildContext context) {
- final deviceSize = MediaQuery.of(context).size;
- final AppLocalizations localization = AppLocalizations.of(context);
- final disabledSubmit = _firstNameController.text.isEmpty ||
- _lastNameController.text.isEmpty ||
- birdthDayValue == null ||
- genderValue == null ||
- _emailController.text.isEmpty ||
- _passwordController.text.isEmpty ||
- createdAccount;
- return CupertinoPageScaffold(
- navigationBar: CupertinoNavigationBar(
- middle: Text(localization.register,
- style: TextStyle(
- fontWeight: FontWeight.w300,
- fontFamily:
- CupertinoTheme.of(context).textTheme.textStyle.fontFamily,
- fontSize: 22,
- color: CupertinoColors.white,
- )),
- backgroundColor: Color(0xFF5F688B),
- ),
- child: GestureDetector(
- onTap: () {
- FocusScope.of(context).requestFocus(new FocusNode());
- },
- child: SafeArea(child: Container(
- child: LayoutBuilder(builder: (context, constraints) {
- return Material(
- color: CupertinoTheme.of(context).scaffoldBackgroundColor,
- child: Padding(
- padding: EdgeInsets.symmetric(
- horizontal: constraints.maxWidth * BUTTONHORIZONTALPADDING),
- child: ListView(
- children: <Widget>[
- Padding(
- padding: EdgeInsets.only(
- top: deviceSize.height * LISTTOPPADDING,
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: <Widget>[
- Flexible(
- flex: 30,
- child: Text(
- localization.firstName,
- style: TextStyle(fontSize: 17),
- ),
- ),
- Flexible(
- flex: 70,
- child: CupertinoTextField(
- controller: _firstNameController,
- style: TextStyle(fontSize: 16),
- onSubmitted: (value) {
- FocusScope.of(context)
- .requestFocus(_lastNameFocusNode);
- },
- keyboardType: TextInputType.text,
- textCapitalization: TextCapitalization.words,
- textInputAction: TextInputAction.next,
- ),
- ),
- ],
- ),
- ),
- Padding(
- padding: EdgeInsets.only(
- top: deviceSize.height * LISTTOPPADDING,
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: <Widget>[
- Flexible(
- flex: 30,
- child: Text(
- localization.lastName,
- style: TextStyle(fontSize: 17),
- ),
- ),
- Flexible(
- flex: 70,
- child: CupertinoTextField(
- controller: _lastNameController,
- focusNode: _lastNameFocusNode,
- style: TextStyle(fontSize: 16),
- keyboardType: TextInputType.text,
- textInputAction: TextInputAction.next,
- textCapitalization: TextCapitalization.words,
- onSubmitted: (value) {
- FocusScope.of(context)
- .requestFocus(_birdthDayFocusNode);
- },
- ),
- ),
- ],
- ),
- ),
- Padding(
- padding: EdgeInsets.only(
- top: deviceSize.height * LISTTOPPADDING,
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: <Widget>[
- Flexible(
- fit: FlexFit.tight,
- flex: 30,
- child: Text(
- localization.birthDate,
- style: TextStyle(fontSize: 17),
- ),
- ),
- Flexible(
- fit: FlexFit.tight,
- flex: 70,
- child: Focus(
- focusNode: _birdthDayFocusNode,
- onFocusChange: (focused) {
- if (focused) birthDateTapHandler(context);
- },
- child: Padding(
- padding:
- const EdgeInsets.symmetric(vertical: 8.0),
- child: GestureDetector(
- onTap: () {
- birthDateTapHandler(context);
- },
- child: Text(
- birdthDayValue == null
- ? localization.tapToSelect
- : "${birdthDayValue.year}-${birdthDayValue.month}-${birdthDayValue.day}",
- textAlign: TextAlign.center,
- style: TextStyle(fontSize: 16),
- ),
- ),
- ),
- )),
- ],
- ),
- ),
- Padding(
- padding: EdgeInsets.only(
- top: deviceSize.height * LISTTOPPADDING,
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: <Widget>[
- Flexible(
- fit: FlexFit.tight,
- flex: 30,
- child: Text(
- localization.gender,
- style: TextStyle(fontSize: 17),
- ),
- ),
- Flexible(
- fit: FlexFit.tight,
- flex: 70,
- child: Focus(
- focusNode: _genderFocusNode,
- child: Padding(
- padding:
- const EdgeInsets.symmetric(vertical: 8.0),
- child: GestureDetector(
- onTap: () {
- genderActionSheetMethod(context);
- },
- child: Text(
- genderValue == null
- ? localization.tapToSelect
- : genderValue,
- textAlign: TextAlign.center,
- style: TextStyle(fontSize: 16),
- ),
- ),
- ),
- )),
- ],
- ),
- ),
- Padding(
- padding: EdgeInsets.only(
- top: deviceSize.height * LISTTOPPADDING,
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: <Widget>[
- Flexible(
- flex: 30,
- child: Text(
- localization.email,
- style: TextStyle(fontSize: 17),
- ),
- ),
- Flexible(
- flex: 70,
- child: CupertinoTextField(
- controller: _emailController,
- style: TextStyle(fontSize: 16),
- onSubmitted: (value) {
- FocusScope.of(context)
- .requestFocus(_passwordFocusNode);
- },
- textInputAction: TextInputAction.next,
- keyboardType: TextInputType.emailAddress,
- ),
- ),
- ],
- ),
- ),
- Padding(
- padding: EdgeInsets.only(
- top: deviceSize.height * LISTTOPPADDING,
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: <Widget>[
- Flexible(
- flex: 30,
- child: Text(
- localization.password,
- style: TextStyle(fontSize: 17),
- ),
- ),
- Flexible(
- flex: 70,
- child: CupertinoTextField(
- controller: _passwordController,
- focusNode: _passwordFocusNode,
- style: TextStyle(fontSize: 16),
- obscureText: true,
- keyboardType: TextInputType.emailAddress,
- ),
- ),
- ],
- ),
- ),
- // Padding(
- // padding: EdgeInsets.only(
- // left: deviceSize.width * LEFTTITLESPADDING,
- // top: deviceSize.height * LISTTOPPADDING,
- // bottom: deviceSize.height * LISTDETAILTOPPADDING,
- // ),
- // child: Row(
- // mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- // children: <Widget>[
- // Flexible(
- // fit: FlexFit.tight,
- // flex: 30,
- // child: Text(
- // localization.image,
- // style: TextStyle(
- // fontSize: 17,
- // ),
- // ),
- // ),
- // Flexible(
- // fit: FlexFit.tight,
- // flex: 70,
- // child: Container(
- // child: Row(
- // children: <Widget>[
- // GestureDetector(
- // onTap: getImage,
- // child: Container(
- // height: 100,
- // width: 100,
- // decoration: BoxDecoration(
- // borderRadius: BorderRadius.all(
- // Radius.circular(5)),
- // border: Border.all(
- // color: Color(0xFFA4A1B5),
- // )),
- // child: Center(
- // child: _image == null
- // ? Text(
- // localization.tapToUploadImage,
- // textAlign: TextAlign.center,
- // style: TextStyle(
- // fontSize: 16,
- // color: Color(0xFFA4A1B5)),
- // )
- // : Image.file(_image),
- // ),
- // ),
- // ),
- // ],
- // ),
- // )),
- // ],
- // ),
- // ),
- Padding(
- padding: EdgeInsets.only(
- top: deviceSize.height * LISTTOPPADDING * 1.5,
- bottom: deviceSize.height * BUTTONBOTTOMPADDING),
- child: this.submitting
- ? Container(
- height: 50.0,
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- CupertinoActivityIndicator(
- radius: 10,
- ),
- ],
- ))
- : CupertinoButton(
- minSize: 0,
- padding: EdgeInsets.all(0),
- onPressed: disabledSubmit
- ? null
- : () {
- handleCreate(deviceSize);
- },
- color: CupertinoTheme.of(context).primaryColor,
- borderRadius: BorderRadius.circular(30.0),
- child: Container(
- height: 50.0,
- child: Center(
- child: Text(
- localization.register.toUpperCase(),
- style: TextStyle(
- color: CupertinoColors.white,
- fontSize: 17),
- ),
- ))),
- ),
- ],
- ),
- ),
- );
- }),
- )),
- ),
- );
- }
- }
- //class Region {
- // final String code;
- // final String name;
- //
- // Country({this.code, this.name});
- //
- // factory Country.fromJson(Map<String, dynamic> json) {
- // return Country(
- // code: json['countryCode'],
- // name: json['countryName'],
- // );
- // }
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement