Skip to content

Commit 41d3aca

Browse files
committed
modified blogs
1 parent 67544a1 commit 41d3aca

File tree

6 files changed

+299
-80
lines changed

6 files changed

+299
-80
lines changed

lib/exports/exports.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export 'package:flutter_svg/flutter_svg.dart';
3535
export 'package:accordion/accordion.dart';
3636
export 'package:accordion/controllers.dart';
3737
export 'package:http/http.dart';
38+
export 'package:image_picker/image_picker.dart';
3839
// models
3940
export "/models/blogs_model.dart";
4041
// controllers
Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
// ignore_for_file: invalid_use_of_visible_for_testing_member
2+
13
import "/exports/exports.dart";
4+
import "dart:io";
5+
26
class AddBlog extends StatefulWidget {
37
final String league;
48
const AddBlog({super.key, required this.league});
@@ -8,8 +12,107 @@ class AddBlog extends StatefulWidget {
812
}
913

1014
class _AddBlogState extends State<AddBlog> {
15+
final titleController = TextEditingController();
16+
final contentController = TextEditingController();
17+
String? image;
1118
@override
1219
Widget build(BuildContext context) {
13-
return Container();
20+
return Scaffold(
21+
appBar: AppBar(
22+
title: const Text("Add Blog"),
23+
),
24+
body: Consumer<LoaderController>(builder: (context, controller, ch) {
25+
return ListView(
26+
padding: const EdgeInsets.fromLTRB(15, 10, 15, 10),
27+
children: [
28+
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
29+
CircleAvatar(
30+
backgroundImage: image == null
31+
? const AssetImage("assets/images/logo.jpeg")
32+
as ImageProvider<Object>
33+
: FileImage(
34+
File(image!),
35+
),
36+
radius: 70,
37+
),
38+
IconButton(
39+
iconSize: 40,
40+
icon: const Icon(
41+
Icons.add_a_photo_rounded,
42+
),
43+
onPressed: () async {
44+
final pickedFile =
45+
await ImagePicker.platform.getImageFromSource(
46+
source: ImageSource.gallery,
47+
);
48+
if (pickedFile != null) {
49+
setState(() {
50+
image = pickedFile.path;
51+
});
52+
}
53+
},
54+
),
55+
]),
56+
CommonTextField(
57+
titleText: "Title",
58+
enableBorder: true,
59+
hintText: "Enter title",
60+
readOnly: controller.isLoading,
61+
controller: titleController,
62+
),
63+
const SizedBox.square(dimension: 20),
64+
TextFormField(
65+
maxLines: 15,
66+
controller: contentController,
67+
readOnly: controller.isLoading,
68+
decoration: InputDecoration(
69+
// helperText: "Description",
70+
hintText: "Enter description",
71+
border: OutlineInputBorder(
72+
borderSide: BorderSide(
73+
color: Colors.grey.shade200,
74+
),
75+
),
76+
),
77+
),
78+
const SizedBox.square(dimension: 20),
79+
CustomButton(
80+
text: "Add Blog Post",
81+
loading: controller.isLoading,
82+
buttonColor: controller.isLoading
83+
? null
84+
: Theme.of(context).primaryColor,
85+
textColor: controller.isLoading ? null : Colors.white,
86+
onPress: () {
87+
if (titleController.text.isEmpty ||
88+
contentController.text.isEmpty ||
89+
image == null) {
90+
showAdaptiveDialog(
91+
context: context,
92+
builder: (context) => AlertDialog.adaptive(
93+
title: const Text("Error"),
94+
content: const Text("All fields are required"),
95+
actions: [
96+
TextButton(
97+
onPressed: () => Routes.popPage(),
98+
child: const Text("OK"),
99+
),
100+
],
101+
),
102+
);
103+
return;
104+
} else {
105+
BlogService.createBlog({
106+
"title": titleController.text,
107+
"content": contentController.text,
108+
"league": widget.league,
109+
"image": image,
110+
});
111+
}
112+
})
113+
],
114+
);
115+
}),
116+
);
14117
}
15-
}
118+
}
Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,127 @@
1+
// ignore_for_file: invalid_use_of_visible_for_testing_member
2+
13
import "/exports/exports.dart";
4+
import "dart:io";
5+
26
class EditBlog extends StatefulWidget {
37
final BlogsModel blog;
4-
const EditBlog({super.key,required this.blog});
8+
const EditBlog({super.key, required this.blog});
59

610
@override
711
State<EditBlog> createState() => _EditBlogState();
812
}
913

1014
class _EditBlogState extends State<EditBlog> {
15+
final titleController = TextEditingController();
16+
final contentController = TextEditingController();
17+
String? image;
18+
@override
19+
void initState() {
20+
super.initState();
21+
setState(() {
22+
titleController.text = widget.blog.title;
23+
contentController.text = widget.blog.content;
24+
});
25+
}
26+
1127
@override
1228
Widget build(BuildContext context) {
13-
return Container();
29+
return Scaffold(
30+
appBar: AppBar(
31+
title: const Text("Edit Blog"),
32+
),
33+
body: Consumer<LoaderController>(builder: (context, controller, ch) {
34+
return ListView(
35+
padding: const EdgeInsets.fromLTRB(15, 10, 15, 10),
36+
children: [
37+
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
38+
CircleAvatar(
39+
backgroundImage: image == null
40+
? NetworkImage(widget.blog.image) as ImageProvider<Object>
41+
: FileImage(
42+
File(image!),
43+
),
44+
radius: 70,
45+
),
46+
IconButton(
47+
iconSize: 40,
48+
icon: const Icon(
49+
Icons.add_a_photo_rounded,
50+
),
51+
onPressed: () async {
52+
final pickedFile =
53+
await ImagePicker.platform.getImageFromSource(
54+
source: ImageSource.gallery,
55+
);
56+
if (pickedFile != null) {
57+
setState(() {
58+
image = pickedFile.path;
59+
});
60+
}
61+
},
62+
),
63+
]),
64+
CommonTextField(
65+
titleText: "Title",
66+
enableBorder: true,
67+
hintText: "Enter title",
68+
readOnly: controller.isLoading,
69+
controller: titleController,
70+
),
71+
const SizedBox.square(dimension: 20),
72+
TextFormField(
73+
maxLines: 15,
74+
controller: contentController,
75+
readOnly: controller.isLoading,
76+
decoration: InputDecoration(
77+
// helperText: "Description",
78+
hintText: "Enter description",
79+
border: OutlineInputBorder(
80+
borderSide: BorderSide(
81+
color: Colors.grey.shade200,
82+
),
83+
),
84+
),
85+
),
86+
const SizedBox.square(dimension: 20),
87+
CustomButton(
88+
text: "Update Blog Post",
89+
loading: controller.isLoading,
90+
buttonColor: controller.isLoading
91+
? null
92+
: Theme.of(context).primaryColor,
93+
textColor: controller.isLoading ? null : Colors.white,
94+
onPress: () {
95+
if (titleController.text.isEmpty ||
96+
contentController.text.isEmpty ||
97+
image == null) {
98+
showAdaptiveDialog(
99+
context: context,
100+
builder: (context) => AlertDialog.adaptive(
101+
title: const Text("Error"),
102+
content: const Text("All fields are required"),
103+
actions: [
104+
TextButton(
105+
onPressed: () => Routes.popPage(),
106+
child: const Text("OK"),
107+
),
108+
],
109+
),
110+
);
111+
return;
112+
} else {
113+
BlogService.updateBlog({
114+
"id": widget.blog.id,
115+
"title": titleController.text,
116+
"content": contentController.text,
117+
"league": widget.blog.league,
118+
"image": image,
119+
});
120+
}
121+
})
122+
],
123+
);
124+
}),
125+
);
14126
}
15-
}
127+
}

0 commit comments

Comments
 (0)