Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.12 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:intl/intl.dart';
  3. import 'package:shared_preferences/shared_preferences.dart';
  4. import 'login.dart';
  5. import '../configuration.dart';
  6.  
  7. import 'package:http/http.dart' as http;
  8. import 'dart:convert';
  9. import 'package:image_picker/image_picker.dart';
  10. import 'dart:io';
  11. import 'dart:async';
  12.  
  13. import 'package:path/path.dart' as path;
  14. import 'package:async/async.dart';
  15.  
  16. class ProfilePage extends StatefulWidget {
  17. @override
  18. MapScreenState createState() => MapScreenState();
  19. }
  20.  
  21. class MapScreenState extends State<ProfilePage>
  22. with SingleTickerProviderStateMixin {
  23. bool _status = true;
  24. final FocusNode myFocusNode = FocusNode();
  25. String _date = DateFormat('yyyy-MM-dd').format(DateTime.now());
  26. SharedPreferences prefs;
  27. String userId,userGender,userFullname,userPoints,userProfilImage,userBirthDate,userEmail,userMobilephone,userAddress;
  28.  
  29. TextEditingController fullnameController = new TextEditingController();
  30. TextEditingController emailController = new TextEditingController();
  31. TextEditingController dateController = new TextEditingController();
  32. TextEditingController mobilephoneController = new TextEditingController();
  33. TextEditingController addressController = new TextEditingController();
  34.  
  35. int gender = 0;
  36. int _groupValue = 0;
  37.  
  38. final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  39. bool _autoValidate = false;
  40. bool hasProfilImage = false;
  41.  
  42. File _image;
  43.  
  44. Future<File> file;
  45. String status = '';
  46. String base64Image;
  47. File tmpFile;
  48. String errMessage = 'Error Uploading Image';
  49.  
  50. Future getImage() async {
  51. var image = await ImagePicker.pickImage(source: ImageSource.gallery);
  52. setState(() {
  53. if(image != null) {
  54. _image = image;
  55. _upload().then((isSuccess){
  56. setState(() {
  57. hasProfilImage = true;
  58. });
  59. });
  60. }
  61. });
  62. }
  63.  
  64. @override
  65. void initState() {
  66. super.initState();
  67. getCredential();
  68. }
  69. getCredential() async {
  70. prefs = await SharedPreferences.getInstance();
  71.  
  72. userFullname = prefs.getString("userFullname");
  73. userEmail = prefs.getString("userEmail");
  74. userProfilImage = prefs.getString("userProfilImage");
  75. userPoints = prefs.getString("userPoints");
  76. userBirthDate = prefs.getString("userBirthDate");
  77. userMobilephone = prefs.getString("userMobilephone");
  78. userAddress = prefs.getString("userAddress");
  79. userGender = prefs.getString("userGender");
  80. userId = prefs.getString("userId");
  81.  
  82. fullnameController.text = userFullname;
  83. emailController.text = userEmail;
  84. dateController.text = userBirthDate;
  85. mobilephoneController.text = userMobilephone;
  86. addressController.text = userAddress;
  87.  
  88. setState(() {
  89. if(userProfilImage == null){
  90. hasProfilImage = false;
  91. }else{
  92. hasProfilImage = true;
  93. }
  94. _groupValue = int.parse(userGender);
  95. });
  96. }
  97.  
  98.  
  99. logout() async {
  100. prefs = await SharedPreferences.getInstance();
  101. prefs.clear();
  102. Navigator.pushReplacement(
  103. context,
  104. MaterialPageRoute(builder: (context) => new LoginScreen()),
  105. );
  106.  
  107. }
  108. @override
  109. Widget build(BuildContext context) {
  110. return new Scaffold(
  111. body: new Container(
  112. color: Colors.white,
  113. child: new ListView(
  114. children: <Widget>[
  115. Form(
  116. key: _formKey,
  117. autovalidate: _autoValidate,
  118. child:Column(
  119. children: <Widget>[
  120. new Container(
  121. height: 250.0,
  122. color: Colors.white,
  123. child: new Column(
  124. children: <Widget>[
  125. Padding(
  126. padding: EdgeInsets.only(top: 20.0),
  127. child: new Stack(fit: StackFit.loose, children: <Widget>[
  128. new Row(
  129. crossAxisAlignment: CrossAxisAlignment.center,
  130. mainAxisAlignment: MainAxisAlignment.center,
  131. children: <Widget>[
  132. new Container(
  133. child: _profileAvatar(),
  134. width: 150.0,
  135. height: 150.0,
  136. ),
  137. ],
  138. ),
  139. Padding(
  140. padding: EdgeInsets.only(top: 90.0, right: 100.0),
  141. child: new Row(
  142. mainAxisAlignment: MainAxisAlignment.center,
  143. children: <Widget>[
  144. new CircleAvatar(
  145. backgroundColor: Colors.brown,
  146. radius: 25.0,
  147. child: new IconButton(
  148. icon:Icon(Icons.camera_alt,color: Colors.white,),
  149. color: Colors.white,
  150. onPressed: () {
  151. getImage();
  152. } ,
  153. ),
  154. )
  155. ],
  156. )),
  157. ]),
  158. )
  159. ],
  160. ),
  161. ),
  162. new Container(
  163. color: Color(0xffFFFFFF),
  164. child: Padding(
  165. padding: EdgeInsets.only(bottom: 25.0),
  166. child: new Column(
  167. crossAxisAlignment: CrossAxisAlignment.start,
  168. mainAxisAlignment: MainAxisAlignment.start,
  169. children: <Widget>[
  170. Padding(
  171. padding: EdgeInsets.only(
  172. left: 25.0, right: 25.0, top: 25.0),
  173. child: new Row(
  174. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  175. mainAxisSize: MainAxisSize.max,
  176. children: <Widget>[
  177. new Column(
  178. mainAxisAlignment: MainAxisAlignment.start,
  179. mainAxisSize: MainAxisSize.min,
  180. children: <Widget>[
  181. new Text(
  182. 'Parsonal Information',
  183. style: TextStyle(
  184. fontSize: 18.0,
  185. fontWeight: FontWeight.bold),
  186. ),
  187. ],
  188. ),
  189. new Column(
  190. mainAxisAlignment: MainAxisAlignment.end,
  191. mainAxisSize: MainAxisSize.min,
  192. children: <Widget>[
  193. _status ? _getEditIcon() : new Container(),
  194. ],
  195. )
  196. ],
  197. )),
  198. Padding(
  199. padding: EdgeInsets.only(
  200. left: 25.0, right: 25.0, top: 25.0),
  201. child: new Row(
  202. mainAxisSize: MainAxisSize.max,
  203. children: <Widget>[
  204. new Column(
  205. mainAxisAlignment: MainAxisAlignment.start,
  206. mainAxisSize: MainAxisSize.min,
  207. children: <Widget>[
  208. new Text(
  209. 'Name',
  210. style: TextStyle(
  211. fontSize: 16.0,
  212. fontWeight: FontWeight.bold),
  213. ),
  214. ],
  215. ),
  216. ],
  217. )),
  218. Padding(
  219. padding: EdgeInsets.only(
  220. left: 25.0, right: 25.0, top: 2.0),
  221. child: new Row(
  222. mainAxisSize: MainAxisSize.max,
  223. children: <Widget>[
  224. new Flexible(
  225. child: new TextFormField(
  226. validator:validateFullname,
  227. controller: fullnameController,
  228. decoration: const InputDecoration(
  229. hintText: "Enter Your Name",
  230. ),
  231. enabled: !_status,
  232. autofocus: !_status,
  233.  
  234. ),
  235. ),
  236. ],
  237. )),
  238. Padding(
  239. padding: EdgeInsets.only(
  240. left: 25.0, right: 25.0, top: 25.0),
  241. child: new Row(
  242. mainAxisSize: MainAxisSize.max,
  243. children: <Widget>[
  244. new Column(
  245. mainAxisAlignment: MainAxisAlignment.start,
  246. mainAxisSize: MainAxisSize.min,
  247. children: <Widget>[
  248. new Text(
  249. 'Email',
  250. style: TextStyle(
  251. fontSize: 16.0,
  252. fontWeight: FontWeight.bold),
  253. ),
  254. ],
  255. ),
  256. ],
  257. )),
  258. Padding(
  259. padding: EdgeInsets.only(
  260. left: 25.0, right: 25.0, top: 2.0),
  261. child: new Row(
  262. mainAxisSize: MainAxisSize.max,
  263. children: <Widget>[
  264. new Flexible(
  265. child: new TextFormField(
  266. validator:validateEmail,
  267. controller: emailController,
  268. decoration: const InputDecoration(
  269. hintText: "Enter Email"),
  270. enabled: false,
  271. ),
  272. ),
  273. ],
  274. )),
  275. Padding(
  276. padding: EdgeInsets.only(
  277. left: 25.0, right: 25.0, top: 25.0),
  278. child: new Row(
  279. mainAxisSize: MainAxisSize.max,
  280. children: <Widget>[
  281. new Column(
  282. mainAxisAlignment: MainAxisAlignment.start,
  283. mainAxisSize: MainAxisSize.min,
  284. children: <Widget>[
  285. new Text(
  286. 'Date Of Birth',
  287. style: TextStyle(
  288. fontSize: 16.0,
  289. fontWeight: FontWeight.bold),
  290. ),
  291. ],
  292. ),
  293. ],
  294. )),
  295. Padding(
  296. padding: EdgeInsets.only(
  297. left: 25.0, right: 25.0, top: 2.0),
  298. child: new Row(
  299. mainAxisSize: MainAxisSize.max,
  300. children: <Widget>[
  301. new Flexible(
  302. child: InkWell(
  303.  
  304. child: IgnorePointer(
  305. child:new TextFormField(
  306. validator: validateDOB,
  307. enabled: !_status,
  308. controller: dateController,
  309. decoration: new InputDecoration(
  310. filled: true,
  311. hintStyle:
  312. new TextStyle(color: Colors.grey[800]),
  313. fillColor: Colors.white70),
  314. ),
  315. ),
  316. onTap: () => { _selectDate(context) }
  317. ),
  318. ),
  319. ],
  320. )),
  321. Padding(
  322. padding: EdgeInsets.only(
  323. left: 25.0, right: 25.0, top: 25.0),
  324. child: new Row(
  325. mainAxisSize: MainAxisSize.max,
  326. children: <Widget>[
  327. new Column(
  328. mainAxisAlignment: MainAxisAlignment.start,
  329. mainAxisSize: MainAxisSize.min,
  330. children: <Widget>[
  331. new Text(
  332. 'Mobile',
  333. style: TextStyle(
  334. fontSize: 16.0,
  335. fontWeight: FontWeight.bold),
  336. ),
  337. ],
  338. ),
  339. ],
  340. )),
  341. Padding(
  342. padding: EdgeInsets.only(
  343. left: 25.0, right: 25.0, top: 2.0),
  344. child: new Row(
  345. mainAxisSize: MainAxisSize.max,
  346. children: <Widget>[
  347. new Flexible(
  348. child: new TextField(
  349. controller: mobilephoneController,
  350.  
  351. decoration: const InputDecoration(
  352. hintText: "Enter Mobile Number"),
  353. enabled: !_status,
  354. ),
  355. ),
  356. ],
  357. )),
  358. Padding(
  359. padding: EdgeInsets.only(
  360. left: 25.0, right: 25.0, top: 25.0),
  361. child: new Row(
  362. mainAxisSize: MainAxisSize.max,
  363. children: <Widget>[
  364. new Column(
  365. mainAxisAlignment: MainAxisAlignment.start,
  366. mainAxisSize: MainAxisSize.min,
  367. children: <Widget>[
  368. new Text(
  369. 'Address',
  370. style: TextStyle(
  371. fontSize: 16.0,
  372. fontWeight: FontWeight.bold),
  373. ),
  374. ],
  375. ),
  376. ],
  377. )
  378. ),
  379. Padding(
  380. padding: EdgeInsets.only(
  381. left: 25.0, right: 25.0, top: 2.0),
  382. child: new Row(
  383. mainAxisSize: MainAxisSize.max,
  384. children: <Widget>[
  385. new Flexible(
  386. child: new TextFormField(
  387. validator: validateAddress,
  388. controller: addressController,
  389. decoration: const InputDecoration(
  390. hintText: "Address"),
  391. enabled: !_status,
  392. ),
  393. ),
  394. ],
  395. )
  396. ),
  397. Padding(
  398. padding: EdgeInsets.only(
  399. left: 25.0, right: 25.0, top: 25.0),
  400. child: new Row(
  401. mainAxisSize: MainAxisSize.max,
  402. children: <Widget>[
  403. new Column(
  404. mainAxisAlignment: MainAxisAlignment.start,
  405. mainAxisSize: MainAxisSize.min,
  406. children: <Widget>[
  407. new Text(
  408. 'Gender',
  409. style: TextStyle(
  410. fontSize: 16.0,
  411. fontWeight: FontWeight.bold),
  412. ),
  413. ],
  414. ),
  415. ],
  416. )
  417. ),
  418. Padding(
  419. padding: EdgeInsets.only(
  420. left: 25.0, right: 25.0, top: 2.0),
  421. child: Column(
  422. children: <Widget>[
  423. _myRadioButton(
  424. title: "Male",
  425. value: 0,
  426. onChanged: (newValue) => setState(() => _groupValue = newValue),
  427. ),
  428. _myRadioButton(
  429. title: "Female",
  430. value: 1,
  431. onChanged: (newValue) => setState(() => _groupValue = newValue),
  432. ),
  433. ],
  434. )
  435. ),
  436. !_status ? _getActionButtons() : new Container(),
  437. Padding(
  438. padding: EdgeInsets.only(
  439. left: 25.0, right: 25.0, top: 10.0),
  440. child: new Row(
  441. mainAxisAlignment: MainAxisAlignment.center,
  442. children: <Widget>[
  443. Center(
  444. child: RaisedButton(
  445. color: Colors.red,
  446. textColor: Colors.white,
  447. onPressed: () {logout();},
  448. child: new Row(
  449. mainAxisAlignment: MainAxisAlignment.center,
  450. mainAxisSize: MainAxisSize.min,
  451. children: <Widget>[
  452. new Text('Logout'),
  453. SizedBox(width: 5),
  454. new Icon(Icons.exit_to_app),
  455. ],
  456. ),
  457. ),
  458. )
  459.  
  460. ],
  461. )),
  462. ],
  463. ),
  464. ),
  465. )
  466. ],
  467. )
  468. ),
  469.  
  470. ],
  471. ),
  472. ));
  473. }
  474.  
  475. @override
  476. void dispose() {
  477. myFocusNode.dispose();
  478. super.dispose();
  479. }
  480.  
  481. Widget _profileAvatar(){
  482. if(!hasProfilImage){
  483. return CircleAvatar(
  484. radius: 30.0,
  485. backgroundImage:
  486. new ExactAssetImage('assets/images/default_profile.png'),
  487. backgroundColor: Colors.transparent,
  488. );
  489. }else {
  490. return CircleAvatar(
  491. radius: 30.0,
  492. backgroundImage:
  493. NetworkImage( "${Configuration.url}assets/profileImage/${userProfilImage}"),
  494. backgroundColor: Colors.transparent,
  495. );
  496. }
  497. }
  498. Widget _getActionButtons() {
  499. return Padding(
  500. padding: EdgeInsets.only(left: 25.0, right: 25.0, top: 45.0),
  501. child: new Row(
  502. mainAxisSize: MainAxisSize.max,
  503. mainAxisAlignment: MainAxisAlignment.start,
  504. children: <Widget>[
  505. Expanded(
  506. child: Padding(
  507. padding: EdgeInsets.only(right: 10.0),
  508. child: Container(
  509. child: new RaisedButton(
  510. child: new Text("Save"),
  511. textColor: Colors.white,
  512. color: Colors.green,
  513. onPressed: () => _validateInputs(),
  514. shape: new RoundedRectangleBorder(
  515. borderRadius: new BorderRadius.circular(20.0)),
  516. )),
  517. ),
  518. flex: 2,
  519. ),
  520. Expanded(
  521. child: Padding(
  522. padding: EdgeInsets.only(left: 10.0),
  523. child: Container(
  524. child: new RaisedButton(
  525. child: new Text("Cancel"),
  526. textColor: Colors.white,
  527. color: Colors.orange,
  528. onPressed: () {
  529. setState(() {
  530. _status = true;
  531. FocusScope.of(context).requestFocus(new FocusNode());
  532. });
  533. },
  534. shape: new RoundedRectangleBorder(
  535. borderRadius: new BorderRadius.circular(20.0)),
  536. )),
  537. ),
  538. flex: 2,
  539. ),
  540. ],
  541. ),
  542. );
  543. }
  544. Widget _getEditIcon() {
  545. return new GestureDetector(
  546. child: new CircleAvatar(
  547. backgroundColor: Colors.brown,
  548. radius: 14.0,
  549. child: new Icon(
  550. Icons.edit,
  551. color: Colors.white,
  552. size: 16.0,
  553. ),
  554. ),
  555. onTap: () {
  556. setState(() {
  557. _status = false;
  558. });
  559. },
  560. );
  561. }
  562. Widget _myRadioButton({String title, int value, Function onChanged}) {
  563. return RadioListTile(
  564. value: value,
  565. groupValue: _groupValue,
  566. onChanged: !_status ? onChanged:null,
  567. title: Text(title),
  568. );
  569. }
  570.  
  571. Future<void> _selectDate(BuildContext context) async {
  572. if(!_status) {
  573. dateController.text = DateFormat('yyyy-MM-dd').format(DateTime.now());
  574. final DateTime picked = await showDatePicker(
  575. context: context,
  576. initialDate: DateTime.parse(_date),
  577. firstDate: DateTime(1970),
  578. lastDate: DateTime.now().add(Duration(days: 30)),
  579. );
  580. if (picked != null && picked != _date) {
  581. setState(() {
  582. _date = picked.toString();
  583. var Datess = DateTime.parse(_date);
  584. dateController.text = DateFormat('yyyy-MM-dd').format(Datess);
  585. });
  586. }
  587. }else{
  588. return ;
  589. }
  590. }
  591.  
  592. _upload() async {
  593. var stream = new http.ByteStream(DelegatingStream.typed(_image.openRead()));
  594. var length = await _image.length();
  595. var uri = Uri.parse(Configuration.url+"api/profileImage/"+userId);
  596. var request = http.MultipartRequest("POST", uri);
  597. var multipartFile = new http.MultipartFile('userfile', stream, length,filename: path.basename(_image.path));
  598. request.files.add(multipartFile);
  599. var response = await request.send();
  600. print(response);
  601. response.stream.transform(utf8.decoder).listen((value) {
  602. final JsonDecoder _decoder = new JsonDecoder();
  603. var datauser = _decoder.convert(value);
  604. prefs.setString('userProfilImage', datauser["data"]);
  605. });
  606. }
  607.  
  608.  
  609.  
  610. void _validateInputs() {
  611. if (_formKey.currentState.validate()) {
  612. setState(() {
  613. _formKey.currentState.save();
  614. _status = true;
  615. _updateProfile();
  616. });
  617. } else {
  618. setState(() {
  619. _autoValidate = true;
  620. });
  621. }
  622. }
  623. String validateEmail(String value) {
  624. if (value.length < 1) {
  625. return 'Please enter your email address';
  626. } else {
  627. Pattern pattern =
  628. 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,}))$';
  629. RegExp regex = new RegExp(pattern);
  630.  
  631. if (!regex.hasMatch(value))
  632. return 'Enter Valid Email';
  633. else
  634. return null;
  635. }
  636. }
  637. String validateFullname(String value) {
  638. if (value.length < 1)
  639. return 'Please enter your fullname';
  640. else
  641. return null;
  642. }
  643. String validateDOB(String value) {
  644. if (value.length < 6)
  645. return 'The minimum length is 6';
  646. else
  647. return null;
  648. }
  649. String validateMobile(String value) {
  650. if (value.length < 0)
  651. return 'Please enter your mobile phone';
  652. else
  653. return null;
  654. }
  655. String validateAddress(String value) {
  656. if (value.length < 0)
  657. return 'Please enter your address';
  658. else
  659. return null;
  660. }
  661.  
  662. Future<dynamic> _updateProfile() async {
  663. final JsonDecoder _decoder = new JsonDecoder();
  664. try {
  665. final response =
  666. await http.post(Configuration.url+"api/updateProfile", body: {
  667. "fullname" : fullnameController.text,
  668. "dob" : dateController.text,
  669. "mobile" : mobilephoneController.text,
  670. "addres" : addressController.text,
  671. "email" : emailController.text,
  672. "gender" : _groupValue.toString()
  673. });
  674. if (response.statusCode < 200 || response.statusCode > 300) {
  675. throw new Exception('Failed to fetch data');
  676. } else {
  677. var datauser = _decoder.convert(response.body);
  678. if (datauser["err"] == 0) {
  679.  
  680. } else {
  681. showDialog(
  682. context: context,
  683. builder: (_) => new AlertDialog(
  684. content: new Text("Wooops, something wrong. Please try again"),
  685. ));
  686. }
  687. }
  688. } catch (e) {
  689. showDialog(
  690. context: context,
  691. builder: (_) => new AlertDialog(
  692. content: new Text("Wooops, something wrong"),
  693. ));
  694. return null;
  695. }
  696. }
  697.  
  698.  
  699.  
  700. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement