Solian/lib/widgets/channel/channel_list.dart

177 lines
4.8 KiB
Dart
Raw Normal View History

2024-05-29 15:22:24 +00:00
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get/get.dart';
import 'package:solian/models/channel.dart';
import 'package:solian/router.dart';
import 'package:solian/widgets/account/account_avatar.dart';
2024-05-30 14:02:54 +00:00
class ChannelListWidget extends StatefulWidget {
2024-05-29 15:22:24 +00:00
final List<Channel> channels;
final int selfId;
2024-07-12 13:59:16 +00:00
final bool isDense;
2024-05-30 14:02:54 +00:00
final bool noCategory;
2024-07-12 13:59:16 +00:00
final bool useReplace;
2024-05-29 15:22:24 +00:00
2024-05-30 14:02:54 +00:00
const ChannelListWidget({
super.key,
required this.channels,
required this.selfId,
2024-07-12 13:59:16 +00:00
this.isDense = false,
2024-05-30 14:02:54 +00:00
this.noCategory = false,
2024-07-12 13:59:16 +00:00
this.useReplace = false,
2024-05-30 14:02:54 +00:00
});
2024-05-29 15:22:24 +00:00
@override
2024-05-30 14:02:54 +00:00
State<ChannelListWidget> createState() => _ChannelListWidgetState();
}
2024-05-29 15:22:24 +00:00
2024-05-30 14:02:54 +00:00
class _ChannelListWidgetState extends State<ChannelListWidget> {
final List<Channel> _globalChannels = List.empty(growable: true);
final Map<String, List<Channel>> _inRealms = {};
2024-05-29 15:22:24 +00:00
2024-05-30 14:02:54 +00:00
void mapChannels() {
_inRealms.clear();
_globalChannels.clear();
if (widget.noCategory) {
_globalChannels.addAll(widget.channels);
return;
}
for (final channel in widget.channels) {
if (channel.realmId != null) {
2024-06-01 16:39:50 +00:00
if (_inRealms[channel.realm!.alias] == null) {
_inRealms[channel.realm!.alias] = List.empty(growable: true);
2024-05-30 14:02:54 +00:00
}
2024-06-01 16:39:50 +00:00
_inRealms[channel.realm!.alias]!.add(channel);
2024-05-30 14:02:54 +00:00
} else {
_globalChannels.add(channel);
}
}
}
@override
void didUpdateWidget(covariant ChannelListWidget oldWidget) {
super.didUpdateWidget(oldWidget);
2024-07-12 13:59:16 +00:00
setState(() => mapChannels());
2024-05-30 14:02:54 +00:00
}
2024-06-22 18:25:45 +00:00
@override
void initState() {
super.initState();
2024-07-12 13:59:16 +00:00
mapChannels();
2024-06-22 18:25:45 +00:00
}
2024-07-12 13:59:16 +00:00
void gotoChannel(Channel item) {
if (widget.useReplace) {
AppRouter.instance.pushReplacementNamed(
'channelChat',
pathParameters: {'alias': item.alias},
queryParameters: {
if (item.realmId != null) 'realm': item.realm!.alias,
},
);
} else {
AppRouter.instance.pushNamed(
'channelChat',
pathParameters: {'alias': item.alias},
queryParameters: {
if (item.realmId != null) 'realm': item.realm!.alias,
},
);
}
}
Widget buildItem(Channel item) {
final padding = widget.isDense
? const EdgeInsets.symmetric(horizontal: 20)
: const EdgeInsets.symmetric(horizontal: 24);
if (item.type == 1) {
final otherside = item.members!
2024-05-30 14:02:54 +00:00
.where((e) => e.account.externalId != widget.selfId)
.first;
return ListTile(
leading: AccountAvatar(
content: otherside.account.avatar,
2024-07-12 13:59:16 +00:00
radius: widget.isDense ? 12 : 24,
2024-05-30 14:02:54 +00:00
bgColor: Colors.indigo,
feColor: Colors.white,
),
2024-07-12 13:59:16 +00:00
contentPadding: padding,
2024-05-30 14:02:54 +00:00
title: Text(otherside.account.nick),
2024-07-12 13:59:16 +00:00
subtitle: !widget.isDense
? Text(
'channelDirectDescription'.trParams(
{'username': '@${otherside.account.name}'},
),
)
: null,
onTap: () => gotoChannel(item),
);
} else {
return ListTile(
minTileHeight: item.realmId == null
? 48
: widget.isDense
? 24
: null,
leading: CircleAvatar(
backgroundColor:
item.realmId == null ? Colors.indigo : Colors.transparent,
radius: widget.isDense ? 12 : 24,
child: FaIcon(
FontAwesomeIcons.hashtag,
color: item.realmId == null ? Colors.white : Colors.indigo,
size: widget.isDense ? 12 : 16,
),
2024-05-30 14:02:54 +00:00
),
2024-07-12 13:59:16 +00:00
contentPadding: padding,
title: Text(item.name),
subtitle: !widget.isDense ? Text(item.description) : null,
onTap: () => gotoChannel(item),
2024-05-30 14:02:54 +00:00
);
}
2024-05-29 15:22:24 +00:00
}
2024-05-30 14:02:54 +00:00
@override
Widget build(BuildContext context) {
if (widget.noCategory) {
2024-07-06 10:17:54 +00:00
return CustomScrollView(
slivers: [
SliverList.builder(
itemCount: _globalChannels.length,
itemBuilder: (context, index) {
final element = _globalChannels[index];
return buildItem(element);
},
),
],
2024-05-30 14:02:54 +00:00
);
}
2024-07-06 10:17:54 +00:00
return CustomScrollView(
slivers: [
SliverList.builder(
itemCount: _globalChannels.length,
itemBuilder: (context, index) {
final element = _globalChannels[index];
return buildItem(element);
},
),
2024-07-12 13:59:16 +00:00
SliverList.list(
children: _inRealms.entries.map((element) {
return ExpansionTile(
tilePadding: const EdgeInsets.symmetric(horizontal: 24),
minTileHeight: 48,
title: Text(element.value.first.realm!.name),
children: element.value.map((x) => buildItem(x)).toList(),
);
}).toList(),
),
2024-05-30 14:02:54 +00:00
],
);
}
2024-05-29 15:22:24 +00:00
}