Solian/lib/screens/account/friend.dart

194 lines
5.4 KiB
Dart
Raw Normal View History

2024-05-23 13:12:47 +00:00
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
2024-05-23 13:12:47 +00:00
import 'package:get/get.dart';
import 'package:solian/exts.dart';
2024-07-23 17:17:41 +00:00
import 'package:solian/models/relations.dart';
import 'package:solian/providers/relation.dart';
import 'package:solian/theme.dart';
2024-07-23 17:17:41 +00:00
import 'package:solian/widgets/account/relative_list.dart';
2024-05-23 13:12:47 +00:00
class FriendScreen extends StatefulWidget {
const FriendScreen({super.key});
@override
State<FriendScreen> createState() => _FriendScreenState();
}
2024-07-23 17:17:41 +00:00
class _FriendScreenState extends State<FriendScreen>
with SingleTickerProviderStateMixin {
late final TabController _tabController;
2024-05-23 13:12:47 +00:00
bool _isBusy = false;
2024-07-23 17:17:41 +00:00
List<Relationship> _relations = List.empty();
2024-05-23 13:12:47 +00:00
List<Relationship> _filterByStatus(int status) {
2024-07-23 17:17:41 +00:00
return _relations.where((x) => x.status == status).toList();
2024-05-23 13:12:47 +00:00
}
Future<void> _loadRelations() async {
2024-05-23 13:12:47 +00:00
setState(() => _isBusy = true);
final RelationshipProvider relations = Get.find();
final resp = await relations.listRelation();
2024-05-23 13:12:47 +00:00
setState(() {
2024-07-23 17:17:41 +00:00
_relations = resp.body
.map((e) => Relationship.fromJson(e))
2024-05-23 13:12:47 +00:00
.toList()
2024-07-23 17:17:41 +00:00
.cast<Relationship>();
2024-05-23 13:12:47 +00:00
_isBusy = false;
});
2024-08-02 15:15:28 +00:00
relations.friendRequestCount.value =
_relations.where((x) => x.status == 0).length;
2024-05-23 13:12:47 +00:00
}
void _promptAddFriend() async {
2024-07-23 17:17:41 +00:00
final RelationshipProvider provider = Get.find();
2024-05-23 13:12:47 +00:00
final controller = TextEditingController();
2024-07-23 17:17:41 +00:00
final input = await showDialog<String?>(
2024-05-23 13:12:47 +00:00
context: context,
builder: (context) {
return AlertDialog(
title: Text('accountFriendNew'.tr),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('accountFriendNewHint'.tr, textAlign: TextAlign.left),
const Gap(18),
2024-05-23 13:12:47 +00:00
TextField(
controller: controller,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'username'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
),
],
),
actions: <Widget>[
TextButton(
style: TextButton.styleFrom(
foregroundColor:
Theme.of(context).colorScheme.onSurface.withOpacity(0.8),
),
onPressed: () => Navigator.pop(context),
child: Text('cancel'.tr),
),
TextButton(
child: Text('next'.tr),
onPressed: () {
Navigator.pop(context, controller.text);
},
),
],
);
},
);
WidgetsBinding.instance.addPostFrameCallback((_) => controller.dispose());
if (input == null || input.isEmpty) return;
try {
setState(() => _isBusy = true);
2024-07-23 17:17:41 +00:00
await provider.makeFriend(input);
2024-05-23 13:12:47 +00:00
} catch (e) {
context.showErrorDialog(e);
} finally {
setState(() => _isBusy = false);
}
}
@override
void initState() {
super.initState();
2024-07-23 17:17:41 +00:00
_tabController = TabController(length: 3, vsync: this);
2024-05-23 13:12:47 +00:00
_loadRelations().then((_) {
if (_filterByStatus(0).isEmpty) {
2024-07-23 17:17:41 +00:00
_tabController.animateTo(1);
}
});
2024-05-23 13:12:47 +00:00
}
@override
Widget build(BuildContext context) {
return Material(
color: Theme.of(context).colorScheme.surface,
child: Scaffold(
2024-07-23 17:17:41 +00:00
appBar: AppBar(
centerTitle: false,
title: Text('accountFriend'.tr),
actions: [
if (_isBusy)
SizedBox(
height: 48,
width: 48,
child: const CircularProgressIndicator(
strokeWidth: 3,
).paddingAll(14),
),
SizedBox(
width: SolianTheme.isLargeScreen(context) ? 8 : 16,
),
],
2024-07-23 17:17:41 +00:00
bottom: TabBar(
controller: _tabController,
tabs: const [
Tab(icon: Icon(Icons.call_received)),
Tab(icon: Icon(Icons.people)),
Tab(icon: Icon(Icons.call_made)),
],
),
),
2024-05-23 13:12:47 +00:00
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () => _promptAddFriend(),
2024-05-23 13:12:47 +00:00
),
2024-07-23 17:17:41 +00:00
body: TabBarView(
controller: _tabController,
children: [
RefreshIndicator(
onRefresh: () => _loadRelations(),
2024-07-23 17:17:41 +00:00
child: CustomScrollView(
slivers: [
SilverRelativeList(
items: _filterByStatus(0),
onUpdate: () => _loadRelations(),
2024-05-23 13:12:47 +00:00
),
2024-07-23 17:17:41 +00:00
],
2024-05-23 13:12:47 +00:00
),
2024-07-23 17:17:41 +00:00
),
RefreshIndicator(
onRefresh: () => _loadRelations(),
2024-07-23 17:17:41 +00:00
child: CustomScrollView(
slivers: [
SilverRelativeList(
items: _filterByStatus(1),
onUpdate: () => _loadRelations(),
2024-05-23 13:12:47 +00:00
),
2024-07-23 17:17:41 +00:00
],
2024-05-23 13:12:47 +00:00
),
2024-07-23 17:17:41 +00:00
),
RefreshIndicator(
onRefresh: () => _loadRelations(),
2024-07-23 17:17:41 +00:00
child: CustomScrollView(
slivers: [
SilverRelativeList(
items: _filterByStatus(3),
onUpdate: () => _loadRelations(),
2024-07-23 17:17:41 +00:00
),
],
2024-05-23 13:12:47 +00:00
),
2024-07-23 17:17:41 +00:00
),
],
2024-05-23 13:12:47 +00:00
),
),
);
}
}