115 lines
3.0 KiB
Dart
115 lines
3.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_first_station/net_article/api/article_api.dart';
|
|
import '../model/article.dart';
|
|
import 'article_detail_page.dart';
|
|
|
|
class ArticleContent extends StatefulWidget {
|
|
const ArticleContent({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<ArticleContent> createState() => _ArticleContentState();
|
|
}
|
|
|
|
class _ArticleContentState extends State<ArticleContent> {
|
|
List<Article> _articles = [];
|
|
ArticleApi api = ArticleApi();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadData();
|
|
}
|
|
|
|
void _loadData() async {
|
|
_articles = await api.loadArticles(0);
|
|
setState(() {});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ListView.builder(
|
|
itemExtent: 80,
|
|
itemCount: _articles.length,
|
|
itemBuilder: _buildItemByIndex,
|
|
);
|
|
}
|
|
|
|
Widget _buildItemByIndex(BuildContext context, int index) {
|
|
return ArticleItem(
|
|
article: _articles[index],
|
|
onTap: _jumpToPage,
|
|
);
|
|
}
|
|
|
|
void _jumpToPage(Article article) {
|
|
Navigator.of(context).push(
|
|
MaterialPageRoute(
|
|
builder: (_) => ArticleDetailPage(article: article),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class ArticleItem extends StatelessWidget {
|
|
final Article article;
|
|
final ValueChanged<Article> onTap;
|
|
|
|
const ArticleItem({Key? key, required this.article, required this.onTap})
|
|
: super(key: key);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GestureDetector(
|
|
onTap: () => onTap(article),
|
|
child: Card(
|
|
elevation: 0,
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(6)),
|
|
color: Colors.white,
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: Text(
|
|
article.title,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style:
|
|
TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
|
|
),
|
|
),
|
|
const SizedBox(
|
|
width: 10,
|
|
),
|
|
// Spacer(),
|
|
Text(
|
|
article.time,
|
|
style: TextStyle(fontSize: 12, color: Colors.grey),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(
|
|
height: 4,
|
|
),
|
|
Expanded(
|
|
child: Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Text(
|
|
article.url,
|
|
style: TextStyle(fontSize: 12, color: Colors.grey),
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|