Solian/lib/widgets/account/account_select.dart

175 lines
5.3 KiB
Dart
Raw Normal View History

2024-07-30 12:49:01 +00:00
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:solian/models/account.dart';
import 'package:solian/models/relations.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/providers/relation.dart';
import 'package:solian/services.dart';
import 'package:solian/widgets/account/account_avatar.dart';
class AccountSelector extends StatefulWidget {
final String title;
final Widget? Function(Account item)? trailingBuilder;
final List<int>? initialSelection;
final Function(List<Account>)? onMultipleSelect;
const AccountSelector({
super.key,
required this.title,
this.trailingBuilder,
this.initialSelection,
this.onMultipleSelect,
});
@override
State<AccountSelector> createState() => _AccountSelectorState();
}
class _AccountSelectorState extends State<AccountSelector> {
final TextEditingController _probeController = TextEditingController();
final List<Account> _relativeUsers = List.empty(growable: true);
final List<Account> _pendingUsers = List.empty(growable: true);
final List<Account> _selectedUsers = List.empty(growable: true);
int _accountId = 0;
_revertSelectedUsers() async {
if (widget.initialSelection?.isEmpty ?? true) return;
2024-09-16 03:57:16 +00:00
final client = await ServiceFinder.configureClient('auth');
2024-07-30 12:49:01 +00:00
final idQuery = widget.initialSelection!.join(',');
final resp = await client.get('/users?id=$idQuery');
setState(() {
_selectedUsers.addAll(
2024-09-16 03:57:16 +00:00
resp.body.map((e) => Account.fromJson(e)).toList().cast<Account>(),
2024-07-30 12:49:01 +00:00
);
});
}
_getFriends() async {
final AuthProvider auth = Get.find();
_accountId = auth.userProfile.value!['id'];
final RelationshipProvider provider = Get.find();
final resp = await provider.listRelationWithStatus(1);
setState(() {
_relativeUsers.addAll(
resp.body
.map((e) => Relationship.fromJson(e).related)
.toList()
.cast<Account>(),
);
});
}
_searchAccount() async {
final AuthProvider auth = Get.find();
_accountId = auth.userProfile.value!['id'];
if (_probeController.text.isEmpty) return;
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('auth');
2024-07-30 12:49:01 +00:00
final resp = await client.get(
'/users/search?probe=${_probeController.text}',
);
setState(() {
_pendingUsers.clear();
_pendingUsers.addAll(
resp.body.map((e) => Account.fromJson(e)).toList().cast<Account>(),
);
});
}
bool _checkSelected(Account item) {
return _selectedUsers.any((x) => x.id == item.id);
}
@override
void initState() {
super.initState();
_getFriends();
_revertSelectedUsers();
}
@override
void dispose() {
super.dispose();
_probeController.dispose();
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: MediaQuery.of(context).size.height * 0.85,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.title,
style: Theme.of(context).textTheme.headlineSmall,
).paddingOnly(left: 24, right: 24, top: 32, bottom: 16),
Container(
color: Theme.of(context).colorScheme.secondaryContainer,
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child: TextField(
controller: _probeController,
decoration: InputDecoration(
isCollapsed: true,
border: InputBorder.none,
hintText: 'search'.tr,
),
onSubmitted: (_) {
_searchAccount();
},
),
),
Expanded(
child: ListView.builder(
itemCount: _pendingUsers.isEmpty
? _relativeUsers.length
: _pendingUsers.length,
itemBuilder: (context, index) {
var element = _pendingUsers.isEmpty
? _relativeUsers[index]
: _pendingUsers[index];
return ListTile(
title: Text(element.nick),
subtitle: Text(element.name),
2024-10-11 16:41:03 +00:00
leading: AttachedCircleAvatar(content: element.avatar),
2024-07-30 12:49:01 +00:00
trailing: widget.trailingBuilder != null
? widget.trailingBuilder!(element)
: _checkSelected(element)
? const Icon(Icons.check)
: null,
onTap: element.id == _accountId
? null
: () {
if (widget.onMultipleSelect == null) {
Navigator.pop(context, element);
return;
}
setState(() {
2024-09-16 03:57:16 +00:00
final idx = _selectedUsers
.indexWhere((x) => x.id == element.id);
2024-07-30 12:49:01 +00:00
if (idx != -1) {
_selectedUsers.removeAt(idx);
} else {
_selectedUsers.add(element);
}
});
widget.onMultipleSelect!(_selectedUsers);
},
);
},
),
),
],
),
);
}
}