break news features apart into separate files

This commit is contained in:
2020-02-21 17:25:21 -05:00
parent 58fa2b6838
commit bc2d0ef6c2
4 changed files with 87 additions and 91 deletions

View File

@@ -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<Home> {
@override
Widget build(BuildContext context) {
var feed = Feed();
return Scaffold(
appBar: AppBar(
title: Text('UNCC Athletics App'),
),
body: ListView(
children: <Widget>[
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(

74
lib/news/article.dart Normal file
View File

@@ -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<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 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: <Widget>[
ListTile(
title: Text(article.title),
trailing: Icon(Icons.more_horiz),
),
Image.network(
article.thumbUrl,
fit: BoxFit.fitWidth,
),
],
),
),
);
}
}

View File

@@ -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<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'],
);
}
}
import 'article.dart';
class Feed {
Future<List<Article>> 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: <Widget>[
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: <Widget>[
ListTile(
title: Text(article.title),
trailing: Icon(Icons.more_horiz),
),
Image.network(
article.thumbUrl,
fit: BoxFit.fitWidth,
),
],
),
),
);
}
}

View File

@@ -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,);
}