OpenContacts/lib/widgets/friends/user_list_tile.dart

96 lines
3 KiB
Dart
Raw Normal View History

import 'package:recon/apis/contact_api.dart';
import 'package:recon/auxiliary.dart';
import 'package:recon/client_holder.dart';
import 'package:recon/models/users/user.dart';
import 'package:recon/widgets/generic_avatar.dart';
2023-04-30 09:43:59 -04:00
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class UserListTile extends StatefulWidget {
const UserListTile({required this.user, required this.isFriend, required this.onChanged, super.key});
2023-04-30 09:43:59 -04:00
final User user;
final bool isFriend;
final Function()? onChanged;
2023-04-30 09:43:59 -04:00
@override
State<UserListTile> createState() => _UserListTileState();
}
class _UserListTileState extends State<UserListTile> {
final DateFormat _regDateFormat = DateFormat.yMMMMd('en_US');
late bool _localAdded = widget.isFriend;
bool _loading = false;
2023-04-30 09:43:59 -04:00
@override
Widget build(BuildContext context) {
final colorScheme = Theme
.of(context)
.colorScheme;
final style = _localAdded ? IconButton.styleFrom(
foregroundColor: colorScheme.onBackground,
side: BorderSide(
color: colorScheme.error,
width: 2
),
) : IconButton.styleFrom(
foregroundColor: colorScheme.onBackground,
side: BorderSide(
color: colorScheme.primary,
width: 2
),
);
2023-04-30 09:43:59 -04:00
return ListTile(
2023-09-29 03:51:46 -04:00
leading: GenericAvatar(imageUri: Aux.resdbToHttp(widget.user.userProfile?.iconUrl),),
2023-04-30 09:43:59 -04:00
title: Text(widget.user.username),
subtitle: Text(_regDateFormat.format(widget.user.registrationDate)),
trailing: IconButton(
splashRadius: 24,
iconSize: 20,
icon: _localAdded ? const Icon(Icons.person_remove) : const Icon(Icons.person_add),
style: style,
onPressed: _loading ? null : () async {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Sorry, this feature is not yet available")));
return;
2023-04-30 09:43:59 -04:00
setState(() {
_loading = true;
});
try {
if (_localAdded) {
2023-09-29 07:17:17 -04:00
await ContactApi.removeUserAsFriend(ClientHolder
.of(context)
.apiClient, user: widget.user);
} else {
2023-09-29 07:17:17 -04:00
await ContactApi.addUserAsFriend(ClientHolder
.of(context)
.apiClient, user: widget.user);
}
setState(() {
_loading = false;
_localAdded = !_localAdded;
});
widget.onChanged?.call();
} catch (e, s) {
FlutterError.reportError(FlutterErrorDetails(exception: e, stack: s));
2023-09-30 08:20:08 -04:00
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
duration: const Duration(seconds: 5),
content: Text(
"Something went wrong: $e",
softWrap: true,
maxLines: null,
),
),
);
2023-09-30 08:20:08 -04:00
}
setState(() {
_loading = false;
});
return;
}
2023-04-30 09:43:59 -04:00
},
),
);
}
}