Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:intl/intl.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- import 'login.dart';
- import '../configuration.dart';
- import 'package:http/http.dart' as http;
- import 'dart:convert';
- import 'package:image_picker/image_picker.dart';
- import 'dart:io';
- import 'dart:async';
- import 'package:path/path.dart' as path;
- import 'package:async/async.dart';
- class ProfilePage extends StatefulWidget {
- @override
- MapScreenState createState() => MapScreenState();
- }
- class MapScreenState extends State<ProfilePage>
- with SingleTickerProviderStateMixin {
- bool _status = true;
- final FocusNode myFocusNode = FocusNode();
- String _date = DateFormat('yyyy-MM-dd').format(DateTime.now());
- SharedPreferences prefs;
- String userId,userGender,userFullname,userPoints,userProfilImage,userBirthDate,userEmail,userMobilephone,userAddress;
- TextEditingController fullnameController = new TextEditingController();
- TextEditingController emailController = new TextEditingController();
- TextEditingController dateController = new TextEditingController();
- TextEditingController mobilephoneController = new TextEditingController();
- TextEditingController addressController = new TextEditingController();
- int gender = 0;
- int _groupValue = 0;
- final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
- bool _autoValidate = false;
- bool hasProfilImage = false;
- File _image;
- Future<File> file;
- String status = '';
- String base64Image;
- File tmpFile;
- String errMessage = 'Error Uploading Image';
- Future getImage() async {
- var image = await ImagePicker.pickImage(source: ImageSource.gallery);
- setState(() {
- if(image != null) {
- _image = image;
- _upload().then((isSuccess){
- setState(() {
- hasProfilImage = true;
- });
- });
- }
- });
- }
- @override
- void initState() {
- super.initState();
- getCredential();
- }
- getCredential() async {
- prefs = await SharedPreferences.getInstance();
- userFullname = prefs.getString("userFullname");
- userEmail = prefs.getString("userEmail");
- userProfilImage = prefs.getString("userProfilImage");
- userPoints = prefs.getString("userPoints");
- userBirthDate = prefs.getString("userBirthDate");
- userMobilephone = prefs.getString("userMobilephone");
- userAddress = prefs.getString("userAddress");
- userGender = prefs.getString("userGender");
- userId = prefs.getString("userId");
- fullnameController.text = userFullname;
- emailController.text = userEmail;
- dateController.text = userBirthDate;
- mobilephoneController.text = userMobilephone;
- addressController.text = userAddress;
- setState(() {
- if(userProfilImage == null){
- hasProfilImage = false;
- }else{
- hasProfilImage = true;
- }
- _groupValue = int.parse(userGender);
- });
- }
- logout() async {
- prefs = await SharedPreferences.getInstance();
- prefs.clear();
- Navigator.pushReplacement(
- context,
- MaterialPageRoute(builder: (context) => new LoginScreen()),
- );
- }
- @override
- Widget build(BuildContext context) {
- return new Scaffold(
- body: new Container(
- color: Colors.white,
- child: new ListView(
- children: <Widget>[
- Form(
- key: _formKey,
- autovalidate: _autoValidate,
- child:Column(
- children: <Widget>[
- new Container(
- height: 250.0,
- color: Colors.white,
- child: new Column(
- children: <Widget>[
- Padding(
- padding: EdgeInsets.only(top: 20.0),
- child: new Stack(fit: StackFit.loose, children: <Widget>[
- new Row(
- crossAxisAlignment: CrossAxisAlignment.center,
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- new Container(
- child: _profileAvatar(),
- width: 150.0,
- height: 150.0,
- ),
- ],
- ),
- Padding(
- padding: EdgeInsets.only(top: 90.0, right: 100.0),
- child: new Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- new CircleAvatar(
- backgroundColor: Colors.brown,
- radius: 25.0,
- child: new IconButton(
- icon:Icon(Icons.camera_alt,color: Colors.white,),
- color: Colors.white,
- onPressed: () {
- getImage();
- } ,
- ),
- )
- ],
- )),
- ]),
- )
- ],
- ),
- ),
- new Container(
- color: Color(0xffFFFFFF),
- child: Padding(
- padding: EdgeInsets.only(bottom: 25.0),
- child: new Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- mainAxisAlignment: MainAxisAlignment.start,
- children: <Widget>[
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 25.0),
- child: new Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- new Column(
- mainAxisAlignment: MainAxisAlignment.start,
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- new Text(
- 'Parsonal Information',
- style: TextStyle(
- fontSize: 18.0,
- fontWeight: FontWeight.bold),
- ),
- ],
- ),
- new Column(
- mainAxisAlignment: MainAxisAlignment.end,
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- _status ? _getEditIcon() : new Container(),
- ],
- )
- ],
- )),
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 25.0),
- child: new Row(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- new Column(
- mainAxisAlignment: MainAxisAlignment.start,
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- new Text(
- 'Name',
- style: TextStyle(
- fontSize: 16.0,
- fontWeight: FontWeight.bold),
- ),
- ],
- ),
- ],
- )),
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 2.0),
- child: new Row(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- new Flexible(
- child: new TextFormField(
- validator:validateFullname,
- controller: fullnameController,
- decoration: const InputDecoration(
- hintText: "Enter Your Name",
- ),
- enabled: !_status,
- autofocus: !_status,
- ),
- ),
- ],
- )),
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 25.0),
- child: new Row(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- new Column(
- mainAxisAlignment: MainAxisAlignment.start,
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- new Text(
- 'Email',
- style: TextStyle(
- fontSize: 16.0,
- fontWeight: FontWeight.bold),
- ),
- ],
- ),
- ],
- )),
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 2.0),
- child: new Row(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- new Flexible(
- child: new TextFormField(
- validator:validateEmail,
- controller: emailController,
- decoration: const InputDecoration(
- hintText: "Enter Email"),
- enabled: false,
- ),
- ),
- ],
- )),
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 25.0),
- child: new Row(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- new Column(
- mainAxisAlignment: MainAxisAlignment.start,
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- new Text(
- 'Date Of Birth',
- style: TextStyle(
- fontSize: 16.0,
- fontWeight: FontWeight.bold),
- ),
- ],
- ),
- ],
- )),
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 2.0),
- child: new Row(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- new Flexible(
- child: InkWell(
- child: IgnorePointer(
- child:new TextFormField(
- validator: validateDOB,
- enabled: !_status,
- controller: dateController,
- decoration: new InputDecoration(
- filled: true,
- hintStyle:
- new TextStyle(color: Colors.grey[800]),
- fillColor: Colors.white70),
- ),
- ),
- onTap: () => { _selectDate(context) }
- ),
- ),
- ],
- )),
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 25.0),
- child: new Row(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- new Column(
- mainAxisAlignment: MainAxisAlignment.start,
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- new Text(
- 'Mobile',
- style: TextStyle(
- fontSize: 16.0,
- fontWeight: FontWeight.bold),
- ),
- ],
- ),
- ],
- )),
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 2.0),
- child: new Row(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- new Flexible(
- child: new TextField(
- controller: mobilephoneController,
- decoration: const InputDecoration(
- hintText: "Enter Mobile Number"),
- enabled: !_status,
- ),
- ),
- ],
- )),
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 25.0),
- child: new Row(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- new Column(
- mainAxisAlignment: MainAxisAlignment.start,
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- new Text(
- 'Address',
- style: TextStyle(
- fontSize: 16.0,
- fontWeight: FontWeight.bold),
- ),
- ],
- ),
- ],
- )
- ),
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 2.0),
- child: new Row(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- new Flexible(
- child: new TextFormField(
- validator: validateAddress,
- controller: addressController,
- decoration: const InputDecoration(
- hintText: "Address"),
- enabled: !_status,
- ),
- ),
- ],
- )
- ),
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 25.0),
- child: new Row(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- new Column(
- mainAxisAlignment: MainAxisAlignment.start,
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- new Text(
- 'Gender',
- style: TextStyle(
- fontSize: 16.0,
- fontWeight: FontWeight.bold),
- ),
- ],
- ),
- ],
- )
- ),
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 2.0),
- child: Column(
- children: <Widget>[
- _myRadioButton(
- title: "Male",
- value: 0,
- onChanged: (newValue) => setState(() => _groupValue = newValue),
- ),
- _myRadioButton(
- title: "Female",
- value: 1,
- onChanged: (newValue) => setState(() => _groupValue = newValue),
- ),
- ],
- )
- ),
- !_status ? _getActionButtons() : new Container(),
- Padding(
- padding: EdgeInsets.only(
- left: 25.0, right: 25.0, top: 10.0),
- child: new Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Center(
- child: RaisedButton(
- color: Colors.red,
- textColor: Colors.white,
- onPressed: () {logout();},
- child: new Row(
- mainAxisAlignment: MainAxisAlignment.center,
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- new Text('Logout'),
- SizedBox(width: 5),
- new Icon(Icons.exit_to_app),
- ],
- ),
- ),
- )
- ],
- )),
- ],
- ),
- ),
- )
- ],
- )
- ),
- ],
- ),
- ));
- }
- @override
- void dispose() {
- myFocusNode.dispose();
- super.dispose();
- }
- Widget _profileAvatar(){
- if(!hasProfilImage){
- return CircleAvatar(
- radius: 30.0,
- backgroundImage:
- new ExactAssetImage('assets/images/default_profile.png'),
- backgroundColor: Colors.transparent,
- );
- }else {
- return CircleAvatar(
- radius: 30.0,
- backgroundImage:
- NetworkImage( "${Configuration.url}assets/profileImage/${userProfilImage}"),
- backgroundColor: Colors.transparent,
- );
- }
- }
- Widget _getActionButtons() {
- return Padding(
- padding: EdgeInsets.only(left: 25.0, right: 25.0, top: 45.0),
- child: new Row(
- mainAxisSize: MainAxisSize.max,
- mainAxisAlignment: MainAxisAlignment.start,
- children: <Widget>[
- Expanded(
- child: Padding(
- padding: EdgeInsets.only(right: 10.0),
- child: Container(
- child: new RaisedButton(
- child: new Text("Save"),
- textColor: Colors.white,
- color: Colors.green,
- onPressed: () => _validateInputs(),
- shape: new RoundedRectangleBorder(
- borderRadius: new BorderRadius.circular(20.0)),
- )),
- ),
- flex: 2,
- ),
- Expanded(
- child: Padding(
- padding: EdgeInsets.only(left: 10.0),
- child: Container(
- child: new RaisedButton(
- child: new Text("Cancel"),
- textColor: Colors.white,
- color: Colors.orange,
- onPressed: () {
- setState(() {
- _status = true;
- FocusScope.of(context).requestFocus(new FocusNode());
- });
- },
- shape: new RoundedRectangleBorder(
- borderRadius: new BorderRadius.circular(20.0)),
- )),
- ),
- flex: 2,
- ),
- ],
- ),
- );
- }
- Widget _getEditIcon() {
- return new GestureDetector(
- child: new CircleAvatar(
- backgroundColor: Colors.brown,
- radius: 14.0,
- child: new Icon(
- Icons.edit,
- color: Colors.white,
- size: 16.0,
- ),
- ),
- onTap: () {
- setState(() {
- _status = false;
- });
- },
- );
- }
- Widget _myRadioButton({String title, int value, Function onChanged}) {
- return RadioListTile(
- value: value,
- groupValue: _groupValue,
- onChanged: !_status ? onChanged:null,
- title: Text(title),
- );
- }
- Future<void> _selectDate(BuildContext context) async {
- if(!_status) {
- dateController.text = DateFormat('yyyy-MM-dd').format(DateTime.now());
- final DateTime picked = await showDatePicker(
- context: context,
- initialDate: DateTime.parse(_date),
- firstDate: DateTime(1970),
- lastDate: DateTime.now().add(Duration(days: 30)),
- );
- if (picked != null && picked != _date) {
- setState(() {
- _date = picked.toString();
- var Datess = DateTime.parse(_date);
- dateController.text = DateFormat('yyyy-MM-dd').format(Datess);
- });
- }
- }else{
- return ;
- }
- }
- _upload() async {
- var stream = new http.ByteStream(DelegatingStream.typed(_image.openRead()));
- var length = await _image.length();
- var uri = Uri.parse(Configuration.url+"api/profileImage/"+userId);
- var request = http.MultipartRequest("POST", uri);
- var multipartFile = new http.MultipartFile('userfile', stream, length,filename: path.basename(_image.path));
- request.files.add(multipartFile);
- var response = await request.send();
- print(response);
- response.stream.transform(utf8.decoder).listen((value) {
- final JsonDecoder _decoder = new JsonDecoder();
- var datauser = _decoder.convert(value);
- prefs.setString('userProfilImage', datauser["data"]);
- });
- }
- void _validateInputs() {
- if (_formKey.currentState.validate()) {
- setState(() {
- _formKey.currentState.save();
- _status = true;
- _updateProfile();
- });
- } else {
- setState(() {
- _autoValidate = true;
- });
- }
- }
- String validateEmail(String value) {
- if (value.length < 1) {
- return 'Please enter your email address';
- } else {
- Pattern pattern =
- r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
- RegExp regex = new RegExp(pattern);
- if (!regex.hasMatch(value))
- return 'Enter Valid Email';
- else
- return null;
- }
- }
- String validateFullname(String value) {
- if (value.length < 1)
- return 'Please enter your fullname';
- else
- return null;
- }
- String validateDOB(String value) {
- if (value.length < 6)
- return 'The minimum length is 6';
- else
- return null;
- }
- String validateMobile(String value) {
- if (value.length < 0)
- return 'Please enter your mobile phone';
- else
- return null;
- }
- String validateAddress(String value) {
- if (value.length < 0)
- return 'Please enter your address';
- else
- return null;
- }
- Future<dynamic> _updateProfile() async {
- final JsonDecoder _decoder = new JsonDecoder();
- try {
- final response =
- await http.post(Configuration.url+"api/updateProfile", body: {
- "fullname" : fullnameController.text,
- "dob" : dateController.text,
- "mobile" : mobilephoneController.text,
- "addres" : addressController.text,
- "email" : emailController.text,
- "gender" : _groupValue.toString()
- });
- if (response.statusCode < 200 || response.statusCode > 300) {
- throw new Exception('Failed to fetch data');
- } else {
- var datauser = _decoder.convert(response.body);
- if (datauser["err"] == 0) {
- } else {
- showDialog(
- context: context,
- builder: (_) => new AlertDialog(
- content: new Text("Wooops, something wrong. Please try again"),
- ));
- }
- }
- } catch (e) {
- showDialog(
- context: context,
- builder: (_) => new AlertDialog(
- content: new Text("Wooops, something wrong"),
- ));
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement