lemmur-app-android/lib/pages/communities_list.dart

87 lines
2.6 KiB
Dart
Raw Normal View History

2020-09-08 23:01:40 +02:00
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
2021-01-24 20:01:55 +01:00
import 'package:lemmy_api_client/v2.dart';
2020-09-08 23:01:40 +02:00
import '../util/goto.dart';
2020-09-08 23:01:40 +02:00
import '../widgets/markdown_text.dart';
import '../widgets/sortable_infinite_list.dart';
2020-09-08 23:01:40 +02:00
2020-09-30 19:05:00 +02:00
/// Infinite list of Communities fetched by the given fetcher
2020-09-08 23:01:40 +02:00
class CommunitiesListPage extends StatelessWidget {
final String title;
final Future<List<CommunityView>> Function(
int page,
int batchSize,
SortType sortType,
) fetcher;
2020-09-08 23:01:40 +02:00
2020-09-29 21:00:50 +02:00
const CommunitiesListPage({Key key, @required this.fetcher, this.title = ''})
: assert(fetcher != null),
2020-09-29 21:00:50 +02:00
assert(title != null),
2020-09-08 23:01:40 +02:00
super(key: key);
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Scaffold(
appBar: AppBar(
brightness: theme.brightness,
2020-09-29 21:00:50 +02:00
title: Text(title, style: theme.textTheme.headline6),
centerTitle: true,
backgroundColor: theme.cardColor,
iconTheme: theme.iconTheme,
),
2020-09-29 11:13:17 +02:00
body: SortableInfiniteList<CommunityView>(
2020-09-29 11:11:43 +02:00
fetcher: fetcher,
builder: (community) => Column(
children: [
2021-01-03 19:43:39 +01:00
const Divider(),
2021-01-08 18:07:58 +01:00
CommunitiesListItem(
community: community,
)
2020-09-29 11:11:43 +02:00
],
),
2020-09-29 11:13:17 +02:00
),
);
}
2020-09-08 23:01:40 +02:00
}
2021-01-08 18:07:58 +01:00
class CommunitiesListItem extends StatelessWidget {
final CommunityView community;
const CommunitiesListItem({Key key, @required this.community})
: assert(community != null),
super(key: key);
2021-01-08 18:07:58 +01:00
@override
Widget build(BuildContext context) => ListTile(
2021-01-24 20:01:55 +01:00
title: Text(community.community.name),
subtitle: community.community.description != null
2021-01-08 18:07:58 +01:00
? Opacity(
opacity: 0.7,
child: MarkdownText(
2021-01-24 20:01:55 +01:00
community.community.description,
2021-01-08 18:07:58 +01:00
instanceHost: community.instanceHost,
),
)
: null,
2021-01-24 20:01:55 +01:00
onTap: () => goToCommunity.byId(
context, community.instanceHost, community.community.id),
leading: community.community.icon != null
2021-01-08 18:07:58 +01:00
? CachedNetworkImage(
height: 50,
width: 50,
2021-01-24 20:01:55 +01:00
imageUrl: community.community.icon,
2021-01-08 18:07:58 +01:00
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover, image: imageProvider),
),
),
errorWidget: (_, __, ___) => const SizedBox(width: 50),
)
: const SizedBox(width: 50),
);
}