2022-10-03 14:32:39 +00:00
|
|
|
import 'package:expandable/expandable.dart';
|
2022-09-28 05:00:43 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-10-04 13:15:51 +00:00
|
|
|
import 'package:photos/ente_theme_data.dart';
|
2023-01-06 06:15:17 +00:00
|
|
|
import 'package:photos/theme/ente_theme.dart';
|
2022-09-28 05:35:17 +00:00
|
|
|
|
2022-10-03 14:32:39 +00:00
|
|
|
class MenuItemWidget extends StatefulWidget {
|
2022-09-28 12:58:07 +00:00
|
|
|
final Widget captionedTextWidget;
|
2022-10-22 06:26:47 +00:00
|
|
|
final bool isExpandable;
|
2022-11-01 11:32:29 +00:00
|
|
|
|
|
|
|
/// leading icon can be passed without specifing size of icon,
|
|
|
|
/// this component sets size to 20x20 irrespective of passed icon's size
|
2022-09-28 12:22:16 +00:00
|
|
|
final IconData? leadingIcon;
|
|
|
|
final Color? leadingIconColor;
|
2022-11-01 11:32:29 +00:00
|
|
|
|
2022-11-22 04:00:12 +00:00
|
|
|
final Widget? leadingIconWidget;
|
|
|
|
// leadIconSize deafult value is 20.
|
|
|
|
final double leadingIconSize;
|
|
|
|
|
2022-11-01 11:32:29 +00:00
|
|
|
/// trailing icon can be passed without size as default size set by
|
|
|
|
/// flutter is what this component expects
|
2022-09-29 12:21:31 +00:00
|
|
|
final IconData? trailingIcon;
|
2022-12-31 18:33:12 +00:00
|
|
|
final Color? trailingIconColor;
|
2022-12-06 18:03:39 +00:00
|
|
|
final Widget? trailingWidget;
|
2022-09-28 11:51:52 +00:00
|
|
|
final bool trailingIconIsMuted;
|
2023-01-08 11:15:00 +00:00
|
|
|
|
|
|
|
/// If provided, add this much extra spacing to the right of the trailing icon.
|
|
|
|
final double trailingExtraMargin;
|
2022-10-04 10:07:55 +00:00
|
|
|
final VoidCallback? onTap;
|
2022-10-05 05:37:09 +00:00
|
|
|
final VoidCallback? onDoubleTap;
|
2022-09-29 12:21:31 +00:00
|
|
|
final Color? menuItemColor;
|
2022-10-03 06:25:23 +00:00
|
|
|
final bool alignCaptionedTextToLeft;
|
2022-10-03 07:19:12 +00:00
|
|
|
final double borderRadius;
|
2022-10-21 13:37:57 +00:00
|
|
|
final Color? pressedColor;
|
2022-10-03 14:32:39 +00:00
|
|
|
final ExpandableController? expandableController;
|
2022-10-28 05:16:00 +00:00
|
|
|
final bool isBottomBorderRadiusRemoved;
|
|
|
|
final bool isTopBorderRadiusRemoved;
|
2022-11-01 11:32:29 +00:00
|
|
|
|
|
|
|
/// disable gesture detector if not used
|
2022-10-28 05:39:11 +00:00
|
|
|
final bool isGestureDetectorDisabled;
|
2022-11-22 04:00:12 +00:00
|
|
|
|
2022-09-28 11:51:52 +00:00
|
|
|
const MenuItemWidget({
|
2022-09-28 12:58:07 +00:00
|
|
|
required this.captionedTextWidget,
|
2022-10-22 06:26:47 +00:00
|
|
|
this.isExpandable = false,
|
2022-09-28 12:22:16 +00:00
|
|
|
this.leadingIcon,
|
|
|
|
this.leadingIconColor,
|
2022-11-22 04:00:12 +00:00
|
|
|
this.leadingIconSize = 20.0,
|
|
|
|
this.leadingIconWidget,
|
2022-09-28 11:51:52 +00:00
|
|
|
this.trailingIcon,
|
2022-12-31 18:33:12 +00:00
|
|
|
this.trailingIconColor,
|
2022-12-06 18:03:39 +00:00
|
|
|
this.trailingWidget,
|
2022-09-28 11:51:52 +00:00
|
|
|
this.trailingIconIsMuted = false,
|
2023-01-08 11:15:00 +00:00
|
|
|
this.trailingExtraMargin = 0.0,
|
2022-09-28 11:51:52 +00:00
|
|
|
this.onTap,
|
2022-10-05 05:37:09 +00:00
|
|
|
this.onDoubleTap,
|
2022-09-29 12:21:31 +00:00
|
|
|
this.menuItemColor,
|
2022-10-03 06:25:23 +00:00
|
|
|
this.alignCaptionedTextToLeft = false,
|
2022-10-03 07:19:12 +00:00
|
|
|
this.borderRadius = 4.0,
|
2022-10-21 13:37:57 +00:00
|
|
|
this.pressedColor,
|
2022-10-03 14:32:39 +00:00
|
|
|
this.expandableController,
|
2022-10-28 05:16:00 +00:00
|
|
|
this.isBottomBorderRadiusRemoved = false,
|
|
|
|
this.isTopBorderRadiusRemoved = false,
|
2022-10-28 05:39:11 +00:00
|
|
|
this.isGestureDetectorDisabled = false,
|
2022-09-28 11:51:52 +00:00
|
|
|
Key? key,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
2022-10-03 14:32:39 +00:00
|
|
|
@override
|
|
|
|
State<MenuItemWidget> createState() => _MenuItemWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MenuItemWidgetState extends State<MenuItemWidget> {
|
2022-10-21 13:37:57 +00:00
|
|
|
Color? menuItemColor;
|
2022-11-22 04:00:12 +00:00
|
|
|
|
2022-10-03 14:32:39 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
2022-10-21 13:37:57 +00:00
|
|
|
menuItemColor = widget.menuItemColor;
|
2022-10-03 14:32:39 +00:00
|
|
|
if (widget.expandableController != null) {
|
|
|
|
widget.expandableController!.addListener(() {
|
|
|
|
setState(() {});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2022-10-22 06:13:35 +00:00
|
|
|
@override
|
|
|
|
void didChangeDependencies() {
|
|
|
|
menuItemColor = widget.menuItemColor;
|
|
|
|
super.didChangeDependencies();
|
|
|
|
}
|
|
|
|
|
2022-10-03 14:32:39 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
if (widget.expandableController != null) {
|
|
|
|
widget.expandableController!.dispose();
|
|
|
|
}
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2022-09-28 11:51:52 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-10-28 05:39:11 +00:00
|
|
|
return widget.isExpandable || widget.isGestureDetectorDisabled
|
2022-09-29 02:08:02 +00:00
|
|
|
? menuItemWidget(context)
|
|
|
|
: GestureDetector(
|
2022-10-04 10:07:55 +00:00
|
|
|
onTap: widget.onTap,
|
2022-10-05 05:37:09 +00:00
|
|
|
onDoubleTap: widget.onDoubleTap,
|
2022-10-21 13:37:57 +00:00
|
|
|
onTapDown: _onTapDown,
|
|
|
|
onTapUp: _onTapUp,
|
2022-10-21 14:00:17 +00:00
|
|
|
onTapCancel: _onCancel,
|
2022-09-29 02:08:02 +00:00
|
|
|
child: menuItemWidget(context),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget menuItemWidget(BuildContext context) {
|
2022-10-05 14:14:28 +00:00
|
|
|
final enteColorScheme = Theme.of(context).colorScheme.enteTheme.colorScheme;
|
2022-10-07 06:39:39 +00:00
|
|
|
final borderRadius = Radius.circular(widget.borderRadius);
|
|
|
|
final isExpanded = widget.expandableController?.value;
|
2022-10-28 05:16:00 +00:00
|
|
|
final bottomBorderRadius =
|
|
|
|
(isExpanded != null && isExpanded) || widget.isBottomBorderRadiusRemoved
|
|
|
|
? const Radius.circular(0)
|
|
|
|
: borderRadius;
|
|
|
|
final topBorderRadius = widget.isTopBorderRadiusRemoved
|
2022-10-07 06:39:39 +00:00
|
|
|
? const Radius.circular(0)
|
|
|
|
: borderRadius;
|
|
|
|
return AnimatedContainer(
|
2022-10-21 13:37:57 +00:00
|
|
|
duration: const Duration(milliseconds: 20),
|
2022-09-29 02:08:02 +00:00
|
|
|
width: double.infinity,
|
2022-11-01 11:32:29 +00:00
|
|
|
padding: const EdgeInsets.only(left: 16, right: 12),
|
2022-10-03 07:19:12 +00:00
|
|
|
decoration: BoxDecoration(
|
2022-10-07 06:39:39 +00:00
|
|
|
borderRadius: BorderRadius.only(
|
2022-10-28 05:16:00 +00:00
|
|
|
topLeft: topBorderRadius,
|
|
|
|
topRight: topBorderRadius,
|
2022-10-07 06:39:39 +00:00
|
|
|
bottomLeft: bottomBorderRadius,
|
|
|
|
bottomRight: bottomBorderRadius,
|
|
|
|
),
|
2022-10-21 13:37:57 +00:00
|
|
|
color: menuItemColor,
|
2022-10-03 07:19:12 +00:00
|
|
|
),
|
2022-09-29 02:08:02 +00:00
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
2022-10-03 14:32:39 +00:00
|
|
|
widget.alignCaptionedTextToLeft && widget.leadingIcon == null
|
2022-10-03 06:25:23 +00:00
|
|
|
? const SizedBox.shrink()
|
|
|
|
: Padding(
|
|
|
|
padding: const EdgeInsets.only(right: 10),
|
|
|
|
child: SizedBox(
|
2022-11-22 04:00:12 +00:00
|
|
|
height: widget.leadingIconSize,
|
|
|
|
width: widget.leadingIconSize,
|
2022-10-03 14:32:39 +00:00
|
|
|
child: widget.leadingIcon == null
|
2022-11-22 04:00:12 +00:00
|
|
|
? (widget.leadingIconWidget != null
|
|
|
|
? FittedBox(
|
|
|
|
fit: BoxFit.contain,
|
|
|
|
child: widget.leadingIconWidget,
|
|
|
|
)
|
|
|
|
: const SizedBox.shrink())
|
2022-10-03 06:25:23 +00:00
|
|
|
: FittedBox(
|
|
|
|
fit: BoxFit.contain,
|
|
|
|
child: Icon(
|
2022-10-03 14:32:39 +00:00
|
|
|
widget.leadingIcon,
|
2022-11-21 10:39:14 +00:00
|
|
|
color: widget.leadingIconColor ??
|
|
|
|
enteColorScheme.strokeBase,
|
2022-10-03 06:25:23 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2022-10-03 14:32:39 +00:00
|
|
|
widget.captionedTextWidget,
|
|
|
|
widget.expandableController != null
|
2022-10-07 08:04:27 +00:00
|
|
|
? AnimatedOpacity(
|
|
|
|
duration: const Duration(milliseconds: 100),
|
2022-10-13 12:25:21 +00:00
|
|
|
curve: Curves.easeInOut,
|
2022-10-07 08:04:27 +00:00
|
|
|
opacity: isExpanded! ? 0 : 1,
|
|
|
|
child: AnimatedSwitcher(
|
|
|
|
transitionBuilder: (child, animation) {
|
|
|
|
return ScaleTransition(scale: animation, child: child);
|
|
|
|
},
|
|
|
|
duration: const Duration(milliseconds: 200),
|
2022-10-13 12:25:21 +00:00
|
|
|
switchInCurve: Curves.easeOut,
|
2022-10-12 03:51:17 +00:00
|
|
|
child: isExpanded
|
2022-10-07 08:04:27 +00:00
|
|
|
? const SizedBox.shrink()
|
2022-12-31 18:33:12 +00:00
|
|
|
: Icon(
|
|
|
|
widget.trailingIcon,
|
|
|
|
color: widget.trailingIconColor,
|
|
|
|
),
|
2022-10-07 08:04:27 +00:00
|
|
|
),
|
|
|
|
)
|
2022-10-03 14:32:39 +00:00
|
|
|
: widget.trailingIcon != null
|
2023-01-08 11:15:00 +00:00
|
|
|
? Padding(
|
|
|
|
padding: EdgeInsets.only(
|
|
|
|
right: widget.trailingExtraMargin,
|
|
|
|
),
|
|
|
|
child: Icon(
|
|
|
|
widget.trailingIcon,
|
|
|
|
color: widget.trailingIconIsMuted
|
|
|
|
? enteColorScheme.strokeMuted
|
|
|
|
: widget.trailingIconColor,
|
|
|
|
),
|
2022-10-04 13:15:51 +00:00
|
|
|
)
|
2022-12-06 18:03:39 +00:00
|
|
|
: widget.trailingWidget ?? const SizedBox.shrink(),
|
2022-09-29 02:08:02 +00:00
|
|
|
],
|
2022-09-28 11:51:52 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-10-21 13:37:57 +00:00
|
|
|
|
|
|
|
void _onTapDown(details) {
|
|
|
|
setState(() {
|
2023-01-06 06:15:17 +00:00
|
|
|
if (widget.pressedColor == null) {
|
|
|
|
hasPassedGestureCallbacks()
|
|
|
|
? menuItemColor = getEnteColorScheme(context).fillFaintPressed
|
|
|
|
: menuItemColor = widget.menuItemColor;
|
|
|
|
} else {
|
|
|
|
menuItemColor = widget.pressedColor;
|
|
|
|
}
|
2022-10-21 13:37:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-06 06:15:17 +00:00
|
|
|
bool hasPassedGestureCallbacks() {
|
|
|
|
return widget.onDoubleTap != null || widget.onTap != null;
|
|
|
|
}
|
|
|
|
|
2022-10-21 13:37:57 +00:00
|
|
|
void _onTapUp(details) {
|
|
|
|
Future.delayed(
|
|
|
|
const Duration(milliseconds: 100),
|
|
|
|
() => setState(() {
|
|
|
|
menuItemColor = widget.menuItemColor;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2022-10-21 14:00:17 +00:00
|
|
|
|
|
|
|
void _onCancel() {
|
|
|
|
setState(() {
|
|
|
|
menuItemColor = widget.menuItemColor;
|
|
|
|
});
|
|
|
|
}
|
2022-09-28 11:51:52 +00:00
|
|
|
}
|