ente/lib/ui/components/button_widget.dart

408 lines
15 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:photos/theme/colors.dart';
import 'package:photos/theme/ente_theme.dart';
import 'package:photos/theme/text_style.dart';
import 'package:photos/ui/common/loading_widget.dart';
import 'package:photos/ui/components/models/button_type.dart';
import 'package:photos/ui/components/models/custom_button_style.dart';
import 'package:photos/utils/debouncer.dart';
enum ExecutionState {
idle,
inProgress,
error,
successful;
}
enum ButtonSize {
small,
large;
}
2022-12-16 06:43:25 +00:00
enum ButtonAction {
first,
second,
2022-12-16 06:43:25 +00:00
third,
cancelled,
error;
}
typedef FutureVoidCallback = Future<void> Function();
class ButtonWidget extends StatelessWidget {
final IconData? icon;
final String? labelText;
final ButtonType buttonType;
final FutureVoidCallback? onTap;
2022-12-10 04:55:17 +00:00
final bool isDisabled;
final ButtonSize buttonSize;
///Passing a non null value to this will pop the Navigator stack and return
///buttonIndex along with it when pressed
2022-12-16 06:43:25 +00:00
final ButtonAction? buttonAction;
///setting this flag to true will make the button appear like how it would
///on dark theme irrespective of the app's theme.
final bool isInActionSheet;
///isInAlert is to dismiss the alert if the action on the button is completed
///This flag is true by default if isInAcitonSheet is true
final bool isInAlert;
const ButtonWidget({
required this.buttonType,
required this.buttonSize,
this.icon,
this.labelText,
this.onTap,
this.isInActionSheet = false,
2022-12-10 04:55:17 +00:00
this.isDisabled = false,
2022-12-16 06:43:25 +00:00
this.buttonAction,
this.isInAlert = false,
super.key,
});
@override
Widget build(BuildContext context) {
final colorScheme =
isInActionSheet ? darkScheme : getEnteColorScheme(context);
final inverseColorScheme = isInActionSheet
? lightScheme
: getEnteColorScheme(context, inverse: true);
final textTheme =
isInActionSheet ? darkTextTheme : getEnteTextTheme(context);
final inverseTextTheme = isInActionSheet
? lightTextTheme
: getEnteTextTheme(context, inverse: true);
final buttonStyle = CustomButtonStyle(
2022-12-09 14:00:58 +00:00
//Dummy default values since we need to keep these properties non-nullable
defaultButtonColor: Colors.transparent,
defaultBorderColor: Colors.transparent,
defaultIconColor: Colors.transparent,
defaultLabelStyle: textTheme.body,
);
buttonStyle.defaultButtonColor = buttonType.defaultButtonColor(colorScheme);
buttonStyle.pressedButtonColor = buttonType.pressedButtonColor(colorScheme);
buttonStyle.disabledButtonColor =
2022-12-15 09:22:39 +00:00
buttonType.disabledButtonColor(colorScheme, buttonSize);
buttonStyle.defaultBorderColor =
buttonType.defaultBorderColor(colorScheme, buttonSize);
2022-12-15 05:29:19 +00:00
buttonStyle.pressedBorderColor = buttonType.pressedBorderColor(
2022-12-15 05:56:32 +00:00
colorScheme: colorScheme,
inverseColorScheme: inverseColorScheme,
2022-12-15 09:22:39 +00:00
buttonSize: buttonSize,
2022-12-15 05:56:32 +00:00
);
2022-12-09 14:00:58 +00:00
buttonStyle.disabledBorderColor =
2022-12-15 09:22:39 +00:00
buttonType.disabledBorderColor(colorScheme, buttonSize);
2022-12-09 14:00:58 +00:00
buttonStyle.defaultIconColor = buttonType.defaultIconColor(
colorScheme: colorScheme,
inverseColorScheme: inverseColorScheme,
);
2022-12-15 09:22:39 +00:00
buttonStyle.pressedIconColor =
buttonType.pressedIconColor(colorScheme, buttonSize);
buttonStyle.disabledIconColor =
buttonType.disabledIconColor(colorScheme, buttonSize);
2022-12-09 14:00:58 +00:00
buttonStyle.defaultLabelStyle = buttonType.defaultLabelStyle(
textTheme: textTheme,
inverseTextTheme: inverseTextTheme,
);
2022-12-09 14:00:58 +00:00
buttonStyle.pressedLabelStyle =
2022-12-15 09:22:39 +00:00
buttonType.pressedLabelStyle(textTheme, colorScheme, buttonSize);
2022-12-09 14:00:58 +00:00
buttonStyle.disabledLabelStyle =
buttonType.disabledLabelStyle(textTheme, colorScheme);
2022-12-12 09:48:07 +00:00
buttonStyle.checkIconColor = buttonType.checkIconColor(colorScheme);
return ButtonChildWidget(
2022-12-09 14:00:58 +00:00
buttonStyle: buttonStyle,
buttonType: buttonType,
2022-12-10 04:55:17 +00:00
isDisabled: isDisabled,
buttonSize: buttonSize,
isInAlert: isInActionSheet ? isInActionSheet : isInAlert,
onTap: onTap,
2022-12-09 14:00:58 +00:00
labelText: labelText,
icon: icon,
2022-12-16 06:43:25 +00:00
buttonAction: buttonAction,
2022-12-09 14:00:58 +00:00
);
}
}
class ButtonChildWidget extends StatefulWidget {
final CustomButtonStyle buttonStyle;
final FutureVoidCallback? onTap;
2022-12-09 14:00:58 +00:00
final ButtonType buttonType;
final String? labelText;
final IconData? icon;
2022-12-10 04:55:17 +00:00
final bool isDisabled;
final ButtonSize buttonSize;
2022-12-16 06:43:25 +00:00
final ButtonAction? buttonAction;
final bool isInAlert;
const ButtonChildWidget({
2022-12-09 14:00:58 +00:00
required this.buttonStyle,
required this.buttonType,
2022-12-10 04:55:17 +00:00
required this.isDisabled,
required this.buttonSize,
required this.isInAlert,
2022-12-09 14:00:58 +00:00
this.onTap,
this.labelText,
this.icon,
2022-12-16 06:43:25 +00:00
this.buttonAction,
2022-12-09 14:00:58 +00:00
super.key,
});
@override
State<ButtonChildWidget> createState() => _ButtonChildWidgetState();
2022-12-09 14:00:58 +00:00
}
class _ButtonChildWidgetState extends State<ButtonChildWidget> {
2022-12-09 15:46:05 +00:00
late Color buttonColor;
late Color borderColor;
late Color iconColor;
late TextStyle labelStyle;
2022-12-12 09:48:07 +00:00
late Color checkIconColor;
late Color loadingIconColor;
2022-12-15 11:10:34 +00:00
///This is used to store the width of the button in idle state (small button)
///to be used as width for the button when the loading/succes states comes.
double? widthOfButton;
final _debouncer = Debouncer(const Duration(milliseconds: 300));
ExecutionState executionState = ExecutionState.idle;
2022-12-09 15:46:05 +00:00
@override
void initState() {
2022-12-12 09:48:07 +00:00
checkIconColor = widget.buttonStyle.checkIconColor ??
widget.buttonStyle.defaultIconColor;
loadingIconColor = widget.buttonStyle.defaultIconColor;
2022-12-10 04:55:17 +00:00
if (widget.isDisabled) {
buttonColor = widget.buttonStyle.disabledButtonColor ??
widget.buttonStyle.defaultButtonColor;
borderColor = widget.buttonStyle.disabledBorderColor ??
widget.buttonStyle.defaultBorderColor;
iconColor = widget.buttonStyle.disabledIconColor ??
widget.buttonStyle.defaultIconColor;
labelStyle = widget.buttonStyle.disabledLabelStyle ??
widget.buttonStyle.defaultLabelStyle;
} else {
buttonColor = widget.buttonStyle.defaultButtonColor;
borderColor = widget.buttonStyle.defaultBorderColor;
iconColor = widget.buttonStyle.defaultIconColor;
labelStyle = widget.buttonStyle.defaultLabelStyle;
}
2022-12-09 15:46:05 +00:00
super.initState();
}
2022-12-09 14:00:58 +00:00
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: _shouldRegisterGestures ? _onTap : null,
onTapDown: _shouldRegisterGestures ? _onTapDown : null,
onTapUp: _shouldRegisterGestures ? _onTapUp : null,
onTapCancel: _shouldRegisterGestures ? _onTapCancel : null,
2022-12-09 15:46:05 +00:00
child: AnimatedContainer(
duration: const Duration(milliseconds: 16),
width: widget.buttonSize == ButtonSize.large ? double.infinity : null,
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(4)),
2022-12-09 15:46:05 +00:00
color: buttonColor,
border: Border.all(color: borderColor),
),
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 14, horizontal: 16),
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 175),
switchInCurve: Curves.easeInOutExpo,
switchOutCurve: Curves.easeInOutExpo,
child: executionState == ExecutionState.idle
? widget.buttonType.hasTrailingIcon
? Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
widget.labelText == null
? const SizedBox.shrink()
: Flexible(
child: Padding(
padding: widget.icon == null
? const EdgeInsets.symmetric(
horizontal: 8,
)
: const EdgeInsets.only(right: 16),
child: Text(
widget.labelText!,
overflow: TextOverflow.ellipsis,
maxLines: 2,
style: labelStyle,
),
),
),
widget.icon == null
? const SizedBox.shrink()
: Icon(
widget.icon,
size: 20,
color: iconColor,
),
],
)
: Builder(
builder: (context) {
SchedulerBinding.instance.addPostFrameCallback(
(timeStamp) {
final box =
context.findRenderObject() as RenderBox;
widthOfButton = box.size.width;
},
);
return Row(
mainAxisSize: widget.buttonSize == ButtonSize.large
? MainAxisSize.max
: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
widget.icon == null
? const SizedBox.shrink()
: Icon(
widget.icon,
size: 20,
color: iconColor,
),
widget.icon == null || widget.labelText == null
? const SizedBox.shrink()
: const SizedBox(width: 8),
widget.labelText == null
? const SizedBox.shrink()
: Flexible(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8,
),
child: Text(
widget.labelText!,
style: labelStyle,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
),
)
],
);
},
)
: executionState == ExecutionState.inProgress
? SizedBox(
width: widthOfButton,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
EnteLoadingWidget(
is20pts: true,
color: loadingIconColor,
),
],
),
)
: executionState == ExecutionState.successful
? SizedBox(
width: widthOfButton,
child: Icon(
Icons.check_outlined,
size: 20,
color: checkIconColor,
),
)
: const SizedBox.shrink(), //fallback
),
),
),
);
}
2022-12-09 15:46:05 +00:00
bool get _shouldRegisterGestures =>
!widget.isDisabled &&
(widget.onTap != null) &&
executionState == ExecutionState.idle;
void _onTap() async {
2022-12-15 05:56:32 +00:00
_debouncer.run(
() => Future(() {
setState(() {
2022-12-15 05:56:32 +00:00
executionState = ExecutionState.inProgress;
});
}),
);
await widget.onTap!.call().onError((error, stackTrace) {
executionState = ExecutionState.error;
_debouncer.cancelDebounce();
});
2022-12-15 05:56:32 +00:00
_debouncer.cancelDebounce();
// when the time taken by widget.onTap is approximately equal to the debounce
// time, the callback is getting executed when/after the if condition
// below is executing/executed which results in execution state stuck at
// idle state. This Future is for delaying the execution of the if
// condition so that the calback in the debouncer finishes execution before.
await Future.delayed(const Duration(milliseconds: 5));
if (executionState == ExecutionState.inProgress) {
setState(() {
executionState = ExecutionState.successful;
Future.delayed(Duration(seconds: widget.isInAlert ? 1 : 2), () {
widget.buttonAction != null && widget.isInAlert
? Navigator.of(context, rootNavigator: true)
.pop(ButtonAction.error)
: null;
if (mounted) {
setState(() {
executionState = ExecutionState.idle;
});
}
});
2022-12-15 05:56:32 +00:00
});
}
if (executionState == ExecutionState.error) {
setState(() {
executionState = ExecutionState.idle;
widget.buttonAction != null && widget.isInAlert
? Future.delayed(
const Duration(seconds: 1),
() => Navigator.of(context, rootNavigator: true).pop(
ButtonAction.error,
),
)
: null;
});
2022-12-10 07:43:55 +00:00
}
}
2022-12-09 15:46:05 +00:00
void _onTapDown(details) {
setState(() {
buttonColor = widget.buttonStyle.pressedButtonColor ??
widget.buttonStyle.defaultButtonColor;
borderColor = widget.buttonStyle.pressedBorderColor ??
widget.buttonStyle.defaultBorderColor;
iconColor = widget.buttonStyle.pressedIconColor ??
widget.buttonStyle.defaultIconColor;
labelStyle = widget.buttonStyle.pressedLabelStyle ??
widget.buttonStyle.defaultLabelStyle;
});
}
void _onTapUp(details) {
Future.delayed(
const Duration(milliseconds: 84),
() => setState(() {
setAllStylesToDefault();
}),
);
}
void _onTapCancel() {
setState(() {
setAllStylesToDefault();
});
}
void setAllStylesToDefault() {
buttonColor = widget.buttonStyle.defaultButtonColor;
borderColor = widget.buttonStyle.defaultBorderColor;
iconColor = widget.buttonStyle.defaultIconColor;
labelStyle = widget.buttonStyle.defaultLabelStyle;
}
}