fluffychat/lib/pages/sticker_picker_dialog.dart

136 lines
4.7 KiB
Dart
Raw Normal View History

2021-07-23 14:24:52 +02:00
import 'package:flutter/cupertino.dart';
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';
import '../widgets/avatar.dart';
import '../widgets/default_app_bar_search_field.dart';
2021-10-26 18:50:34 +02:00
import '../widgets/event_content/image_bubble.dart';
2021-07-23 14:24:52 +02:00
class StickerPickerDialog extends StatefulWidget {
final Room room;
const StickerPickerDialog({this.room, Key key}) : super(key: key);
@override
StickerPickerDialogState createState() => StickerPickerDialogState();
}
class StickerPickerDialogState extends State<StickerPickerDialog> {
String searchFilter;
@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) {
final pack = stickerPacks[packSlugs[packIndex]];
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)));
}
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(
pack.pack.avatarUrl,
packName,
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) {
final image = pack.images[imageKeys[imageIndex]];
final fakeEvent = Event.fromJson(<String, dynamic>{
'type': EventTypes.Sticker,
'content': <String, dynamic>{
'url': image.url.toString(),
'info': image.info,
},
'event_id': 'fake_event',
}, widget.room);
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,
),
title: DefaultAppBarSearchField(
autofocus: false,
hintText: L10n.of(context).search,
2021-10-14 18:09:30 +02:00
suffix: const Icon(Icons.search_outlined),
2021-07-23 14:24:52 +02:00
onChanged: (s) => setState(() => searchFilter = s),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
_packBuilder,
childCount: packSlugs.length,
)),
],
),
),
);
}
}