Skip to content

Commit 53fdc5b

Browse files
committed
image api connected
1 parent e480fc0 commit 53fdc5b

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

lib/NetworkHandler.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ class NetworkHandler {
4040
return response;
4141
}
4242

43+
Future<http.StreamedResponse> patchImage(String url, String filepath) async {
44+
url = formater(url);
45+
String token = await storage.read(key: "token");
46+
var request = http.MultipartRequest('PATCH', Uri.parse(url));
47+
request.files.add(await http.MultipartFile.fromPath("img", filepath));
48+
request.headers.addAll({
49+
"Content-type": "multipart/form-data",
50+
"Authorization": "Bearer $token"
51+
});
52+
var response = request.send();
53+
return response;
54+
}
55+
4356
String formater(String url) {
4457
return baseurl + url;
4558
}

lib/Profile/CreatProfile.dart

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

33
import 'package:blogapp/NetworkHandler.dart';
4+
import 'package:blogapp/Pages/HomePage.dart';
5+
import 'package:blogapp/Screen/HomeScreen.dart';
46
import 'package:flutter/material.dart';
57
import 'package:image_picker/image_picker.dart';
68

@@ -13,7 +15,7 @@ class CreatProfile extends StatefulWidget {
1315

1416
class _CreatProfileState extends State<CreatProfile> {
1517
final networkHandler = NetworkHandler();
16-
18+
bool circular = false;
1719
PickedFile _imageFile;
1820
final _globalkey = GlobalKey<FormState>();
1921
TextEditingController _name = TextEditingController();
@@ -56,6 +58,9 @@ class _CreatProfileState extends State<CreatProfile> {
5658
),
5759
InkWell(
5860
onTap: () async {
61+
setState(() {
62+
circular = true;
63+
});
5964
if (_globalkey.currentState.validate()) {
6065
Map<String, String> data = {
6166
"name": _name.text,
@@ -66,7 +71,28 @@ class _CreatProfileState extends State<CreatProfile> {
6671
};
6772
var response =
6873
await networkHandler.post("/profile/add", data);
69-
print(response.statusCode);
74+
if (response.statusCode == 200 ||
75+
response.statusCode == 201) {
76+
if (_imageFile.path != null) {
77+
var imageResponse = await networkHandler.patchImage(
78+
"/profile/add/image", _imageFile.path);
79+
if (imageResponse.statusCode == 200) {
80+
setState(() {
81+
circular = false;
82+
});
83+
Navigator.of(context).pushAndRemoveUntil(
84+
MaterialPageRoute(builder: (context) => HomePage()),
85+
(route) => false);
86+
}
87+
} else {
88+
setState(() {
89+
circular = false;
90+
});
91+
Navigator.of(context).pushAndRemoveUntil(
92+
MaterialPageRoute(builder: (context) => HomePage()),
93+
(route) => false);
94+
}
95+
}
7096
}
7197
},
7298
child: Center(
@@ -78,14 +104,16 @@ class _CreatProfileState extends State<CreatProfile> {
78104
borderRadius: BorderRadius.circular(10),
79105
),
80106
child: Center(
81-
child: Text(
82-
"Submit",
83-
style: TextStyle(
84-
color: Colors.white,
85-
fontSize: 18,
86-
fontWeight: FontWeight.bold,
87-
),
88-
),
107+
child: circular
108+
? CircularProgressIndicator()
109+
: Text(
110+
"Submit",
111+
style: TextStyle(
112+
color: Colors.white,
113+
fontSize: 18,
114+
fontWeight: FontWeight.bold,
115+
),
116+
),
89117
),
90118
),
91119
),

0 commit comments

Comments
 (0)