Skip to content

Instantly share code, notes, and snippets.

@Iamkosgei
Last active March 26, 2020 09:38
Show Gist options
  • Save Iamkosgei/358f44cfd49213d3adb54e0d9db2283d to your computer and use it in GitHub Desktop.
Save Iamkosgei/358f44cfd49213d3adb54e0d9db2283d to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'SignUpResponseModel.dart';
class SignUpPage extends StatefulWidget {
@override
_SignUpPageState createState() => _SignUpPageState();
}
class _SignUpPageState extends State<SignUpPage> {
final _formKey = GlobalKey<FormState>();
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
String _name, _email, _password, _phone;
bool _autoValidate = false;
bool _isSubmitting = false;
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
elevation: 0.0,
title: Text("SIGNUP"),
centerTitle: true,
),
body: SingleChildScrollView(
child: Form(
autovalidate: _autoValidate,
key: _formKey,
child: Container(
padding: EdgeInsets.only(left: 15, right: 15, top: 15),
child: Column(children: [
Text(
"SIGNUP",
style: TextStyle(fontSize: 30),
),
SizedBox(height: 10),
TextFormField(
validator: (value) =>
value.trim().length < 1 ? "Enter a value" : null,
onSaved: (value) => _name = value,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
hintText: "NAME")),
SizedBox(height: 10),
TextFormField(
validator: (value) =>
value.trim().length < 1 ? "Enter a value" : null,
onSaved: (value) => _email = value,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
hintText: "Email")),
SizedBox(height: 10),
TextFormField(
validator: (value) =>
value.trim().length < 1 ? "Enter a value" : null,
onSaved: (value) => _phone = value,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
hintText: "PHONE")),
SizedBox(height: 10),
TextFormField(
validator: (value) =>
value.trim().length < 1 ? "Enter a value" : null,
onSaved: (value) => _password = value,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
hintText: "Password")),
SizedBox(height: 20),
_isSubmitting
? CircularProgressIndicator()
: RaisedButton(
onPressed: () => _signUpUser(context),
child: Text("SIGNUP"),
)
]),
),
),
),
);
}
void _signUpUser(BuildContext context) {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
setState(() {
_isSubmitting = true;
});
Map<String, String> _payload = Map();
_payload["name"] = _name;
_payload["email"] = _email;
_payload["password"] = _password;
_payload["phone"] = _phone;
print(_payload.toString());
http
.post("https://moving-kinshasa.herokuapp.com/users/new",
headers: {"Content-Type": "application/json"},
body: json.encode(_payload))
.then((response) {
setState(() {
_isSubmitting = false;
});
//serialize json to Dart
SignUpResponse signUpResponse =
SignUpResponse.fromJson(json.decode(response.body));
if (response.statusCode == 200) {
if (signUpResponse.message == "success") {
//navigate to another screen
_showSnackBar("Welcome");
} else {
_showSnackBar(signUpResponse.message);
}
} else {
_showSnackBar(signUpResponse.message);
}
}).catchError((onError) {
setState(() {
_isSubmitting = false;
});
});
} else {
setState(() {
_autoValidate = true;
});
}
}
void _showSnackBar(message) {
_scaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text(message),
),
);
}
}
//user signup response model
class SignUpResponse {
String message;
Response response;
SignUpResponse({this.message, this.response});
SignUpResponse.fromJson(Map<String, dynamic> json) {
message = json['message'];
response = json['response'] != null
? new Response.fromJson(json['response'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['message'] = this.message;
if (this.response != null) {
data['response'] = this.response.toJson();
}
return data;
}
}
class Response {
String sId;
int iV;
Response({this.sId, this.iV});
Response.fromJson(Map<String, dynamic> json) {
sId = json['_id'];
iV = json['__v'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['_id'] = this.sId;
data['__v'] = this.iV;
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment