import './home.dart';
import 'package:flutter/material.dart';
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class BlogPost {
int id;
String title;
String details;
String featuredImageUrl;
String category;
String createdAt;
}
import './blog_post.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:flutter_html/flutter_html.dart';
class BlogPostDetailsScreen extends StatefulWidget {
final BlogPost blogPost;
BlogPostDetailsScreen({this.blogPost});
@override
_BlogPostDetailsScreenState createState() => _BlogPostDetailsScreenState();
}
class _BlogPostDetailsScreenState extends State<BlogPostDetailsScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(this.widget.blogPost.title),
),
body: ListView(
children: <Widget>[
Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(6.0),
child: Image.network(this.widget.blogPost.featuredImageUrl),
),
Padding(
padding: const EdgeInsets.all(6.0),
child: Text(this.widget.blogPost.title,
textAlign: TextAlign.left,
style:
TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(6.0),
child: Text(
this.widget.blogPost.category,
style: TextStyle(
backgroundColor: Colors.black12, fontSize: 16.0),
),
),
Padding(
padding: const EdgeInsets.all(6.0),
child: Text(
DateFormat("dd-MMM-yyyy").format(
DateTime.parse(this.widget.blogPost.createdAt)),
style: TextStyle(
backgroundColor: Colors.black12, fontSize: 16.0),
),
),
],
),
Padding(
padding: const EdgeInsets.all(6.0),
child: Html(
data: this.widget.blogPost.details,
),
)
],
),
)
],
),
);
}
}
import './repository.dart';
class BlogPostService {
Repository _repository;
BlogPostService() {
_repository = Repository();
}
getAllBlogPosts() async {
return await _repository.httpGet('posts');
}
}
import 'dart:convert';
import './blog_post.dart';
import './blog_post_details_screen.dart';
import './blog_post_service.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
BlogPostService _blogPostService = BlogPostService();
List<BlogPost> _list = List<BlogPost>();
Future<List<BlogPost>> _getAllBlogPosts() async {
return _blogPostService.getAllBlogPosts().then((result) {
try {
if (result != null) {
var blogPosts = json.decode(result.body);
blogPosts.forEach((blogPost) {
var model = BlogPost();
model.title = blogPost['title'];
model.details = blogPost['details'];
model.featuredImageUrl = blogPost['featured_image_url'];
model.category = blogPost['category']['name'];
model.createdAt = blogPost['created_at'];
setState(() {
_list.add(model);
});
});
}else{
return _list;
}
}
catch (e){
print(e.error);
}
return _list;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Blog App'),
),
body: FutureBuilder<List<BlogPost>>(
future: _getAllBlogPosts(),
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(6.0),
child: Image.network(
snapshot.data[index].featuredImageUrl),
),
Padding(
padding: const EdgeInsets.all(6.0),
child: InkWell(
onTap: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => BlogPostDetailsScreen(blogPost: snapshot.data[index],)));
},
child: Text(
snapshot.data[index].title,
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold),
),
),
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(6.0),
child: Text(
snapshot.data[index].category,
style: TextStyle(
backgroundColor: Colors.black12,
fontSize: 16.0),
),
),
Padding(
padding: const EdgeInsets.all(6.0),
child: Text(
DateFormat("dd-MMM-yyyy").format(
DateTime.parse(
snapshot.data[index].createdAt)),
style: TextStyle(
backgroundColor: Colors.black12,
fontSize: 16.0),
),
)
],
),
],
),
),
);
});
}
else {
return Container(
child: Text('Loading ...'),
);
}
},
),
);
}
}
import './app.dart';
import 'package:flutter/material.dart';
void main() => runApp(App());
import 'package:http/http.dart' as http;
class Repository {
String _baseUrl = "http://10.0.2.2:8080/api";
httpGet(String api) async {
return await http.get(_baseUrl + "/" + api);
}
}
No comments:
Post a Comment