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

73 lines
2.5 KiB
Dart
Raw Normal View History

import 'dart:convert';
2019-03-02 11:17:46 +01:00
import 'package:flutter/material.dart';
2019-12-12 07:02:48 +01:00
import 'package:git_touch/models/github.dart';
2020-01-12 09:18:37 +01:00
import 'package:git_touch/models/theme.dart';
2019-09-25 11:06:36 +02:00
import 'package:git_touch/scaffolds/tab_stateful.dart';
2019-09-08 15:20:12 +02:00
import 'package:git_touch/utils/utils.dart';
2019-09-11 13:59:47 +02:00
import 'package:git_touch/widgets/app_bar_title.dart';
2019-09-24 14:45:55 +02:00
import 'package:git_touch/widgets/user_item.dart';
import 'package:http/http.dart' as http;
2019-09-23 12:28:33 +02:00
import 'package:git_touch/widgets/repository_item.dart';
2020-01-12 09:18:37 +01:00
import 'package:provider/provider.dart';
2019-03-02 11:17:46 +01:00
2019-09-24 14:45:55 +02:00
class TrendingScreen extends StatelessWidget {
2019-03-02 11:17:46 +01:00
Widget build(BuildContext context) {
2019-12-30 12:55:59 +01:00
return TabStatefulScaffold<Iterable>(
2019-09-11 13:59:47 +02:00
title: AppBarTitle('Trending'),
2019-12-30 13:55:56 +01:00
tabs: ['Repositories', 'Developers'],
2019-09-30 11:37:51 +02:00
fetchData: (tabIndex) async {
2019-12-30 12:55:59 +01:00
final uri = Uri.parse('https://github-trending-api.now.sh')
2019-09-24 14:45:55 +02:00
.resolve(tabIndex == 1 ? '/developers' : '/');
2019-12-30 12:55:59 +01:00
final res = await http.get(uri);
return json.decode(res.body) as List;
},
2019-09-24 14:45:55 +02:00
bodyBuilder: (payload, activeTab) {
2020-01-12 09:18:37 +01:00
final theme = Provider.of<ThemeModel>(context);
2019-03-02 11:17:46 +01:00
return Column(
2019-09-08 15:20:12 +02:00
crossAxisAlignment: CrossAxisAlignment.stretch,
children: join(
2019-10-02 10:09:54 +02:00
CommonStyle.border,
2019-12-30 12:55:59 +01:00
payload.map<Widget>((v) {
2019-09-24 14:45:55 +02:00
switch (activeTab) {
case 0:
2019-12-30 12:55:59 +01:00
final item = GithubTrendingItem.fromJson(v);
2020-01-11 12:52:17 +01:00
return RepositoryItem.gh(
2019-12-07 07:42:33 +01:00
item.author,
item.avatar,
item.name,
item.description,
2019-12-07 07:43:35 +01:00
item.stars ?? 0,
item.forks ?? 0,
2019-12-07 07:42:33 +01:00
item.language,
item.languageColor,
2020-01-11 12:25:33 +01:00
'${item.currentPeriodStars} stars today',
2019-12-07 07:42:33 +01:00
);
2019-09-24 14:45:55 +02:00
case 1:
2019-12-30 12:55:59 +01:00
final item = GithubTrendingUser.fromJson(v);
2019-09-24 14:45:55 +02:00
return UserItem(
2019-12-30 12:55:59 +01:00
login: item.username,
2019-12-07 07:42:33 +01:00
name: item.name,
avatarUrl: item.avatar,
2020-01-02 06:56:50 +01:00
bio: Row(
children: <Widget>[
2020-01-12 09:18:37 +01:00
Icon(
Octicons.repo,
2020-01-14 06:33:02 +01:00
size: 15,
2020-01-14 11:13:07 +01:00
color: theme.paletteOf(context).secondaryText,
2020-01-12 09:18:37 +01:00
),
2020-01-02 06:56:50 +01:00
SizedBox(width: 2),
2020-01-14 06:33:02 +01:00
Text(item.repo.name)
2020-01-02 06:56:50 +01:00
],
),
2019-09-24 14:45:55 +02:00
);
default:
throw '';
}
}).toList(),
2019-09-08 15:20:12 +02:00
),
2019-03-02 11:17:46 +01:00
);
},
);
}
}