diff --git a/lib/news_feed.dart b/lib/news_feed.dart index 41ab6eb..0d867c3 100644 --- a/lib/news_feed.dart +++ b/lib/news_feed.dart @@ -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 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> 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: [ 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
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: [ 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, ), ], diff --git a/pubspec.lock b/pubspec.lock index fcf6763..dd132b2 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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: diff --git a/pubspec.yaml b/pubspec.yaml index 958dc91..5d4a329 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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