ente/lib/ui/components/menu_item_widget.dart

124 lines
3.9 KiB
Dart
Raw Normal View History

import 'package:expandable/expandable.dart';
2022-09-28 05:00:43 +00:00
import 'package:flutter/material.dart';
import 'package:photos/ente_theme_data.dart';
class MenuItemWidget extends StatefulWidget {
2022-09-28 12:58:07 +00:00
final Widget captionedTextWidget;
final bool isHeaderOfExpansion;
2022-10-07 04:56: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
final IconData? leadingIcon;
final Color? leadingIconColor;
2022-10-07 04:56: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;
final Widget? trailingSwitch;
final bool trailingIconIsMuted;
2022-10-04 10:07:55 +00:00
final VoidCallback? onTap;
final VoidCallback? onDoubleTap;
2022-09-29 12:21:31 +00:00
final Color? menuItemColor;
final bool alignCaptionedTextToLeft;
final double borderRadius;
final ExpandableController? expandableController;
const MenuItemWidget({
2022-09-28 12:58:07 +00:00
required this.captionedTextWidget,
this.isHeaderOfExpansion = false,
this.leadingIcon,
this.leadingIconColor,
this.trailingIcon,
this.trailingSwitch,
this.trailingIconIsMuted = false,
this.onTap,
this.onDoubleTap,
2022-09-29 12:21:31 +00:00
this.menuItemColor,
this.alignCaptionedTextToLeft = false,
this.borderRadius = 4.0,
this.expandableController,
Key? key,
}) : super(key: key);
@override
State<MenuItemWidget> createState() => _MenuItemWidgetState();
}
class _MenuItemWidgetState extends State<MenuItemWidget> {
@override
void initState() {
if (widget.expandableController != null) {
widget.expandableController!.addListener(() {
setState(() {});
});
}
super.initState();
}
@override
void dispose() {
if (widget.expandableController != null) {
widget.expandableController!.dispose();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return widget.isHeaderOfExpansion
? menuItemWidget(context)
: GestureDetector(
2022-10-04 10:07:55 +00:00
onTap: widget.onTap,
onDoubleTap: widget.onDoubleTap,
child: menuItemWidget(context),
);
}
Widget menuItemWidget(BuildContext context) {
2022-10-05 14:14:28 +00:00
final enteColorScheme = Theme.of(context).colorScheme.enteTheme.colorScheme;
return Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 12),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(widget.borderRadius),
color: widget.menuItemColor,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
widget.alignCaptionedTextToLeft && widget.leadingIcon == null
? const SizedBox.shrink()
: Padding(
padding: const EdgeInsets.only(right: 10),
child: SizedBox(
height: 20,
width: 20,
child: widget.leadingIcon == null
? const SizedBox.shrink()
: FittedBox(
fit: BoxFit.contain,
child: Icon(
widget.leadingIcon,
color: widget.leadingIconColor,
),
),
),
),
widget.captionedTextWidget,
widget.expandableController != null
? _isExpanded()
? const SizedBox.shrink()
: Icon(widget.trailingIcon)
: widget.trailingIcon != null
? Icon(
widget.trailingIcon,
color: widget.trailingIconIsMuted
2022-10-05 14:14:28 +00:00
? enteColorScheme.strokeMuted
: null,
)
2022-10-05 15:57:41 +00:00
: widget.trailingSwitch ?? const SizedBox.shrink(),
],
),
);
}
bool _isExpanded() {
return widget.expandableController!.value;
}
}