basic api call for news feed

This commit is contained in:
2020-02-21 17:14:02 -05:00
parent df1db9f523
commit 58fa2b6838
3 changed files with 90 additions and 7 deletions

View File

@@ -1,4 +1,53 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class Article {
final int id;
final String type;
final String sport;
final String title;
final String mediumHeadline;
final String url;
final String summary;
final String thumbUrl;
Article(
this.id, {
this.type,
this.sport,
this.title,
this.mediumHeadline,
this.url,
this.summary,
this.thumbUrl,
});
factory Article.fromJson(Map<String, dynamic> json,
{String root = 'https://www.ncaa.com'}) {
return Article(
json['id'],
type: json['type'],
sport: json['sport'],
title: json['title'],
mediumHeadline: json['medium_headline'],
url: json['url'],
summary: json['summary'],
thumbUrl: json['thumb']['url'],
);
}
}
class Feed {
Future<List<Article>> getPage(int page, {int size = 10}) async {
var url =
'https://www.ncaa.com/ncaa/cobra/school-content/charlotte,$page,$size';
http.Response response = await http.get(url);
Iterable articles = json.decode(response.body);
return articles.map((e) => Article.fromJson(e)).toList();
}
}
class HorizontalNewsFeed extends StatelessWidget {
const HorizontalNewsFeed({
@@ -13,6 +62,8 @@ class HorizontalNewsFeed extends StatelessWidget {
var width = MediaQuery.of(context).size.width / 1.25;
var height = width * .6;
var feed = Feed();
return Column(
children: <Widget>[
Divider(),
@@ -25,11 +76,25 @@ class HorizontalNewsFeed extends StatelessWidget {
),
Container(
height: height,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 6,
itemBuilder: (ictx, iidx) {
return NewsCard(height: height, width: width);
child: FutureBuilder(
future: feed.getPage(1),
builder: (ctx, snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
} else {
List<Article> articles = snapshot.data;
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: articles.length,
itemBuilder: (ctx, idx) {
return NewsCard(
article: articles[idx],
height: height,
width: width,
);
},
);
}
},
),
),
@@ -41,10 +106,12 @@ class HorizontalNewsFeed extends StatelessWidget {
class NewsCard extends StatelessWidget {
const NewsCard({
Key key,
@required this.article,
@required this.height,
@required this.width,
}) : super(key: key);
final Article article;
final double height;
final double width;
@@ -59,11 +126,11 @@ class NewsCard extends StatelessWidget {
verticalDirection: VerticalDirection.up,
children: <Widget>[
ListTile(
title: Text("no u"),
title: Text(article.title),
trailing: Icon(Icons.more_horiz),
),
Image.network(
"https://pbs.twimg.com/media/ERFFfZpU4AAJmIy.jpg:small",
article.thumbUrl,
fit: BoxFit.fitWidth,
),
],

View File

@@ -74,6 +74,20 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.0+4"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
image:
dependency: transitive
description:

View File

@@ -20,6 +20,8 @@ dependencies:
flutter:
sdk: flutter
http: ^0.12.0+4
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2