fluffychat/lib/pages/chat/sticker_picker_dialog.dart

159 lines
5.3 KiB
Dart
Raw Normal View History

2021-07-23 12:24:52 +00:00
import 'package:flutter/material.dart';
2021-10-26 16:50:34 +00:00
2021-07-23 12:24:52 +00:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:matrix/matrix.dart';
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/utils/url_launcher.dart';
import 'package:fluffychat/widgets/mxc_image.dart';
2021-11-09 20:32:16 +00:00
import '../../widgets/avatar.dart';
2021-07-23 12:24:52 +00:00
class StickerPickerDialog extends StatefulWidget {
final Room room;
final void Function(ImagePackImageContent) onSelected;
2021-07-23 12:24:52 +00:00
const StickerPickerDialog({
required this.onSelected,
required this.room,
super.key,
});
2021-07-23 12:24:52 +00:00
@override
StickerPickerDialogState createState() => StickerPickerDialogState();
}
class StickerPickerDialogState extends State<StickerPickerDialog> {
2022-01-29 11:35:03 +00:00
String? searchFilter;
2021-07-23 12:24:52 +00:00
@override
Widget build(BuildContext context) {
final stickerPacks = widget.room.getImagePacks(ImagePackUsage.sticker);
final packSlugs = stickerPacks.keys.toList();
2021-10-14 16:09:30 +00:00
// ignore: prefer_function_declarations_over_variables
2022-08-14 14:59:21 +00:00
final packBuilder = (BuildContext context, int packIndex) {
2022-01-29 11:35:03 +00:00
final pack = stickerPacks[packSlugs[packIndex]]!;
2021-07-23 12:24:52 +00:00
final filteredImagePackImageEntried = pack.images.entries.toList();
if (searchFilter?.isNotEmpty ?? false) {
filteredImagePackImageEntried.removeWhere(
(e) => !(e.key.toLowerCase().contains(searchFilter!.toLowerCase()) ||
(e.value.body
?.toLowerCase()
.contains(searchFilter!.toLowerCase()) ??
false)),
);
2021-07-23 12:24:52 +00:00
}
final imageKeys =
filteredImagePackImageEntried.map((e) => e.key).toList();
if (imageKeys.isEmpty) {
return const SizedBox.shrink();
2021-07-23 12:24:52 +00:00
}
final packName = pack.pack.displayName ?? packSlugs[packIndex];
return Column(
children: <Widget>[
2021-10-14 16:09:30 +00:00
if (packIndex != 0) const SizedBox(height: 20),
2021-07-23 12:24:52 +00:00
if (packName != 'user')
ListTile(
leading: Avatar(
2021-11-20 09:42:23 +00:00
mxContent: pack.pack.avatarUrl,
name: packName,
2021-07-23 12:24:52 +00:00
client: widget.room.client,
),
title: Text(packName),
),
2021-10-14 16:09:30 +00:00
const SizedBox(height: 6),
2021-07-23 12:24:52 +00:00
GridView.builder(
itemCount: imageKeys.length,
2021-10-14 16:09:30 +00:00
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 128,
),
2021-07-23 12:24:52 +00:00
shrinkWrap: true,
2021-10-14 16:09:30 +00:00
physics: const NeverScrollableScrollPhysics(),
2021-07-23 12:24:52 +00:00
itemBuilder: (BuildContext context, int imageIndex) {
2022-01-29 11:35:03 +00:00
final image = pack.images[imageKeys[imageIndex]]!;
2021-07-23 12:24:52 +00:00
return InkWell(
radius: AppConfig.borderRadius,
2021-07-23 12:24:52 +00:00
key: ValueKey(image.url.toString()),
onTap: () {
// copy the image
final imageCopy =
ImagePackImageContent.fromJson(image.toJson().copy());
// set the body, if it doesn't exist, to the key
imageCopy.body ??= imageKeys[imageIndex];
widget.onSelected(imageCopy);
2021-07-23 12:24:52 +00:00
},
child: AbsorbPointer(
absorbing: true,
child: MxcImage(
uri: image.url,
2021-07-23 12:24:52 +00:00
fit: BoxFit.contain,
width: 128,
height: 128,
animated: true,
2021-07-23 12:24:52 +00:00
),
),
);
},
),
],
);
};
return Scaffold(
2024-04-01 08:32:11 +00:00
backgroundColor: Theme.of(context).colorScheme.onInverseSurface,
2021-10-14 16:09:30 +00:00
body: SizedBox(
2021-07-23 12:24:52 +00:00
width: double.maxFinite,
child: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
floating: true,
pinned: true,
automaticallyImplyLeading: false,
2024-04-01 08:32:11 +00:00
backgroundColor: Colors.transparent,
title: SizedBox(
height: 42,
child: TextField(
autofocus: false,
decoration: InputDecoration(
hintText: L10n.of(context)!.search,
prefixIcon: const Icon(Icons.search_outlined),
contentPadding: EdgeInsets.zero,
),
onChanged: (s) => setState(() => searchFilter = s),
2022-05-18 10:13:59 +00:00
),
2021-07-23 12:24:52 +00:00
),
),
if (packSlugs.isEmpty)
SliverFillRemaining(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(L10n.of(context)!.noEmotesFound),
const SizedBox(height: 12),
OutlinedButton.icon(
onPressed: () => UrlLauncher(
context,
'https://matrix.to/#/#fluffychat-stickers:janian.de',
).launchUrl(),
icon: const Icon(Icons.explore_outlined),
label: Text(L10n.of(context)!.discover),
),
],
),
),
)
else
SliverList(
delegate: SliverChildBuilderDelegate(
packBuilder,
childCount: packSlugs.length,
),
),
2021-07-23 12:24:52 +00:00
],
),
),
);
}
}