Skip to content

Commit

Permalink
workaround to fix update news list
Browse files Browse the repository at this point in the history
  • Loading branch information
BrightDV committed Oct 12, 2022
1 parent b6831b5 commit 1d556c8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 45 deletions.
97 changes: 53 additions & 44 deletions lib/api/news.dart
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,13 @@ class NewsItem extends StatelessWidget {

class NewsList extends StatefulWidget {
final List items;
final ValueListenable itemsValueNotifier;

NewsList({Key key, this.items});
NewsList({
Key key,
this.items,
this.itemsValueNotifier,
});
@override
_NewsListState createState() => _NewsListState();
}
Expand All @@ -369,52 +374,56 @@ class _NewsListState extends State<NewsList> {
@override
Widget build(BuildContext context) {
List originalItems = widget.items;
return ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: BouncingScrollPhysics(),
itemCount:
(present <= originalItems.length) ? items.length + 1 : items.length,
itemBuilder: (context, index) {
return index == items.length
? Padding(
padding: EdgeInsets.only(
top: 5,
bottom: 10,
),
child: ElevatedButton(
child: Padding(
padding:
EdgeInsets.only(left: 7, right: 7, top: 5, bottom: 5),
child: Text(
AppLocalizations.of(context).loadMore,
style: TextStyle(
fontSize: 18,
return ValueListenableBuilder(
valueListenable: widget.itemsValueNotifier,
builder: (context, value, child) => ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: BouncingScrollPhysics(),
itemCount:
(present <= originalItems.length) ? items.length + 1 : items.length,
itemBuilder: (context, index) {
return index == items.length
? Padding(
padding: EdgeInsets.only(
top: 5,
bottom: 10,
),
child: ElevatedButton(
child: Padding(
padding:
EdgeInsets.only(left: 7, right: 7, top: 5, bottom: 5),
child: Text(
AppLocalizations.of(context).loadMore,
style: TextStyle(
fontSize: 18,
),
),
),
onPressed: (() {
setState(
() {
if ((present + perPage) > originalItems.length) {
items.addAll(originalItems.getRange(
present, originalItems.length));
} else {
items.addAll(
originalItems.getRange(
present, present + perPage),
);
}
present = present + perPage;
},
);
}),
),
onPressed: (() {
setState(
() {
if ((present + perPage) > originalItems.length) {
items.addAll(originalItems.getRange(
present, originalItems.length));
} else {
items.addAll(
originalItems.getRange(present, present + perPage),
);
}
present = present + perPage;
},
);
}),
),
)
: NewsItem(
items[index],
false,
);
},
)
: NewsItem(
items[index],
false,
);
},
),
);
}
}
Expand Down
7 changes: 6 additions & 1 deletion lib/helpers/news_feed_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class _NewsFeedWidgetState extends State<NewsFeedWidget> {

final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
new GlobalKey<RefreshIndicatorState>();
ValueNotifier<List> itemsValueNotifier = ValueNotifier([]);

Future<List> refreshedNews;

@override
Expand All @@ -63,6 +65,7 @@ class _NewsFeedWidgetState extends State<NewsFeedWidget> {
onRefresh: () async {
_refreshIndicatorKey.currentState?.show(atTop: false);
List freshItems = await getLatestNewsItems(tagId: widget.tagId);
itemsValueNotifier = ValueNotifier(freshItems);
setState(() {
refreshedNews = Future.value(freshItems);
});
Expand All @@ -71,15 +74,18 @@ class _NewsFeedWidgetState extends State<NewsFeedWidget> {
? snapshot.error.toString() == 'XMLHttpRequest error.'
? NewsList(
items: F1NewsFetcher().formatResponse(latestNews),
itemsValueNotifier: itemsValueNotifier,
)
: RequestErrorWidget(snapshot.error.toString())
: snapshot.hasData
? NewsList(
items: snapshot.data,
itemsValueNotifier: itemsValueNotifier,
)
: latestNews['items'] != null
? NewsList(
items: F1NewsFetcher().formatResponse(latestNews),
itemsValueNotifier: itemsValueNotifier,
)
: LoadingIndicatorUtil(),
),
Expand Down Expand Up @@ -118,6 +124,5 @@ class _NewsFeedWidgetState extends State<NewsFeedWidget> {
);
ScaffoldMessenger.of(context).showSnackBar(offlineSnackBar);
}
return;
}
}

0 comments on commit 1d556c8

Please sign in to comment.