git-touch-android-ios-app/lib/screens/news.dart

46 lines
1.4 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import '../scaffolds/list.dart';
import '../widgets/event_item.dart';
2019-02-07 07:35:19 +01:00
import '../providers/settings.dart';
import '../utils/utils.dart';
class NewsScreen extends StatefulWidget {
@override
NewsScreenState createState() => NewsScreenState();
}
class NewsScreenState extends State<NewsScreen> {
2019-02-10 05:54:48 +01:00
Future<List<EventPayload>> fetchEvents(int page) async {
2019-02-07 09:46:21 +01:00
var settings = SettingsProvider.of(context);
var login = settings.activeLogin;
2019-02-09 07:20:21 +01:00
List data = await settings.getWithCredentials(
'/users/$login/received_events?page=$page&per_page=$pageSize');
// print(data);
2019-02-10 05:54:48 +01:00
return data
.map<EventPayload>((item) => EventPayload.fromJson(item))
.toList();
2019-02-07 07:35:19 +01:00
}
@override
Widget build(context) {
// FIXME: can't add generic type here. Don't know why
// type '(Event) => EventItem' is not a subtype of type '(dynamic) => Widget'
2019-02-03 07:42:50 +01:00
return ListScaffold(
2019-02-05 13:57:05 +01:00
title: Text('News'),
itemBuilder: (payload) => EventItem(payload),
2019-02-03 07:42:50 +01:00
onRefresh: () async {
var page = 1;
2019-02-07 09:46:21 +01:00
var items = await fetchEvents(page);
return ListPayload(
2019-02-09 07:20:21 +01:00
cursor: page + 1, hasMore: items.length == pageSize, items: items);
2019-02-03 07:42:50 +01:00
},
onLoadMore: (page) async {
2019-02-07 09:46:21 +01:00
var items = await fetchEvents(page);
return ListPayload(
2019-02-09 07:20:21 +01:00
cursor: page + 1, hasMore: items.length == pageSize, items: items);
2019-02-03 07:42:50 +01:00
},
);
}
}