-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathnews_page.dart
More file actions
86 lines (81 loc) · 2.52 KB
/
news_page.dart
File metadata and controls
86 lines (81 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_testing_tutorial/article_page.dart';
import 'package:flutter_testing_tutorial/news_change_notifier.dart';
/// A page that displays a list of news articles.
class NewsPage extends StatefulWidget {
/// Creates a [NewsPage].
const NewsPage({Key? key}) : super(key: key);
@override
State<NewsPage> createState() => _NewsPageState();
}
class _NewsPageState extends State<NewsPage> {
@override
void initState() {
super.initState();
// Fetch articles when the widget is initialized.
Future.microtask(
() => context.read<NewsChangeNotifier>().getArticles(),
);
}
@override
Widget build(BuildContext context) {
// Build the UI for the NewsPage.
return Scaffold(
appBar: AppBar(
title: const Text('News'),
actions: [
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () {
// Refresh articles when the refresh button is pressed.
context.read<NewsChangeNotifier>().getArticles();
},
),
],
),
body: Consumer<NewsChangeNotifier>(
builder: (context, notifier, child) {
if (notifier.isLoading) {
return const Center(
child: CircularProgressIndicator(
key: Key('progress-indicator'),
),
);
}
return ListView.builder(
itemCount: notifier.articles.length,
itemBuilder: (_, index) {
final article = notifier.articles[index];
return Card(
elevation: 2,
child: InkWell(
onTap: () {
// Navigate to the ArticlePage when an article is tapped.
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => ArticlePage(article: article),
),
);
},
child: ListTile(
title: Text(article.title),
subtitle: Text(
article.content,
maxLines: 4,
overflow: TextOverflow.ellipsis,
),
),
),
);
},
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 16,
),
);
},
),
);
}
}