button_widget.dart 15 KB

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