button_widget.dart 15 KB

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