From dfb32cc0fd686a4399357a7914ed7fe131466d1c Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sat, 21 Apr 2018 23:10:04 +0800 Subject: [PATCH] Add more components of home page --- lib/ios/home.dart | 84 ++++++++++++++++++++++++++++------------- lib/ios/home/event.dart | 26 +++++++++++++ lib/utils.dart | 4 +- 3 files changed, 85 insertions(+), 29 deletions(-) diff --git a/lib/ios/home.dart b/lib/ios/home.dart index 9e1cc4d..9b3faaa 100644 --- a/lib/ios/home.dart +++ b/lib/ios/home.dart @@ -1,34 +1,72 @@ -// import 'dart:async'; +import 'dart:async'; // import 'dart:convert'; import '../utils.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'home/event.dart'; -class IosHomeTab extends StatelessWidget { +class IosHomeTab extends StatefulWidget { + @override + createState() { + return new IosHomeTabState(); + } +} + +class IosHomeTabState extends State { + int page = 1; + List events = []; + + loadFirst() async { + events = await fetchEvents(); + page = 1; + return events; + } + + loadMore() async { + events.addAll(await fetchEvents(page + 1)); + page++; + return events; + } + @override Widget build(context) { return new FutureBuilder( - future: fetchEvents(), + future: loadFirst(), builder: (context, snapshot) { Widget widget; if (snapshot.hasData) { - List events = snapshot.data; - widget = new ListView( - padding: new EdgeInsets.all(8.0), - // itemExtent: 20.0, - children: events.map((event) { - switch (event.type) { - case 'IssuesEvent': - return new IssuesEvent(event); - case 'PushEvent': - return new PushEvent(event); - case 'PullRequestEvent': - return new PullRequestEvent(event); - default: - return new Text(''); - } - }).toList(), + // List events = snapshot.data; + widget = new CustomScrollView( + slivers: [ + new CupertinoRefreshControl( + onRefresh: () { + return loadFirst(); + }, + ), + new SliverSafeArea( + // top: true, + sliver: new SliverList( + delegate: new SliverChildBuilderDelegate( + (context, index) { + var event = events[index]; + switch (event.type) { + case 'IssuesEvent': + return new IssuesEvent(event); + case 'PushEvent': + return new PushEvent(event); + case 'PullRequestEvent': + return new PullRequestEvent(event); + case 'WatchEvent': + return new WatchEvent(event); + default: + return new Text('Not implement yet'); + } + }, + childCount: 30, + ), + ), + ), + ], ); } else if (snapshot.hasError) { widget = new Text("${snapshot.error}"); @@ -37,14 +75,6 @@ class IosHomeTab extends StatelessWidget { } return widget; - // return new CustomScrollView(slivers: [ - // new CupertinoRefreshControl( - // onRefresh: () { - // return fetchEvents(); - // }, - // ), - // // new CupertinoActivityIndicator(), - // ]); }, ); } diff --git a/lib/ios/home/event.dart b/lib/ios/home/event.dart index c3a932c..f0b564b 100644 --- a/lib/ios/home/event.dart +++ b/lib/ios/home/event.dart @@ -142,3 +142,29 @@ class IssueCommentEvent extends StatelessWidget { ); } } + +class WatchEvent extends StatelessWidget { + final Event event; + WatchEvent(this.event); + + @override + build(ctx) { + return new Row( + children: [ + new _Avatar(event.avatar), + new Expanded( + child: new RichText( + text: new TextSpan( + style: new TextStyle(color: CupertinoColors.black), + children: [ + _strong(event.actor), + new TextSpan(text: ' ${event.payload['action']} '), + _strong(event.repo), + ], + ), + ), + ), + ], + ); + } +} diff --git a/lib/utils.dart b/lib/utils.dart index 7aa68e2..62001a3 100644 --- a/lib/utils.dart +++ b/lib/utils.dart @@ -47,9 +47,9 @@ class Event { // } } -Future> fetchEvents() async { +Future> fetchEvents([int page = 1]) async { final res = await http.get( - prefix + '/users/pd4d10/received_events/public', + prefix + '/users/pd4d10/received_events/public?page=$page', headers: {HttpHeaders.AUTHORIZATION: 'token $token'}, ); List resJson = json.decode(res.body);