add universal user list item

This commit is contained in:
krawieck 2021-01-08 18:19:24 +01:00
parent 5feee4c8b8
commit c17714148b
2 changed files with 43 additions and 37 deletions

View File

@ -4,11 +4,11 @@ import 'package:lemmy_api_client/lemmy_api_client.dart';
import '../comment_tree.dart';
import '../hooks/stores.dart';
import '../util/goto.dart';
import '../widgets/comment.dart';
import '../widgets/post.dart';
import '../widgets/sortable_infinite_list.dart';
import 'communities_list.dart';
import 'users_list.dart';
class SearchResultsPage extends HookWidget {
final String instance;
@ -113,11 +113,7 @@ class _SearchResultsList extends HookWidget {
child: Post(data as PostView),
);
case SearchType.users:
// TODO: extract to universal widget
return ListTile(
title: Text((data as UserView).name),
onTap: () =>
goToUser.byId(context, instance, (data as UserView).id));
return UsersListItem(user: data as UserView);
default:
throw Error();
}

View File

@ -2,6 +2,7 @@ import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:lemmy_api_client/lemmy_api_client.dart';
import '../util/goto.dart';
import '../widgets/markdown_text.dart';
/// Infinite list of Users fetched by the given fetcher
@ -31,38 +32,47 @@ class UsersListPage extends StatelessWidget {
iconTheme: theme.iconTheme,
),
body: ListView.builder(
itemBuilder: (context, i) => ListTile(
title: Text((users[i].preferredUsername == null ||
users[i].preferredUsername.isEmpty)
? '@${users[i].name}'
: users[i].preferredUsername),
subtitle: users[i].bio != null
? Opacity(
opacity: 0.5,
child: MarkdownText(
users[i].bio,
instanceHost: users[i].instanceHost,
),
)
: null,
onTap: () => goToUser(context, users[i].id),
leading: users[i].avatar != null
? CachedNetworkImage(
height: 50,
width: 50,
imageUrl: users[i].avatar,
errorWidget: (_, __, ___) =>
const SizedBox(height: 50, width: 50),
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover, image: imageProvider),
),
))
: const SizedBox(width: 50),
),
itemBuilder: (context, i) => UsersListItem(user: users[i]),
itemCount: users.length,
));
}
}
class UsersListItem extends StatelessWidget {
final UserView user;
const UsersListItem({Key key, this.user}) : super(key: key);
@override
Widget build(BuildContext context) => ListTile(
title: Text(
(user.preferredUsername == null || user.preferredUsername.isEmpty)
? '@${user.name}'
: user.preferredUsername),
subtitle: user.bio != null
? Opacity(
opacity: 0.5,
child: MarkdownText(
user.bio,
instanceHost: user.instanceHost,
),
)
: null,
onTap: () => goToUser.byId(context, user.instanceHost, user.id),
leading: user.avatar != null
? CachedNetworkImage(
height: 50,
width: 50,
imageUrl: user.avatar,
errorWidget: (_, __, ___) =>
const SizedBox(height: 50, width: 50),
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover, image: imageProvider),
),
))
: const SizedBox(width: 50),
);
}