From bc2d0ef6c262e83b65408a92921291dd14d4f427 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 21 Feb 2020 17:25:21 -0500 Subject: [PATCH] break news features apart into separate files --- lib/home_widget.dart | 12 ++-- lib/news/article.dart | 74 +++++++++++++++++++++++ lib/{news_feed.dart => news/feed.dart} | 83 ++------------------------ lib/placeholder_widget.dart | 9 --- 4 files changed, 87 insertions(+), 91 deletions(-) create mode 100644 lib/news/article.dart rename lib/{news_feed.dart => news/feed.dart} (51%) delete mode 100644 lib/placeholder_widget.dart diff --git a/lib/home_widget.dart b/lib/home_widget.dart index 6dc6eaf..546016c 100644 --- a/lib/home_widget.dart +++ b/lib/home_widget.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import 'news_feed.dart'; +import 'news/feed.dart'; class Home extends StatefulWidget { @override @@ -9,16 +9,18 @@ class Home extends StatefulWidget { class HomeState extends State { @override Widget build(BuildContext context) { + var feed = Feed(); + return Scaffold( appBar: AppBar( title: Text('UNCC Athletics App'), ), body: ListView( children: [ - HorizontalNewsFeed(title: Text("Basketball")), - HorizontalNewsFeed(title: Text("Soccer")), - HorizontalNewsFeed(title: Text("Football")), - HorizontalNewsFeed(title: Text("Volleyball")), + HorizontalNewsFeed(newsFeed: feed, title: Text("Basketball")), + HorizontalNewsFeed(newsFeed: feed, title: Text("Soccer")), + HorizontalNewsFeed(newsFeed: feed, title: Text("Football")), + HorizontalNewsFeed(newsFeed: feed, title: Text("Volleyball")), ], ), bottomNavigationBar: BottomNavigationBar( diff --git a/lib/news/article.dart b/lib/news/article.dart new file mode 100644 index 0000000..1e920b1 --- /dev/null +++ b/lib/news/article.dart @@ -0,0 +1,74 @@ +import 'package:flutter/material.dart'; + +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 ArticleCard extends StatelessWidget { + const ArticleCard({ + Key key, + @required this.article, + @required this.height, + @required this.width, + }) : super(key: key); + + final Article article; + final double height; + final double width; + + @override + Widget build(BuildContext context) { + return SizedBox( + height: height, + width: width, + child: Card( + clipBehavior: Clip.antiAlias, + child: Wrap( + verticalDirection: VerticalDirection.up, + children: [ + ListTile( + title: Text(article.title), + trailing: Icon(Icons.more_horiz), + ), + Image.network( + article.thumbUrl, + fit: BoxFit.fitWidth, + ), + ], + ), + ), + ); + } +} diff --git a/lib/news_feed.dart b/lib/news/feed.dart similarity index 51% rename from lib/news_feed.dart rename to lib/news/feed.dart index 0d867c3..5c34582 100644 --- a/lib/news_feed.dart +++ b/lib/news/feed.dart @@ -1,43 +1,9 @@ -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; +import 'dart:convert'; - 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'], - ); - } -} +import 'article.dart'; class Feed { Future> getPage(int page, {int size = 10}) async { @@ -52,9 +18,11 @@ class Feed { class HorizontalNewsFeed extends StatelessWidget { const HorizontalNewsFeed({ Key key, + @required this.newsFeed, @required this.title, }) : super(key: key); + final Feed newsFeed; final Widget title; @override @@ -62,8 +30,6 @@ class HorizontalNewsFeed extends StatelessWidget { var width = MediaQuery.of(context).size.width / 1.25; var height = width * .6; - var feed = Feed(); - return Column( children: [ Divider(), @@ -77,7 +43,7 @@ class HorizontalNewsFeed extends StatelessWidget { Container( height: height, child: FutureBuilder( - future: feed.getPage(1), + future: newsFeed.getPage(1), builder: (ctx, snapshot) { if (!snapshot.hasData) { return Center(child: CircularProgressIndicator()); @@ -87,7 +53,7 @@ class HorizontalNewsFeed extends StatelessWidget { scrollDirection: Axis.horizontal, itemCount: articles.length, itemBuilder: (ctx, idx) { - return NewsCard( + return ArticleCard( article: articles[idx], height: height, width: width, @@ -102,40 +68,3 @@ 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; - - @override - Widget build(BuildContext context) { - return SizedBox( - height: height, - width: width, - child: Card( - clipBehavior: Clip.antiAlias, - child: Wrap( - verticalDirection: VerticalDirection.up, - children: [ - ListTile( - title: Text(article.title), - trailing: Icon(Icons.more_horiz), - ), - Image.network( - article.thumbUrl, - fit: BoxFit.fitWidth, - ), - ], - ), - ), - ); - } -} diff --git a/lib/placeholder_widget.dart b/lib/placeholder_widget.dart deleted file mode 100644 index 10b8eb6..0000000 --- a/lib/placeholder_widget.dart +++ /dev/null @@ -1,9 +0,0 @@ -import 'package:flutter/material.dart'; - -class PlaceholderWidget extends StatelessWidget{ - final Color color; - PlaceholderWidget(this.color); - - @override - Widget build(BuildContext context) => Container(color: color,); -} \ No newline at end of file