ente/lib/ui/viewer/file/fading_bottom_bar.dart

274 lines
8 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:photos/models/file.dart';
import 'package:photos/models/file_type.dart';
import 'package:photos/models/selected_files.dart';
import 'package:photos/models/trash_file.dart';
import 'package:photos/theme/colors.dart';
import 'package:photos/theme/ente_theme.dart';
import "package:photos/ui/actions/file/file_actions.dart";
2023-02-14 10:32:56 +00:00
import 'package:photos/ui/collection_action_sheet.dart';
import 'package:photos/utils/delete_file_util.dart';
import 'package:photos/utils/share_util.dart';
class FadingBottomBar extends StatefulWidget {
final File file;
final Function(File) onEditRequested;
final Function(File) onFileRemoved;
2021-09-15 20:40:08 +00:00
final bool showOnlyInfoButton;
final int? userID;
const FadingBottomBar(
2021-07-11 08:01:47 +00:00
this.file,
2021-09-15 20:40:08 +00:00
this.onEditRequested,
this.showOnlyInfoButton, {
required this.onFileRemoved,
this.userID,
Key? key,
}) : super(key: key);
@override
FadingBottomBarState createState() => FadingBottomBarState();
}
class FadingBottomBarState extends State<FadingBottomBar> {
bool _shouldHide = false;
final GlobalKey shareButtonKey = GlobalKey();
@override
Widget build(BuildContext context) {
return _getBottomBar();
}
void hide() {
setState(() {
_shouldHide = true;
});
}
void show() {
setState(() {
_shouldHide = false;
});
}
void safeRefresh() {
if (mounted) {
setState(() {});
}
}
Widget _getBottomBar() {
2022-08-29 14:43:31 +00:00
final List<Widget> children = [];
final bool isOwnedByUser =
widget.file.ownerID == null || widget.file.ownerID == widget.userID;
children.add(
2021-10-03 21:58:33 +00:00
Tooltip(
2022-05-17 11:38:21 +00:00
message: "Info",
2021-10-03 21:58:33 +00:00
child: Padding(
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: IconButton(
icon: Icon(
Platform.isAndroid ? Icons.info_outline : CupertinoIcons.info,
color: Colors.white,
),
onPressed: () async {
await _displayInfo(widget.file);
safeRefresh(); //to instantly show the new caption if keypad is closed after pressing 'done' - here the caption will be updated before the bottom sheet is closed
await Future.delayed(
const Duration(milliseconds: 500),
); //Waiting for some time till the caption gets updated in db if the user closes the bottom sheet without pressing 'done'
safeRefresh();
2021-10-03 21:58:33 +00:00
},
),
),
),
);
if (widget.file is TrashFile) {
_addTrashOptions(children);
}
if (!widget.showOnlyInfoButton && widget.file is! TrashFile) {
2021-09-15 20:40:08 +00:00
if (widget.file.fileType == FileType.image ||
widget.file.fileType == FileType.livePhoto) {
children.add(
2021-10-03 21:58:33 +00:00
Tooltip(
2022-05-17 11:38:21 +00:00
message: "Edit",
2021-10-03 21:58:33 +00:00
child: Padding(
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: IconButton(
2022-07-04 06:02:17 +00:00
icon: const Icon(
Icons.tune_outlined,
color: Colors.white,
),
2021-10-03 21:58:33 +00:00
onPressed: () {
widget.onEditRequested(widget.file);
},
),
2021-09-15 20:40:08 +00:00
),
),
);
}
if (isOwnedByUser) {
children.add(
Tooltip(
message: "Delete",
child: Padding(
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: IconButton(
icon: Icon(
Platform.isAndroid
? Icons.delete_outline
: CupertinoIcons.delete,
color: Colors.white,
),
onPressed: () async {
await _showSingleFileDeleteSheet(widget.file);
},
2021-10-03 21:57:09 +00:00
),
),
),
);
}
children.add(
2021-10-03 21:58:33 +00:00
Tooltip(
2022-05-17 11:38:21 +00:00
message: "Share",
2021-10-03 21:58:33 +00:00
child: Padding(
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: IconButton(
key: shareButtonKey,
icon: Icon(
Platform.isAndroid
? Icons.share_outlined
: CupertinoIcons.share,
color: Colors.white,
),
2021-10-03 21:58:33 +00:00
onPressed: () {
share(context, [widget.file], shareButtonKey: shareButtonKey);
2021-10-03 21:58:33 +00:00
},
),
),
),
);
}
2022-08-29 14:43:31 +00:00
final safeAreaBottomPadding = MediaQuery.of(context).padding.bottom * .5;
return IgnorePointer(
ignoring: _shouldHide,
child: AnimatedOpacity(
2022-07-03 09:45:00 +00:00
opacity: _shouldHide ? 0 : 1,
2022-07-04 06:02:17 +00:00
duration: const Duration(milliseconds: 150),
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.transparent,
Colors.black.withOpacity(0.6),
Colors.black.withOpacity(0.72),
],
stops: const [0, 0.8, 1],
),
),
child: Padding(
padding: EdgeInsets.only(bottom: safeAreaBottomPadding),
2022-11-05 04:41:21 +00:00
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
2022-11-05 12:01:25 +00:00
widget.file.caption?.isNotEmpty ?? false
2022-11-05 04:41:21 +00:00
? Padding(
padding: const EdgeInsets.fromLTRB(
16,
28,
16,
12,
),
child: Text(
widget.file.caption!,
overflow: TextOverflow.ellipsis,
maxLines: 4,
2022-11-05 04:41:21 +00:00
style: getEnteTextTheme(context)
.small
.copyWith(color: textBaseDark),
2022-11-05 05:58:11 +00:00
textAlign: TextAlign.center,
2022-11-05 04:41:21 +00:00
),
)
: const SizedBox.shrink(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: children,
),
],
),
),
2021-09-15 20:40:08 +00:00
),
),
),
);
}
Future<void> _showSingleFileDeleteSheet(File file) async {
await showSingleFileDeleteSheet(
context,
file,
onFileRemoved: widget.onFileRemoved,
);
}
void _addTrashOptions(List<Widget> children) {
children.add(
Tooltip(
2022-05-17 11:38:21 +00:00
message: "Restore",
child: Padding(
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: IconButton(
2022-07-04 06:02:17 +00:00
icon: const Icon(
Icons.restore_outlined,
color: Colors.white,
),
onPressed: () {
final selectedFiles = SelectedFiles();
selectedFiles.toggleSelection(widget.file);
2023-02-14 10:13:34 +00:00
showCollectionActionSheet(
2022-06-11 08:23:52 +00:00
context,
2023-02-14 10:13:34 +00:00
selectedFiles: selectedFiles,
2023-01-25 04:57:40 +00:00
actionType: CollectionActionType.restoreFiles,
2022-06-11 08:23:52 +00:00
);
},
),
),
),
);
children.add(
Tooltip(
2022-05-17 11:38:21 +00:00
message: "Delete",
child: Padding(
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: IconButton(
2022-07-04 06:02:17 +00:00
icon: const Icon(
Icons.delete_forever_outlined,
color: Colors.white,
),
onPressed: () async {
final trashedFile = <TrashFile>[];
trashedFile.add(widget.file as TrashFile);
if (await deleteFromTrash(context, trashedFile) == true) {
Navigator.pop(context);
}
},
),
),
),
);
}
Future<void> _displayInfo(File file) async {
await showInfoSheet(context, file);
}
}