ente/lib/ui/components/icon_button_widget.dart

106 lines
2.7 KiB
Dart
Raw Normal View History

2022-10-27 07:14:52 +00:00
import 'package:flutter/material.dart';
import 'package:photos/theme/colors.dart';
2022-10-27 07:14:52 +00:00
import 'package:photos/theme/ente_theme.dart';
2022-10-27 10:46:02 +00:00
class IconButtonWidget extends StatefulWidget {
2022-10-27 07:14:52 +00:00
final bool isPrimary;
final bool isSecondary;
final bool isRounded;
final IconData icon;
final bool disableGestureDetector;
2022-10-27 10:46:02 +00:00
final VoidCallback? onTap;
final Color? defaultColor;
final Color? pressedColor;
final Color? iconColor;
2022-10-27 07:14:52 +00:00
const IconButtonWidget({
2022-10-27 10:46:02 +00:00
required this.icon,
this.isPrimary = false,
2022-10-27 07:14:52 +00:00
this.isSecondary = false,
this.isRounded = false,
this.disableGestureDetector = false,
2022-10-27 10:46:02 +00:00
this.onTap,
this.defaultColor,
this.pressedColor,
this.iconColor,
2022-10-27 07:14:52 +00:00
super.key,
});
2022-10-27 10:46:02 +00:00
@override
State<IconButtonWidget> createState() => _IconButtonWidgetState();
}
class _IconButtonWidgetState extends State<IconButtonWidget> {
Color? iconStateColor;
@override
void didChangeDependencies() {
setState(() {
iconStateColor = null;
});
super.didChangeDependencies();
}
2022-10-27 07:14:52 +00:00
@override
Widget build(BuildContext context) {
if (!widget.isPrimary && !widget.isRounded && !widget.isSecondary) {
return const SizedBox.shrink();
}
2022-10-27 07:14:52 +00:00
final colorTheme = getEnteColorScheme(context);
2022-10-27 10:46:02 +00:00
iconStateColor ??
(iconStateColor = widget.defaultColor ??
(widget.isRounded ? colorTheme.fillFaint : null));
return widget.disableGestureDetector
? _iconButton(colorTheme)
: GestureDetector(
onTapDown: _onTapDown,
onTapUp: _onTapUp,
onTapCancel: _onTapCancel,
onTap: widget.onTap,
child: _iconButton(colorTheme),
);
}
Widget _iconButton(EnteColorScheme colorTheme) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: AnimatedContainer(
duration: const Duration(milliseconds: 20),
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: iconStateColor,
),
child: Icon(
widget.icon,
color: widget.iconColor ??
(widget.isSecondary
? colorTheme.strokeMuted
: colorTheme.strokeBase),
size: 24,
),
2022-10-27 07:14:52 +00:00
),
);
}
2022-10-27 10:46:02 +00:00
_onTapDown(details) {
final colorTheme = getEnteColorScheme(context);
setState(() {
iconStateColor = widget.pressedColor ??
(widget.isRounded ? colorTheme.fillMuted : colorTheme.fillFaint);
2022-10-27 10:46:02 +00:00
});
}
_onTapUp(details) {
Future.delayed(const Duration(milliseconds: 100), () {
setState(() {
iconStateColor = null;
});
});
}
_onTapCancel() {
setState(() {
iconStateColor = null;
});
}
2022-10-27 07:14:52 +00:00
}