button_widget.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/scheduler.dart';
  3. import 'package:photos/theme/colors.dart';
  4. import 'package:photos/theme/ente_theme.dart';
  5. import 'package:photos/theme/text_style.dart';
  6. import 'package:photos/ui/common/loading_widget.dart';
  7. import 'package:photos/ui/components/models/button_type.dart';
  8. import 'package:photos/ui/components/models/custom_button_style.dart';
  9. import 'package:photos/utils/debouncer.dart';
  10. enum ExecutionState {
  11. idle,
  12. inProgress,
  13. successful,
  14. }
  15. enum ButtonSize {
  16. small,
  17. large;
  18. }
  19. typedef FutureVoidCallback = Future<void> Function();
  20. class ButtonWidget extends StatelessWidget {
  21. final IconData? icon;
  22. final String? labelText;
  23. final ButtonType buttonType;
  24. final FutureVoidCallback? onTap;
  25. final bool isDisabled;
  26. final ButtonSize buttonSize;
  27. ///setting this flag to true will make the button appear like how it would
  28. ///on dark theme irrespective of the app's theme.
  29. final bool isInActionSheet;
  30. const ButtonWidget({
  31. required this.buttonType,
  32. required this.buttonSize,
  33. this.icon,
  34. this.labelText,
  35. this.onTap,
  36. this.isInActionSheet = false,
  37. this.isDisabled = false,
  38. super.key,
  39. });
  40. @override
  41. Widget build(BuildContext context) {
  42. final colorScheme =
  43. isInActionSheet ? darkScheme : getEnteColorScheme(context);
  44. final inverseColorScheme = isInActionSheet
  45. ? lightScheme
  46. : getEnteColorScheme(context, inverse: true);
  47. final textTheme =
  48. isInActionSheet ? darkTextTheme : getEnteTextTheme(context);
  49. final inverseTextTheme = isInActionSheet
  50. ? lightTextTheme
  51. : getEnteTextTheme(context, inverse: true);
  52. final buttonStyle = CustomButtonStyle(
  53. //Dummy default values since we need to keep these properties non-nullable
  54. defaultButtonColor: Colors.transparent,
  55. defaultBorderColor: Colors.transparent,
  56. defaultIconColor: Colors.transparent,
  57. defaultLabelStyle: textTheme.body,
  58. );
  59. buttonStyle.defaultButtonColor = buttonType.defaultButtonColor(colorScheme);
  60. buttonStyle.pressedButtonColor = buttonType.pressedButtonColor(colorScheme);
  61. buttonStyle.disabledButtonColor =
  62. buttonType.disabledButtonColor(colorScheme, buttonSize);
  63. buttonStyle.defaultBorderColor =
  64. buttonType.defaultBorderColor(colorScheme, buttonSize);
  65. buttonStyle.pressedBorderColor = buttonType.pressedBorderColor(
  66. colorScheme: colorScheme,
  67. inverseColorScheme: inverseColorScheme,
  68. buttonSize: buttonSize,
  69. );
  70. buttonStyle.disabledBorderColor =
  71. buttonType.disabledBorderColor(colorScheme, buttonSize);
  72. buttonStyle.defaultIconColor = buttonType.defaultIconColor(
  73. colorScheme: colorScheme,
  74. inverseColorScheme: inverseColorScheme,
  75. );
  76. buttonStyle.pressedIconColor =
  77. buttonType.pressedIconColor(colorScheme, buttonSize);
  78. buttonStyle.disabledIconColor =
  79. buttonType.disabledIconColor(colorScheme, buttonSize);
  80. buttonStyle.defaultLabelStyle = buttonType.defaultLabelStyle(
  81. textTheme: textTheme,
  82. inverseTextTheme: inverseTextTheme,
  83. );
  84. buttonStyle.pressedLabelStyle =
  85. buttonType.pressedLabelStyle(textTheme, colorScheme, buttonSize);
  86. buttonStyle.disabledLabelStyle =
  87. buttonType.disabledLabelStyle(textTheme, colorScheme);
  88. buttonStyle.checkIconColor = buttonType.checkIconColor(colorScheme);
  89. return LargeButtonChildWidget(
  90. buttonStyle: buttonStyle,
  91. buttonType: buttonType,
  92. isDisabled: isDisabled,
  93. buttonSize: buttonSize,
  94. onTap: onTap,
  95. labelText: labelText,
  96. icon: icon,
  97. );
  98. }
  99. }
  100. class LargeButtonChildWidget extends StatefulWidget {
  101. final CustomButtonStyle buttonStyle;
  102. final FutureVoidCallback? onTap;
  103. final ButtonType buttonType;
  104. final String? labelText;
  105. final IconData? icon;
  106. final bool isDisabled;
  107. final ButtonSize buttonSize;
  108. const LargeButtonChildWidget({
  109. required this.buttonStyle,
  110. required this.buttonType,
  111. required this.isDisabled,
  112. required this.buttonSize,
  113. this.onTap,
  114. this.labelText,
  115. this.icon,
  116. super.key,
  117. });
  118. @override
  119. State<LargeButtonChildWidget> createState() => _LargeButtonChildWidgetState();
  120. }
  121. class _LargeButtonChildWidgetState extends State<LargeButtonChildWidget> {
  122. late Color buttonColor;
  123. late Color borderColor;
  124. late Color iconColor;
  125. late TextStyle labelStyle;
  126. late Color checkIconColor;
  127. late Color loadingIconColor;
  128. double? widthOfButton;
  129. final _debouncer = Debouncer(const Duration(milliseconds: 300));
  130. ExecutionState executionState = ExecutionState.idle;
  131. @override
  132. void initState() {
  133. checkIconColor = widget.buttonStyle.checkIconColor ??
  134. widget.buttonStyle.defaultIconColor;
  135. loadingIconColor = widget.buttonStyle.defaultIconColor;
  136. if (widget.isDisabled) {
  137. buttonColor = widget.buttonStyle.disabledButtonColor ??
  138. widget.buttonStyle.defaultButtonColor;
  139. borderColor = widget.buttonStyle.disabledBorderColor ??
  140. widget.buttonStyle.defaultBorderColor;
  141. iconColor = widget.buttonStyle.disabledIconColor ??
  142. widget.buttonStyle.defaultIconColor;
  143. labelStyle = widget.buttonStyle.disabledLabelStyle ??
  144. widget.buttonStyle.defaultLabelStyle;
  145. } else {
  146. buttonColor = widget.buttonStyle.defaultButtonColor;
  147. borderColor = widget.buttonStyle.defaultBorderColor;
  148. iconColor = widget.buttonStyle.defaultIconColor;
  149. labelStyle = widget.buttonStyle.defaultLabelStyle;
  150. }
  151. super.initState();
  152. }
  153. @override
  154. Widget build(BuildContext context) {
  155. return GestureDetector(
  156. onTap: _shouldRegisterGestures ? _onTap : null,
  157. onTapDown: _shouldRegisterGestures ? _onTapDown : null,
  158. onTapUp: _shouldRegisterGestures ? _onTapUp : null,
  159. onTapCancel: _shouldRegisterGestures ? _onTapCancel : null,
  160. child: AnimatedContainer(
  161. duration: const Duration(milliseconds: 16),
  162. width: widget.buttonSize == ButtonSize.large ? double.infinity : null,
  163. decoration: BoxDecoration(
  164. borderRadius: const BorderRadius.all(Radius.circular(4)),
  165. color: buttonColor,
  166. border: Border.all(color: borderColor),
  167. ),
  168. child: Padding(
  169. padding: const EdgeInsets.symmetric(vertical: 14, horizontal: 16),
  170. child: AnimatedSwitcher(
  171. duration: const Duration(milliseconds: 175),
  172. switchInCurve: Curves.easeInOutExpo,
  173. switchOutCurve: Curves.easeInOutExpo,
  174. child: executionState == ExecutionState.idle
  175. ? widget.buttonType.hasTrailingIcon
  176. ? Row(
  177. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  178. children: [
  179. widget.labelText == null
  180. ? const SizedBox.shrink()
  181. : Flexible(
  182. child: Padding(
  183. padding: widget.icon == null
  184. ? const EdgeInsets.symmetric(
  185. horizontal: 8,
  186. )
  187. : const EdgeInsets.only(right: 16),
  188. child: Text(
  189. widget.labelText!,
  190. overflow: TextOverflow.ellipsis,
  191. maxLines: 2,
  192. style: labelStyle,
  193. ),
  194. ),
  195. ),
  196. widget.icon == null
  197. ? const SizedBox.shrink()
  198. : Icon(
  199. widget.icon,
  200. size: 20,
  201. color: iconColor,
  202. ),
  203. ],
  204. )
  205. : Builder(
  206. builder: (context) {
  207. SchedulerBinding.instance.addPostFrameCallback(
  208. (timeStamp) {
  209. final box =
  210. context.findRenderObject() as RenderBox;
  211. widthOfButton = box.size.width;
  212. },
  213. );
  214. return Row(
  215. mainAxisSize: widget.buttonSize == ButtonSize.large
  216. ? MainAxisSize.max
  217. : MainAxisSize.min,
  218. mainAxisAlignment: MainAxisAlignment.center,
  219. children: [
  220. widget.icon == null
  221. ? const SizedBox.shrink()
  222. : Icon(
  223. widget.icon,
  224. size: 20,
  225. color: iconColor,
  226. ),
  227. widget.icon == null || widget.labelText == null
  228. ? const SizedBox.shrink()
  229. : const SizedBox(width: 8),
  230. widget.labelText == null
  231. ? const SizedBox.shrink()
  232. : Flexible(
  233. child: Padding(
  234. padding: const EdgeInsets.symmetric(
  235. horizontal: 8,
  236. ),
  237. child: Text(
  238. widget.labelText!,
  239. style: labelStyle,
  240. maxLines: 2,
  241. overflow: TextOverflow.ellipsis,
  242. ),
  243. ),
  244. )
  245. ],
  246. );
  247. },
  248. )
  249. : executionState == ExecutionState.inProgress
  250. ? SizedBox(
  251. width: widthOfButton,
  252. child: Row(
  253. mainAxisAlignment: MainAxisAlignment.center,
  254. mainAxisSize: MainAxisSize.min,
  255. children: [
  256. EnteLoadingWidget(
  257. is20pts: true,
  258. color: loadingIconColor,
  259. ),
  260. ],
  261. ),
  262. )
  263. : executionState == ExecutionState.successful
  264. ? SizedBox(
  265. width: widthOfButton,
  266. child: Icon(
  267. Icons.check_outlined,
  268. size: 20,
  269. color: checkIconColor,
  270. ),
  271. )
  272. : const SizedBox.shrink(), //fallback
  273. ),
  274. ),
  275. ),
  276. );
  277. }
  278. bool get _shouldRegisterGestures =>
  279. !widget.isDisabled &&
  280. (widget.onTap != null) &&
  281. executionState == ExecutionState.idle;
  282. void _onTap() async {
  283. _debouncer.run(
  284. () => Future(() {
  285. setState(() {
  286. executionState = ExecutionState.inProgress;
  287. });
  288. }),
  289. );
  290. await widget.onTap!
  291. .call()
  292. .onError((error, stackTrace) => _debouncer.cancelDebounce());
  293. _debouncer.cancelDebounce();
  294. // when the time taken by widget.onTap is approximately equal to the debounce
  295. // time, the callback is getting executed when/after the if condition
  296. // below is executing/executed which results in execution state stuck at
  297. // idle state. This Future is for delaying the execution of the if
  298. // condition so that the calback in the debouncer finishes execution before.
  299. await Future.delayed(const Duration(milliseconds: 5));
  300. if (executionState == ExecutionState.inProgress) {
  301. setState(() {
  302. executionState = ExecutionState.successful;
  303. Future.delayed(const Duration(seconds: 2), () {
  304. setState(() {
  305. executionState = ExecutionState.idle;
  306. });
  307. });
  308. });
  309. }
  310. }
  311. void _onTapDown(details) {
  312. setState(() {
  313. buttonColor = widget.buttonStyle.pressedButtonColor ??
  314. widget.buttonStyle.defaultButtonColor;
  315. borderColor = widget.buttonStyle.pressedBorderColor ??
  316. widget.buttonStyle.defaultBorderColor;
  317. iconColor = widget.buttonStyle.pressedIconColor ??
  318. widget.buttonStyle.defaultIconColor;
  319. labelStyle = widget.buttonStyle.pressedLabelStyle ??
  320. widget.buttonStyle.defaultLabelStyle;
  321. });
  322. }
  323. void _onTapUp(details) {
  324. Future.delayed(
  325. const Duration(milliseconds: 84),
  326. () => setState(() {
  327. setAllStylesToDefault();
  328. }),
  329. );
  330. }
  331. void _onTapCancel() {
  332. setState(() {
  333. setAllStylesToDefault();
  334. });
  335. }
  336. void setAllStylesToDefault() {
  337. buttonColor = widget.buttonStyle.defaultButtonColor;
  338. borderColor = widget.buttonStyle.defaultBorderColor;
  339. iconColor = widget.buttonStyle.defaultIconColor;
  340. labelStyle = widget.buttonStyle.defaultLabelStyle;
  341. }
  342. }