Skip to content

Commit f5bc899

Browse files
committed
get profile connected
1 parent 8f43ef6 commit f5bc899

File tree

4 files changed

+120
-8
lines changed

4 files changed

+120
-8
lines changed

lib/NetworkHandler.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import 'dart:convert';
22

3+
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
34
import 'package:http/http.dart' as http;
45
import 'package:logger/logger.dart';
56

67
class NetworkHandler {
7-
String baseurl = "https://sheltered-waters-80365.herokuapp.com";
8+
String baseurl = "http://192.168.43.92:5000";
89
var log = Logger();
9-
10+
FlutterSecureStorage storage = FlutterSecureStorage();
1011
Future get(String url) async {
12+
String token = await storage.read(key: "token");
1113
url = formater(url);
1214
// /user/register
13-
var response = await http.get(url);
15+
var response = await http.get(
16+
url,
17+
headers: {"Authorization": "Bearer $token"},
18+
);
1419
if (response.statusCode == 200 || response.statusCode == 201) {
1520
log.i(response.body);
1621

@@ -21,11 +26,15 @@ class NetworkHandler {
2126
}
2227

2328
Future<http.Response> post(String url, Map<String, String> body) async {
29+
String token = await storage.read(key: "token");
2430
url = formater(url);
2531
log.d(body);
2632
var response = await http.post(
2733
url,
28-
headers: {"Content-type": "application/json"},
34+
headers: {
35+
"Content-type": "application/json",
36+
"Authorization": "Bearer $token"
37+
},
2938
body: json.encode(body),
3039
);
3140
return response;

lib/Pages/SignUpPage.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class _SignUpPageState extends State<SignUpPage> {
7878
var responseRegister =
7979
await networkHandler.post("/user/register", data);
8080

81-
//Login Logic added here
81+
//Login Logic added here
8282
if (responseRegister.statusCode == 200 ||
8383
responseRegister.statusCode == 201) {
8484
Map<String, String> data = {

lib/Profile/CreatProfile.dart

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:io';
22

3+
import 'package:blogapp/NetworkHandler.dart';
34
import 'package:flutter/material.dart';
45
import 'package:image_picker/image_picker.dart';
56

@@ -11,14 +12,23 @@ class CreatProfile extends StatefulWidget {
1112
}
1213

1314
class _CreatProfileState extends State<CreatProfile> {
15+
final networkHandler = NetworkHandler();
16+
1417
PickedFile _imageFile;
18+
final _globalkey = GlobalKey<FormState>();
19+
TextEditingController _name = TextEditingController();
20+
TextEditingController _profession = TextEditingController();
21+
TextEditingController _dob = TextEditingController();
22+
TextEditingController _title = TextEditingController();
23+
TextEditingController _about = TextEditingController();
1524
final ImagePicker _picker = ImagePicker();
1625
@override
1726
Widget build(BuildContext context) {
1827
return Scaffold(
19-
body: Padding(
20-
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 20),
28+
body: Form(
29+
key: _globalkey,
2130
child: ListView(
31+
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 30),
2232
children: <Widget>[
2333
imageProfile(),
2434
SizedBox(
@@ -44,6 +54,42 @@ class _CreatProfileState extends State<CreatProfile> {
4454
SizedBox(
4555
height: 20,
4656
),
57+
InkWell(
58+
onTap: () async {
59+
if (_globalkey.currentState.validate()) {
60+
Map<String, String> data = {
61+
"name": _name.text,
62+
"profession": _profession.text,
63+
"dob": _dob.text,
64+
"title": _title.text,
65+
"about": _about.text,
66+
};
67+
var response =
68+
await networkHandler.post("/profile/add", data);
69+
print(response.statusCode);
70+
}
71+
},
72+
child: Center(
73+
child: Container(
74+
width: 200,
75+
height: 50,
76+
decoration: BoxDecoration(
77+
color: Colors.teal,
78+
borderRadius: BorderRadius.circular(10),
79+
),
80+
child: Center(
81+
child: Text(
82+
"Submit",
83+
style: TextStyle(
84+
color: Colors.white,
85+
fontSize: 18,
86+
fontWeight: FontWeight.bold,
87+
),
88+
),
89+
),
90+
),
91+
),
92+
),
4793
],
4894
),
4995
),
@@ -131,6 +177,12 @@ class _CreatProfileState extends State<CreatProfile> {
131177

132178
Widget nameTextField() {
133179
return TextFormField(
180+
controller: _name,
181+
validator: (value) {
182+
if (value.isEmpty) return "Name can't be empty";
183+
184+
return null;
185+
},
134186
decoration: InputDecoration(
135187
border: OutlineInputBorder(
136188
borderSide: BorderSide(
@@ -154,6 +206,12 @@ class _CreatProfileState extends State<CreatProfile> {
154206

155207
Widget professionTextField() {
156208
return TextFormField(
209+
controller: _profession,
210+
validator: (value) {
211+
if (value.isEmpty) return "Profession can't be empty";
212+
213+
return null;
214+
},
157215
decoration: InputDecoration(
158216
border: OutlineInputBorder(
159217
borderSide: BorderSide(
@@ -177,6 +235,12 @@ class _CreatProfileState extends State<CreatProfile> {
177235

178236
Widget dobField() {
179237
return TextFormField(
238+
controller: _dob,
239+
validator: (value) {
240+
if (value.isEmpty) return "DOB can't be empty";
241+
242+
return null;
243+
},
180244
decoration: InputDecoration(
181245
border: OutlineInputBorder(
182246
borderSide: BorderSide(
@@ -200,6 +264,12 @@ class _CreatProfileState extends State<CreatProfile> {
200264

201265
Widget titleTextField() {
202266
return TextFormField(
267+
controller: _title,
268+
validator: (value) {
269+
if (value.isEmpty) return "Title can't be empty";
270+
271+
return null;
272+
},
203273
decoration: InputDecoration(
204274
border: OutlineInputBorder(
205275
borderSide: BorderSide(
@@ -223,6 +293,12 @@ class _CreatProfileState extends State<CreatProfile> {
223293

224294
Widget aboutTextField() {
225295
return TextFormField(
296+
controller: _about,
297+
validator: (value) {
298+
if (value.isEmpty) return "About can't be empty";
299+
300+
return null;
301+
},
226302
maxLines: 4,
227303
decoration: InputDecoration(
228304
border: OutlineInputBorder(

lib/Profile/ProfileScreen.dart

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:blogapp/NetworkHandler.dart';
12
import 'package:blogapp/Profile/CreatProfile.dart';
23
import 'package:flutter/material.dart';
34

@@ -9,13 +10,39 @@ class ProfileScreen extends StatefulWidget {
910
}
1011

1112
class _ProfileScreenState extends State<ProfileScreen> {
13+
NetworkHandler networkHandler = NetworkHandler();
14+
Widget page = CircularProgressIndicator();
15+
@override
16+
void initState() {
17+
// TODO: implement initState
18+
super.initState();
19+
checkProfile();
20+
}
21+
22+
void checkProfile() async {
23+
var response = await networkHandler.get("/profile/checkProfile");
24+
if (response["status"] == true) {
25+
setState(() {
26+
page = showProfile();
27+
});
28+
} else {
29+
setState(() {
30+
page = button();
31+
});
32+
}
33+
}
34+
1235
@override
1336
Widget build(BuildContext context) {
1437
return Scaffold(
15-
body: button(),
38+
body: page,
1639
);
1740
}
1841

42+
Widget showProfile() {
43+
return Center(child: Text("Profile Data is Avalable"));
44+
}
45+
1946
Widget button() {
2047
return Padding(
2148
padding: const EdgeInsets.symmetric(horizontal: 70),

0 commit comments

Comments
 (0)