Created
December 9, 2021 18:10
-
-
Save crazy-diya/2ad701f26cdb9e29e7856d475a12a1c6 to your computer and use it in GitHub Desktop.
API Call POST request Flutter Dio SignUp page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:dio/dio.dart'; | |
import 'package:doctor_app/Constant/api_url.dart'; | |
import 'package:doctor_app/Constant/loading.dart'; | |
import 'package:doctor_app/profile_page.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:doctor_app/Constant/app_colors.dart'; | |
class SignUpSecond extends StatefulWidget { | |
final String username; | |
final String email; | |
final String password; | |
final int isDoctor; | |
const SignUpSecond( | |
{Key? key, | |
required this.username, | |
required this.email, | |
required this.password, | |
required this.isDoctor}) | |
: super(key: key); | |
@override | |
_SignUpSecondState createState() => _SignUpSecondState(); | |
} | |
class _SignUpSecondState extends State<SignUpSecond> { | |
String? dropdownValue; | |
TextEditingController address = TextEditingController(); | |
TextEditingController hospital = TextEditingController(); | |
TextEditingController mobileNumber = TextEditingController(); | |
String imgUrl = "dfdjfk"; | |
GlobalKey<FormState>? _formKey; | |
bool loading = false; | |
@override | |
void initState() { | |
// TODO: implement initState | |
super.initState(); | |
_formKey = GlobalKey(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
double width = MediaQuery.of(context).size.width; | |
double height = MediaQuery.of(context).size.height; | |
return SafeArea( | |
child: Scaffold( | |
body: Stack( | |
children: [ | |
Container( | |
padding: EdgeInsets.symmetric( | |
horizontal: width * 0.1, vertical: width * 0.04), | |
child: SingleChildScrollView( | |
child: Form( | |
key: _formKey, | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Container( | |
child: Text( | |
"DOCTOR", | |
style: TextStyle( | |
color: AppColors.Button, | |
fontSize: width * 0.08, | |
fontWeight: FontWeight.bold), | |
), | |
), | |
SizedBox( | |
height: height * 0.06, | |
), | |
Container( | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
children: [ | |
Text("Sign up", | |
style: TextStyle( | |
color: Colors.black.withOpacity(0.7), | |
fontSize: width * 0.08, | |
fontWeight: FontWeight.bold)), | |
// SvgPicture.asset('assets/svg/user_icon.svg', color:Colors.grey,), | |
], | |
), | |
), | |
SizedBox( | |
height: height * 0.08, | |
), | |
Container( | |
child: Row( | |
children: [ | |
Container( | |
width: 80, | |
height: 80, | |
decoration: BoxDecoration( | |
border: Border.all( | |
width: 1, color: Colors.grey.shade400), | |
shape: BoxShape.circle, | |
// image: DecorationImage( | |
// image: AssetImage( | |
// "assets/images/png/user.png", | |
// ), | |
// fit: BoxFit.cover), | |
), | |
), | |
SizedBox( | |
width: 20, | |
), | |
Text( | |
"Add profile picture", | |
style: TextStyle( | |
color: Colors.grey.shade400, | |
fontWeight: FontWeight.bold), | |
) | |
], | |
)), | |
SizedBox( | |
height: height * 0.07, | |
), | |
TextFormField( | |
cursorColor: AppColors.Button, | |
decoration: InputDecoration( | |
enabledBorder: UnderlineInputBorder( | |
borderSide: BorderSide(color: Colors.grey.shade400), | |
), | |
focusedBorder: UnderlineInputBorder( | |
borderSide: BorderSide(color: AppColors.Button), | |
), | |
border: UnderlineInputBorder(), | |
hintText: 'Address', | |
hintStyle: TextStyle( | |
color: Colors.grey, fontWeight: FontWeight.bold), | |
), | |
controller: address, | |
autovalidateMode: AutovalidateMode.onUserInteraction, | |
validator: (value) { | |
if (value!.isEmpty) { | |
return 'Fill the Field!'; | |
} else { | |
return null; | |
} | |
}, | |
), | |
SizedBox( | |
height: width * 0.07, | |
), | |
TextFormField( | |
cursorColor: AppColors.Button, | |
decoration: InputDecoration( | |
enabledBorder: UnderlineInputBorder( | |
borderSide: BorderSide(color: Colors.grey.shade400), | |
), | |
focusedBorder: UnderlineInputBorder( | |
borderSide: BorderSide(color: AppColors.Button), | |
), | |
border: UnderlineInputBorder(), | |
hintText: 'Hospital', | |
hintStyle: TextStyle( | |
color: Colors.grey, fontWeight: FontWeight.bold), | |
), | |
controller: hospital, | |
autovalidateMode: AutovalidateMode.onUserInteraction, | |
validator: (value) { | |
if (value!.isEmpty) { | |
return 'Fill the Field!'; | |
} else { | |
return null; | |
} | |
}, | |
), | |
SizedBox( | |
height: width * 0.07, | |
), | |
TextFormField( | |
cursorColor: AppColors.Button, | |
decoration: InputDecoration( | |
enabledBorder: UnderlineInputBorder( | |
borderSide: BorderSide(color: Colors.grey.shade400), | |
), | |
focusedBorder: UnderlineInputBorder( | |
borderSide: BorderSide(color: AppColors.Button), | |
), | |
border: UnderlineInputBorder(), | |
hintText: 'Mobile number', | |
hintStyle: TextStyle( | |
color: Colors.grey, fontWeight: FontWeight.bold), | |
), | |
controller: mobileNumber, | |
keyboardType: TextInputType.phone, | |
autovalidateMode: AutovalidateMode.onUserInteraction, | |
validator: (value) { | |
if (value!.isEmpty) { | |
return 'Fill the Field!'; | |
} else { | |
return null; | |
} | |
}, | |
), | |
SizedBox( | |
height: height * 0.05, | |
), | |
Container( | |
padding: EdgeInsets.symmetric( | |
horizontal: 12, | |
), | |
decoration: BoxDecoration( | |
borderRadius: BorderRadius.circular(12), | |
border: Border.all( | |
color: Colors.grey.shade400, width: 2)), | |
child: DropdownButton<String>( | |
underline: SizedBox(), | |
icon: Icon( | |
Icons.keyboard_arrow_down, | |
size: 35, | |
color: Colors.grey.shade400, | |
), | |
isExpanded: true, | |
value: dropdownValue, | |
items: <String>['Specialization', 'Two', 'Free', 'Four'] | |
.map<DropdownMenuItem<String>>((String value) { | |
return DropdownMenuItem<String>( | |
value: value, | |
child: Text( | |
value, | |
style: TextStyle( | |
color: Colors.grey, | |
fontWeight: FontWeight.bold), | |
), | |
); | |
}).toList(), | |
onChanged: (String? newValue) { | |
setState(() { | |
dropdownValue = newValue!; | |
}); | |
print(dropdownValue); | |
}, | |
), | |
), | |
SizedBox( | |
height: height * 0.06, | |
), | |
Center( | |
child: OutlinedButton( | |
onPressed: () { | |
validateCredential(); | |
}, | |
child: Text( | |
"Sign up", | |
style: TextStyle( | |
color: AppColors.font_color, | |
fontSize: width * 0.055, | |
fontWeight: FontWeight.bold), | |
), | |
style: OutlinedButton.styleFrom( | |
backgroundColor: AppColors.Button, | |
padding: EdgeInsets.symmetric( | |
vertical: 10, horizontal: width * 0.2), | |
shape: RoundedRectangleBorder( | |
borderRadius: BorderRadius.circular(20)), | |
side: BorderSide( | |
color: AppColors.Button, | |
width: 2.0, | |
style: BorderStyle.solid), | |
), | |
), | |
), | |
SizedBox( | |
height: width * 0.04, | |
), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text( | |
"Already have an account ?", | |
style: TextStyle(color: Colors.grey), | |
), | |
InkWell( | |
onTap: () {}, | |
child: Text( | |
'sign in', | |
style: TextStyle(color: AppColors.Button), | |
), | |
) | |
], | |
), | |
SizedBox( | |
height: height * 0.04, | |
), | |
], | |
), | |
), | |
), | |
), | |
loading | |
? Align( | |
alignment: Alignment.center, | |
child: Loading(), | |
) | |
: SizedBox(), | |
], | |
), | |
)); | |
} | |
validateCredential() async { | |
_formKey!.currentState!.save(); | |
if (!_formKey!.currentState!.validate()) { | |
print("Not Validate!"); | |
} else { | |
await callRegisterAPI(); | |
} | |
} | |
callRegisterAPI() async { | |
print(widget.username + | |
"\n" + | |
widget.password + | |
"\n" + | |
widget.email + | |
"\n" + | |
imgUrl + | |
"\n" + | |
address.text + | |
"\n" + | |
hospital.text + | |
"\n" + | |
mobileNumber.text + | |
"\n" + | |
dropdownValue!); | |
try { | |
setState(() { | |
loading = true; | |
}); | |
var response = await Dio().post( | |
/*ApiUrl().userApi+*/ 'http://ec2-54-191-116-138.us-west-2.compute.amazonaws.com:8080/user/register', | |
data: { | |
"username": widget.username, | |
"password": widget.password, | |
"email": widget.email, | |
"profilepicture": imgUrl, | |
"address": address.text, | |
"hospital": hospital.text, | |
"mobilenumber": mobileNumber.text, | |
"specialization": dropdownValue | |
}); | |
print(response); | |
if (response.statusCode == 200 && response.data["success"] == true) { | |
ScaffoldMessenger.of(context).showSnackBar(SnackBar( | |
content: Text("Successfully Registered!"), | |
)); | |
setState(() { | |
loading = false; | |
}); | |
Navigator.push( | |
context, MaterialPageRoute(builder: (context) => ProfilePage())); | |
} else if (response.data["status"] == "UsernameExists") { | |
setState(() { | |
loading = false; | |
}); | |
ScaffoldMessenger.of(context).showSnackBar(SnackBar( | |
content: Text(response.data["message"]), | |
)); | |
} else if (response.statusCode == 500) { | |
setState(() { | |
loading = false; | |
}); | |
ScaffoldMessenger.of(context).showSnackBar(SnackBar( | |
content: Text("Server Error!. Cant load Your Profile Data!"), | |
)); | |
} else { | |
setState(() { | |
loading = false; | |
}); | |
ScaffoldMessenger.of(context).showSnackBar(SnackBar( | |
content: Text("Unknown Error! Please contact admin panel."), | |
)); | |
} | |
} catch (e) { | |
setState(() { | |
loading = false; | |
}); | |
ScaffoldMessenger.of(context).showSnackBar(SnackBar( | |
content: | |
Text("Unknown Error! Please contact admin panel. Error is : $e"), | |
)); | |
print(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment