Skip to content

Commit 3f14c62

Browse files
committed
validation added
1 parent 745df1c commit 3f14c62

File tree

3 files changed

+74
-26
lines changed

3 files changed

+74
-26
lines changed

lib/Blog/addBlog.dart

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:image_picker/image_picker.dart';
23

34
class AddBlog extends StatefulWidget {
45
AddBlog({Key key}) : super(key: key);
@@ -8,13 +9,26 @@ class AddBlog extends StatefulWidget {
89
}
910

1011
class _AddBlogState extends State<AddBlog> {
12+
final _globalkey = GlobalKey<FormState>();
13+
TextEditingController _title = TextEditingController();
14+
TextEditingController _body = TextEditingController();
15+
ImagePicker _picker = ImagePicker();
16+
PickedFile _imageFile;
17+
IconData iconphoto = Icons.image;
1118
@override
1219
Widget build(BuildContext context) {
1320
return Scaffold(
1421
appBar: AppBar(
1522
backgroundColor: Colors.white54,
1623
elevation: 0,
17-
leading: IconButton(icon: Icon(Icons.clear), onPressed: null),
24+
leading: IconButton(
25+
icon: Icon(
26+
Icons.clear,
27+
color: Colors.black,
28+
),
29+
onPressed: () {
30+
Navigator.pop(context);
31+
}),
1832
actions: <Widget>[
1933
FlatButton(
2034
onPressed: null,
@@ -24,15 +38,18 @@ class _AddBlogState extends State<AddBlog> {
2438
))
2539
],
2640
),
27-
body: ListView(
28-
children: <Widget>[
29-
titleTextField(),
30-
bodyTextField(),
31-
SizedBox(
32-
height: 20,
33-
),
34-
addButton(),
35-
],
41+
body: Form(
42+
key: _globalkey,
43+
child: ListView(
44+
children: <Widget>[
45+
titleTextField(),
46+
bodyTextField(),
47+
SizedBox(
48+
height: 20,
49+
),
50+
addButton(),
51+
],
52+
),
3653
),
3754
);
3855
}
@@ -44,6 +61,15 @@ class _AddBlogState extends State<AddBlog> {
4461
vertical: 10,
4562
),
4663
child: TextFormField(
64+
controller: _title,
65+
validator: (value) {
66+
if (value.isEmpty) {
67+
return "Title can't be empty";
68+
} else if (value.length > 100) {
69+
return "Title length should be <=100";
70+
}
71+
return null;
72+
},
4773
decoration: InputDecoration(
4874
border: OutlineInputBorder(
4975
borderSide: BorderSide(
@@ -59,10 +85,10 @@ class _AddBlogState extends State<AddBlog> {
5985
labelText: "Add Image and Title",
6086
prefixIcon: IconButton(
6187
icon: Icon(
62-
Icons.image,
88+
iconphoto,
6389
color: Colors.teal,
6490
),
65-
onPressed: null,
91+
onPressed: takeCoverPhoto,
6692
),
6793
),
6894
maxLength: 100,
@@ -77,6 +103,13 @@ class _AddBlogState extends State<AddBlog> {
77103
horizontal: 10,
78104
),
79105
child: TextFormField(
106+
controller: _body,
107+
validator: (value) {
108+
if (value.isEmpty) {
109+
return "Body can't be empty";
110+
}
111+
return null;
112+
},
80113
decoration: InputDecoration(
81114
border: OutlineInputBorder(
82115
borderSide: BorderSide(
@@ -97,19 +130,30 @@ class _AddBlogState extends State<AddBlog> {
97130
}
98131

99132
Widget addButton() {
100-
return Center(
101-
child: Container(
102-
height: 50,
103-
width: 200,
104-
decoration: BoxDecoration(
105-
borderRadius: BorderRadius.circular(10), color: Colors.teal),
106-
child: Center(
107-
child: Text(
108-
"Add Blog",
109-
style: TextStyle(
110-
color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold),
111-
)),
133+
return InkWell(
134+
onTap: () {},
135+
child: Center(
136+
child: Container(
137+
height: 50,
138+
width: 200,
139+
decoration: BoxDecoration(
140+
borderRadius: BorderRadius.circular(10), color: Colors.teal),
141+
child: Center(
142+
child: Text(
143+
"Add Blog",
144+
style: TextStyle(
145+
color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold),
146+
)),
147+
),
112148
),
113149
);
114150
}
151+
152+
void takeCoverPhoto() async {
153+
final coverPhoto = await _picker.getImage(source: ImageSource.gallery);
154+
setState(() {
155+
_imageFile = coverPhoto;
156+
iconphoto = Icons.check_box;
157+
});
158+
}
115159
}

lib/Pages/HomePage.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:blogapp/Blog/addBlog.dart';
12
import 'package:blogapp/Screen/HomeScreen.dart';
23
import 'package:blogapp/Profile/ProfileScreen.dart';
34
import 'package:flutter/material.dart';
@@ -55,7 +56,10 @@ class _HomePageState extends State<HomePage> {
5556
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
5657
floatingActionButton: FloatingActionButton(
5758
backgroundColor: Colors.teal,
58-
onPressed: () {},
59+
onPressed: () {
60+
Navigator.of(context)
61+
.push(MaterialPageRoute(builder: (context) => AddBlog()));
62+
},
5963
child: Text(
6064
"+",
6165
style: TextStyle(fontSize: 40),

lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class _MyAppState extends State<MyApp> {
4747
Theme.of(context).textTheme,
4848
),
4949
),
50-
home: AddBlog(),
50+
home: page,
5151
);
5252
}
5353
}

0 commit comments

Comments
 (0)