better widgets

This commit is contained in:
2020-02-27 00:05:43 -05:00
parent bc2d0ef6c2
commit 548e590b12
2 changed files with 91 additions and 70 deletions

View File

@@ -11,18 +11,17 @@ class Article {
final String thumbUrl;
Article(
this.id, {
this.type,
this.sport,
this.title,
this.mediumHeadline,
this.url,
this.summary,
this.thumbUrl,
});
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'}) {
factory Article.fromJson(Map<String, dynamic> json) {
return Article(
json['id'],
type: json['type'],
@@ -40,33 +39,51 @@ class ArticleCard extends StatelessWidget {
const ArticleCard({
Key key,
@required this.article,
@required this.height,
@required this.width,
this.numCards = 1.25,
}) : super(key: key);
final Article article;
final double height;
final double width;
final double numCards;
double widthIn(BuildContext context) {
return MediaQuery.of(context).size.width / numCards;
}
@override
Widget build(BuildContext context) {
var decoration = BoxDecoration(
image: DecorationImage(
image: NetworkImage(
article.thumbUrl,
),
fit: BoxFit.cover,
),
);
var body = Column(
verticalDirection: VerticalDirection.up,
children: <Widget>[
Container(
color: Colors.white,
child: ListTile(
title: Text(
article.mediumHeadline,
overflow: TextOverflow.ellipsis,
maxLines: 2,
),
),
),
],
);
return SizedBox(
height: height,
width: width,
width: widthIn(context),
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,
),
],
semanticContainer: true,
clipBehavior: Clip.antiAliasWithSaveLayer,
child: Container(
decoration: decoration,
child: body,
),
),
);

View File

@@ -6,9 +6,10 @@ import 'dart:convert';
import 'article.dart';
class Feed {
static final cobraBase = "https://www.ncaa.com/ncaa/cobra";
Future<List<Article>> getPage(int page, {int size = 10}) async {
var url =
'https://www.ncaa.com/ncaa/cobra/school-content/charlotte,$page,$size';
var url = '$cobraBase/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();
@@ -16,55 +17,58 @@ class Feed {
}
class HorizontalNewsFeed extends StatelessWidget {
final Feed newsFeed;
final Widget title;
final double numCards;
const HorizontalNewsFeed({
Key key,
@required this.newsFeed,
@required this.title,
this.numCards = 3.25,
}) : super(key: key);
final Feed newsFeed;
final Widget title;
double heightIn(BuildContext context) {
return MediaQuery.of(context).size.height / numCards;
}
@override
Widget build(BuildContext context) {
var width = MediaQuery.of(context).size.width / 1.25;
var height = width * .6;
return Column(
children: <Widget>[
Divider(),
ListTile(
title: title,
trailing: IconButton(
icon: Icon(Icons.navigate_next),
onPressed: () {},
return SizedBox(
height: heightIn(context),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
ListTile(
title: title,
trailing: IconButton(
icon: Icon(Icons.navigate_next),
onPressed: () {},
),
),
),
Container(
height: height,
child: FutureBuilder(
future: newsFeed.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 ArticleCard(
article: articles[idx],
height: height,
width: width,
);
},
);
}
},
Expanded(
child: FutureBuilder(
future: newsFeed.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 ArticleCard(
article: articles[idx],
);
},
);
}
},
),
),
),
],
],
),
);
}
}