fluffychat/lib/pages/chat/sticker_picker_dialog.dart

140 lines
4.8 KiB
Dart
Raw Permalink Normal View History

2021-07-23 14:24:52 +02:00
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
2021-07-23 14:24:52 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:matrix/matrix.dart';
2021-11-09 21:32:16 +01:00
import '../../widgets/avatar.dart';
import 'events/image_bubble.dart';
2021-07-23 14:24:52 +02:00
class StickerPickerDialog extends StatefulWidget {
final Room room;
2022-01-29 12:35:03 +01:00
const StickerPickerDialog({required this.room, Key? key}) : super(key: key);
2021-07-23 14:24:52 +02:00
@override
StickerPickerDialogState createState() => StickerPickerDialogState();
}
class StickerPickerDialogState extends State<StickerPickerDialog> {
2022-01-29 12:35:03 +01:00
String? searchFilter;
2021-07-23 14:24:52 +02:00
@override
Widget build(BuildContext context) {
final stickerPacks = widget.room.getImagePacks(ImagePackUsage.sticker);
final packSlugs = stickerPacks.keys.toList();
2021-10-14 18:09:30 +02:00
// ignore: prefer_function_declarations_over_variables
2021-07-23 14:24:52 +02:00
final _packBuilder = (BuildContext context, int packIndex) {
2022-01-29 12:35:03 +01:00
final pack = stickerPacks[packSlugs[packIndex]]!;
2021-07-23 14:24:52 +02:00
final filteredImagePackImageEntried = pack.images.entries.toList();
if (searchFilter?.isNotEmpty ?? false) {
filteredImagePackImageEntried.removeWhere((e) =>
2022-01-29 12:35:03 +01:00
!(e.key.toLowerCase().contains(searchFilter!.toLowerCase()) ||
2021-07-23 14:24:52 +02:00
(e.value.body
?.toLowerCase()
2022-01-29 12:35:03 +01:00
.contains(searchFilter!.toLowerCase()) ??
2021-07-23 14:24:52 +02:00
false)));
}
final imageKeys =
filteredImagePackImageEntried.map((e) => e.key).toList();
if (imageKeys.isEmpty) {
return Container();
}
final packName = pack.pack.displayName ?? packSlugs[packIndex];
return Column(
children: <Widget>[
2021-10-14 18:09:30 +02:00
if (packIndex != 0) const SizedBox(height: 20),
2021-07-23 14:24:52 +02:00
if (packName != 'user')
ListTile(
leading: Avatar(
2021-11-20 10:42:23 +01:00
mxContent: pack.pack.avatarUrl,
name: packName,
2021-07-23 14:24:52 +02:00
client: widget.room.client,
),
title: Text(packName),
),
2021-10-14 18:09:30 +02:00
const SizedBox(height: 6),
2021-07-23 14:24:52 +02:00
GridView.builder(
itemCount: imageKeys.length,
2021-10-14 18:09:30 +02:00
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
2021-07-23 14:24:52 +02:00
maxCrossAxisExtent: 100),
shrinkWrap: true,
2021-10-14 18:09:30 +02:00
physics: const NeverScrollableScrollPhysics(),
2021-07-23 14:24:52 +02:00
itemBuilder: (BuildContext context, int imageIndex) {
2022-01-29 12:35:03 +01:00
final image = pack.images[imageKeys[imageIndex]]!;
2022-02-06 17:09:45 +01:00
final fakeEvent = Event(
type: EventTypes.Sticker,
content: {
2021-07-23 14:24:52 +02:00
'url': image.url.toString(),
'info': image.info,
},
2022-02-06 17:09:45 +01:00
originServerTs: DateTime.now(),
room: widget.room,
eventId: 'fake_event',
senderId: widget.room.client.userID!,
);
2021-07-23 14:24:52 +02:00
return InkWell(
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];
Navigator.of(context, rootNavigator: false)
.pop<ImagePackImageContent>(imageCopy);
},
child: AbsorbPointer(
absorbing: true,
child: ImageBubble(
fakeEvent,
tapToView: false,
fit: BoxFit.contain,
width: 100,
height: 100,
),
),
);
},
),
],
);
};
return Scaffold(
2021-10-14 18:09:30 +02:00
body: SizedBox(
2021-07-23 14:24:52 +02:00
width: double.maxFinite,
child: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
floating: true,
pinned: true,
automaticallyImplyLeading: false,
titleSpacing: 0,
backgroundColor: Theme.of(context).dialogBackgroundColor,
leading: IconButton(
2021-10-14 18:09:30 +02:00
icon: const Icon(Icons.close),
2021-07-23 14:24:52 +02:00
onPressed: Navigator.of(context, rootNavigator: false).pop,
),
2022-05-18 12:13:59 +02:00
title: TextField(
2021-07-23 14:24:52 +02:00
autofocus: false,
2022-05-18 12:13:59 +02:00
decoration: InputDecoration(
hintText: L10n.of(context)!.search,
suffix: const Icon(Icons.search_outlined),
contentPadding: const EdgeInsets.symmetric(horizontal: 16),
),
2021-07-23 14:24:52 +02:00
onChanged: (s) => setState(() => searchFilter = s),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
_packBuilder,
childCount: packSlugs.length,
)),
],
),
),
);
}
}