11import 'package:flutter/material.dart' ;
2+ import 'package:image_picker/image_picker.dart' ;
23
34class AddBlog extends StatefulWidget {
45 AddBlog ({Key key}) : super (key: key);
@@ -8,13 +9,26 @@ class AddBlog extends StatefulWidget {
89}
910
1011class _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}
0 commit comments